Uploading file to Fedora php server creates empty file

Dear Experts,

I looked at : file upload script uploads empty file (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23060161.html)

but I still have the same problem.. uploading a File creates the file but doesn't fill in the data.

I gave (on my code) ownership of the folder to Apache and did a chmod 755.

I looked at my php.ini:

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
upload_tmp_dir = /tmp/

; Maximum allowed size for uploaded files.
upload_max_filesize = 12M


Yet it's still not working.. I moved the code from another server (so I know my code works) and even with the test code I got from the other question, I'm not getting any close..

how do I upload the file so it actually fills the data?
LVL 1
DraceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

alexbumbaceaCommented:
You should check if SE linux is disabled
http://www.crypt.gen.nz/selinux/disable_selinux.html
0
NitrowareCommented:
Yes, many times have I been killed by SE Linux.  Defiantly check that first.

Other Questions:
1) How is this Data being uploaded (Flash, HTML form, java, etc)
2) Can you post the PHP code that is handling the upload
3) Can you post the code if it is HTML, especally the "<form" tag
4) Is PHP safe mode on? (in phpinfo();)

That would be very helpful.  I have has this issue before I just cant remember what the issue was.
0
DraceAuthor Commented:
SElinux is disabled, i checked
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

DraceAuthor Commented:
I used the code from the other question.. which claims that it works.. and I think it does.


<?php
if (isset($_FILES['file'])){
        $file = $_FILES['file'];
        move_uploaded_file($file['tmp_name'],$file['name']); }
?>
<form method="POST" enctype="multipart/form-data"><input type="file" name="file"><br><input type="submit" name="submit" value="Upload">
</form>

Open in new window

0
DraceAuthor Commented:

Hi,

I also checked for Php safe mode:

safe_mode => Off => Off


I still need help on this!
0
DraceAuthor Commented:
Here's my SElinux file:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#      enforcing - SELinux security policy is enforced.
#      permissive - SELinux prints warnings instead of enforcing.
#      disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#      targeted - Only targeted network daemons are protected.
#      strict - Full SELinux protection.
SELINUXTYPE=targeted
0
NitrowareCommented:
Have you enabled errors via the following:


error_reporting(E_ALL);
ini_set('display_errors', '1');

Open in new window

0
DraceAuthor Commented:

Hi,

Just added the code and I still don't get any errors...

Looked at the error_log file and its not showing errors.

What else can I try?

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

if (isset($_FILES['file'])){
        $file = $_FILES['file'];
        move_uploaded_file($file['tmp_name'],$file['name']); }
?>
<form method="POST" enctype="multipart/form-data"><input type="file" name="file"><br><input type="submit" name="submit" value="Upload">
</form>

Open in new window

0
DraceAuthor Commented:
I'm still trying to get this to work on my server.

So far all the solutions offered didn't solve my problem.

0
13598Commented:
How big is the file?
Take a look at this:
i was having difficulty with the upload_max_filesize , if u set the max file size lesser than the php setting then ur script to report error will only work till this difference between ur max set file size and the php set max size .Hence if the uploaded file exceeds the php max file size then php end abruptly without a trace of error that is it behaves like no file is uploaded and hence no error reported .Sure if uploading a file is optional for a form then a user who uploads larger file will get no error and still the form will be processed only without the file.
0
Ray PaseurCommented:
File uploads may create error information that is useful.  Please try this:

var_dump($_FILES);

And then refer to the man pages here:
http://us3.php.net/manual/en/features.file-upload.php
http://us3.php.net/manual/en/features.file-upload.errors.php
http://us3.php.net/manual/en/features.file-upload.common-pitfalls.php

An example of how to do a file upload is available here:
http://us3.php.net/manual/en/features.file-upload.post-method.php

Please look that information over (all of the moving parts are important) and post back with any questions.  Best regards, ~Ray
0
Ray PaseurCommented:
Did you try the var_dump() suggested?  If so, please post the output you got from that.  Did you read the man pages referred to above?  Were there any questions you wanted us to answer?

Here is a complete code example of how I do file uploads.  You can install it and run it.  Please feel free to modify it to suit your own needs, including adding in the var_dump() and error handling code that will help you diagnose your issues.  Then post back with any questions and a link to the installed script.  

Best regards, ~Ray
<?php // RAY_upload_example.php
error_reporting(E_ALL);


// MANUAL REFERENCE PAGES
// http://docs.php.net/manual/en/features.file-upload.php
// http://docs.php.net/manual/en/features.file-upload.common-pitfalls.php
// http://docs.php.net/manual/en/function.move-uploaded-file.php
// http://docs.php.net/manual/en/function.getimagesize.php


// ESTABLISH THE NAME OF THE 'uploads' DIRECTORY
$uploads = 'uploads';

// ESTABLISH THE BIGGEST FILE SIZE WE CAN ACCEPT
$max_file_size = '8192000';  // EIGHT MEGABYTE LIMIT ON UPLOADS

// ESTABLISH THE KINDS OF FILE EXTENSIONS WE CAN ACCEPT
$file_exts = array('jpg', 'gif', 'png', 'txt', 'pdf');

// ESTABLISH THE MAXIMUM NUMBER OF FILES WE CAN UPLOAD
$nf = 3;


// THIS IS A LIST OF THE POSSIBLE ERRORS THAT CAN BE REPORTED IN $_FILES[]["error"]
$errors    = array(
    0 => "Success!",
    1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
    2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
    3 => "The uploaded file was only partially uploaded",
    4 => "No file was uploaded",
    6 => "Missing a temporary folder",
    7 => "Cannot write file to disk"
);


