1

I can't seem to get files to be uploaded with PHP. From what I can tell, everything checks out. I even went as far as digging up the old textbook and copied the example straight out of the text and still no go.

At first, I was thinking it was an issue with the directory permissions where the files are being saved so I changed permissions to 777 and still nothing. If anyone has any suggestions I'm all ears :/

Here is the code (HTML):

  <form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file1">
        <br/>
    <input type="submit" value="Upload">
  </form>

The PHP file:

<?php
$tmp_name = $_FILES['file1']['tmp_name'];
$path = getcwd() . DIRECTORY_SEPARATOR . 'images';
$name = $path . DIRECTORY_SEPARATOR . $_FILES['file1']['name'];
$success = move_uploaded_file($tmp_name, $name);
if($success) {
    $upload_message = $name . ' has been uploaded.';
    echo "$upload_message";
} else {
    echo "something went wrong :(";
}
?>
4
  • The code is not structured that way in the actual file...the html and php are in their correct positions. I just uploaded the two parts so people can see both aspects without including the ENTIRE file
    – whitwhoa
    Sep 18, 2012 at 0:52
  • What does $name resolve to just before you do move_uploaded_file()?
    – G-Nugget
    Sep 18, 2012 at 0:54
  • Is move_uploaded_file returning false or true but not moving the file?
    – Jared
    Sep 18, 2012 at 1:01
  • $name resolves to /var/www/site.net/aClass/images/ just before move_uploaded_file()....I'm not sure what value is getting returned into $success. I try to echo it and I don't get anything back (echo "$success";), which is quite strange :/
    – whitwhoa
    Sep 18, 2012 at 2:43

3 Answers 3

2

You should be checking for an upload error before attempting to move it then you can track down the problem: http://www.php.net/manual/en/features.file-upload.errors.php

Heres a basic example:

<?php
error_reporting(-1);

$uploaddir = './images/';

// Check for upload attempt
if(isset($_FILES['file1'])){
    $uploadfile = $uploaddir.basename($_FILES['file1']['name']);

    // If no error
    if($_FILES['file1']['error'] == 0){
        //Attempt to move
        if (move_uploaded_file($_FILES['file1']['tmp_name'], $uploadfile)) {
            echo "File is valid, and was successfully uploaded.";
        }else{
            echo 'Error moving file.';
        }
    } else {
        // Has error
        $errors = array(0=>'OK',
                        1=>'UPLOAD_ERR_INI_SIZE',
                        2=>'UPLOAD_ERR_FORM_SIZE',
                        3=>'UPLOAD_ERR_PARTIAL',
                        6=>'UPLOAD_ERR_NO_TMP_DIR',
                        7=>'UPLOAD_ERR_CANT_WRITE',
                        8=>'UPLOAD_ERR_EXTENSION'
                        );
        echo "Error: ".$errors[$_FILES['file1']['error']];
    }
}

BTW. Be very wary of allowing uploads.

3
  • I tried the above and the same thing happened...the page does a post back and then nothing is displayed to say whether an error occurred...
    – whitwhoa
    Sep 18, 2012 at 2:25
  • "Be very wary of allowing uploads." So how would you design a site with user pictures? Upload seems full of issues.
    – dbasnett
    Sep 19, 2012 at 13:53
  • Well with pictures its simple you would check its a valid picture with a function like getimagesize(), deny cgi access to the upload directory & store it out if the web root and not to allow any file type upload like php. Interface the file with php with a passthrough script. Sep 19, 2012 at 15:24
1

You need to make sure you have permission to write to upload_tmp_dir, check post_max_size and upload_max_filesize limits in php.ini to be larger than the file you are trying to upload. Also $_FILES contains error information, dump that too to have more information about what isn't happening as it's expected to.

5
  • The values for post_max_size and upload_max_filesize are well within range, However upload_tmp_dir was commented out and it stated that the system default would be used if value not specified...not sure what the default is, but i set it to = On. Having an issue with dumping the error from $_FILES as I stated in the comment to the post below this one...hmmm...
    – whitwhoa
    Sep 18, 2012 at 2:30
  • upload_tmp_dir is a directory, setting it to On is almost certain not to work, try something like /tmp (on Linux) or c:\DirectoryPhpCanWriteIn (on Windows). The directory should already exist and the user php runs under should be allowed to write to it. Also for $_FILES just do var_dump($_FILES['file1']['error']); or you could just look at the whole thing var_dump($_FILES);
    – xception
    Sep 18, 2012 at 7:45
  • I fixed the php.ini file and insured that the /tmp directory has 777 permission...error persists. Also I did echo'd var_dump($_FILES); and it comes back with "array(0){}" like it never makes it that far??
    – whitwhoa
    Sep 19, 2012 at 1:55
  • Alright, so in my mind there is NOTHING wrong with this code...it should work, and it DOES work, but not on THIS server. I created an example on a completely different server with the exact code from above that I tested on the server where I'm getting the issue and it worked flawlessly...so the problem lies somewhere on the server...maybe in the php.ini file but I'm not sure what parameters could be causing this issue? Any suggestions?
    – whitwhoa
    Sep 19, 2012 at 3:24
  • Try mailing php developers or if they have an irc channel ask there.
    – xception
    Sep 19, 2012 at 4:04
0

I am not quite sure this works or not, have not try it out yet.

<?
$uploadpath = '../images/';

if ($_FILES["file1"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file1"]["error"] . "<br />";
    }

  else
    {
echo "Upload: " . $_FILES["file1"]["name"] . "<br />";
    echo "Type: " . $_FILES["file1"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file1"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file1"]["tmp_name"] . "<br />";
    }

    if (file_exists($uploadpath . $_FILES["file1"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      $uploadpath . $_FILES["file"]["name"]);
      echo "UPLOAD SUCCESS!";
      }

?>

Source from : W3school

4
  • I have a code I used for my current website development, tell me if the above solution doesn't work. I will give you my code, it is almost the same as the code above but my code consist of mysql queries
    – Furry
    Sep 18, 2012 at 2:42
  • Alright, I just tried the above code (fixed the syntax errors in the last else clause), and the problem still persists. Whenever I try to upload a postback occurs, the code is executed, $_FILES["file1"]["error"] equals 0, but none of the information comes back (content that should be next to "Upload: ", "Type: ", etc), and the file is not uploaded...this is getting ridiculous...ANY ideas???
    – whitwhoa
    Sep 19, 2012 at 2:49
  • Alright, so in my mind there is NOTHING wrong with this code...it should work, and it DOES work, but not on THIS server. I created an example on a completely different server with the exact code from above that I tested on the server where I'm getting the issue and it worked flawlessly...so the problem lies somewhere on the server...maybe in the php.ini file but I'm not sure what parameters could be causing this issue? Any suggestions?
    – whitwhoa
    Sep 19, 2012 at 3:23
  • Perhaps it maybe the cause of php.ini, if you're using xampp, you may overwrite it with my php.ini : uploadmb.com/dw.php?id=1348034469
    – Furry
    Sep 19, 2012 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.