// IF THERE IS NOTHING IN $_POST, PUT UP THE FORM FOR INPUT
if (empty($_POST))
{
    ?>
    <h2>Upload <?php echo $nf; ?> file(s)</h2>

    <!--
        SOME THINGS TO NOTE ABOUT THIS FORM...
        NOTE THE CHOICE OF ENCTYPE IN THE HTML FORM STATEMENT
        MAX_FILE_SIZE MUST PRECEDE THE FILE INPUT FIELD
        INPUT NAME= IN TYPE=FILE DETERMINES THE NAME YOU FIND IN $_FILES ARRAY
    -->

    <form name="UploadForm" enctype="multipart/form-data" action="<?=$_SERVER["REQUEST_URI"]?>" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?=$max_file_size?>" />
    <p>
    Find the file(s) you want to upload and click the "Upload" button below.
    </p>

    <?php // CREATE INPUT STATEMENTS FOR UP TO $n FILE NAMES
    for ($n = 0; $n < $nf; $n++)
    {
        echo "<input name=\"userfile$n\" type=\"file\" size=\"80\" /><br/>\n";
    }
    ?>

    <br/>Check this box <input autocomplete="off" type="checkbox" name="overwrite" /> to <b>overwrite</b> existing files.
    <input type="submit" name="_submit" value="Upload" />
    </form>
    <?php
    die();
}
// END OF THE FORM SCRIPT


else // WE HAVE GOT SOMETHING IN $_POST - RUN THE ACTION SCRIPT
{

// THERE IS POST DATA - PROCESS IT
    echo "<h2>Results: File Upload</h2>\n";

// ACTIVATE THIS TO SEE WHAT IS COMING THROUGH
//    echo "<pre>"; var_dump($_FILES); var_dump($_POST); echo "</pre>\n";

// ITERATE OVER THE CONTENTS OF $_FILES
    foreach ($_FILES as $my_uploaded_file)
    {

// SKIP OVER EMPTY SPOTS - NOTHING UPLOADED
        $error_code    = $my_uploaded_file["error"];
        if ($error_code == 4) continue;

// SYNTHESIZE THE NEW FILE NAME
        $f_type    = trim(strtolower(end    (explode( '.', basename($my_uploaded_file['name'] )))));
        $f_name    = trim(strtolower(current(explode( '.', basename($my_uploaded_file['name'] )))));
        $my_new_file = getcwd() . '/' . $uploads . '/' . $f_name .'.'. $f_type;
        $my_file     = $uploads . '/' . $f_name .'.'. $f_type;

// OPTIONAL TEST FOR ALLOWABLE EXTENSIONS
        if (!in_array($f_type, $file_exts)) die("Sorry, $f_type files not allowed");

// IF THERE ARE ERRORS
        if ($error_code != 0)
        {
            $error_message = $errors[$error_code];
            die("Sorry, Upload Error Code: $error_code: $error_message");
        }

// GET THE FILE SIZE
        $file_size    = number_format($my_uploaded_file["size"]);

// MOVE THE FILE INTO THE DIRECTORY
// IF THE FILE IS NEW
        if (!file_exists($my_new_file))
        {
            if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
            {
                $upload_success = 1;
            }
            else
            {
                $upload_success = -1;
            }

// IF THE FILE ALREADY EXISTS
        }
        else
        {
            echo "<br/><b><i>$my_file</i></b> already exists.\n";

// SHOULD WE OVERWRITE THE FILE? IF NOT
            if (empty($_POST["overwrite"]))
            {
                $upload_success = 0;

// IF WE SHOULD OVERWRITE THE FILE, TRY TO MAKE A BACKUP
            }
            else
            {
                $now    = date('Y-m-d');
                $my_bak = $my_new_file . '.' . $now . '.bak';
                if (!copy($my_new_file, $my_bak))
                {
                    echo "<br/><b>Attempted Backup Failed!</b>\n";
                }
                if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
                {
                    $upload_success = 2;
                }
                else
                {
                    $upload_success = -1;
                }
            }
        }

// REPORT OUR SUCCESS OR FAILURE
        if ($upload_success == 2) { echo "<br/>It has been overwritten.\n"; }
        if ($upload_success == 1) { echo "<br/><b><i>$my_file</i></b> has been saved.\n"; }
        if ($upload_success == 0) { echo "<br/><b>It was NOT overwritten.</b>\n"; }
        if ($upload_success < 0)  { echo "<br/><b>ERROR <i>$my_file</i> NOT SAVED - SEE WARNING FROM move_uploaded_file() COMMAND</b>\n"; }
        if ($upload_success > 0)
        {
            echo "$file_size bytes uploaded.\n";
            if (!chmod ($my_new_file, 0755))
            {
                echo "<br/>chmod(0755) FAILED: fileperms() = ";
                echo substr(sprintf('%o', fileperms($my_new_file)), -4);
            }
            echo "<br/><a href=\"$my_file\">See the file $my_file</a>\n";
        }
// END ITERATOR
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Standing by.  Best to all, ~Ray
0
DraceAuthor Commented:
Hi Ray,

ok, I'm trying this out and here's what I got so far:


Results: File Upload

ERROR uploads/pn.txt NOT SAVED - SEE WARNING FROM move_uploaded_file() COMMAND


pn.txt is a simple notepad file I tried uploading.. I'll try anything as long as you give me steps to follow!

Let me know what I should do next.. thanks!

- Drace
0
DraceAuthor Commented:
helped me determine the error, and through the error_log I was able to solve the issue... BIG THANKS for the help !!!
0
Ray PaseurCommented:
Thanks for the points!  Best of luck with your project, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.