move_uploaded_file returning false but still moving the files


I have a script running which allows you to upload. I grab that image and I create 3 different sizes. What I do then is move the files into a directory to hold them all in there.

The problem I’m having is that move_uploaded_file() is returning false but it’s still moving them into the correct directory. I’m not sure why it’s doing that?

if(move_uploaded_file($strRegularFileName, $strRegularFile) &&
move_uploaded_file($strMediumFileName, $strMediumFile) &&
move_uploaded_file($strSmallFileName, $strSmallFile))

If I leave it to just check the first move_uploaded_file it returns true, but the second I add two or more, it returns false.

I’ve included the code for both variables strSmallFileName and strSmallFile.

$now = time();
  while(file_exists($strSmallPic = str_replace($_FILES['file']['name'], $now.$strExtension, '../images/users/.'$_FILES['file']['name']))) {
  $strSmallFileName = $now.$strExtension;
  $strSmallFile = $strSmallPic;

The variable strExtension is from an earlier validation I had done, which adds jpg, png or gif extension.

I’ve looked at the manual and the only explanation I could find is that every example always references the tmp_name array. But if that was the case, then just doing:

if(move_uploaded_file($strRegularFileName, $strRegularFile))

Should return false, but it’s not.

If anyone could shed some light, thanks :slight_smile:

The function moves the file. After the first time it is moved, it is no longer located at the original location.

Use is_uploaded_file() to test, and then use copy() three times, optionally followed by unlink()

Right on.

What would I use unlink function for? To delete the tmp file?


yes, unlink deletes the file.


that’s simple
you should not use this function for not uploaded files.
use rename() instead

I’m having another issue.

The uploader I built is not allowing files over 200KBs or so.

I’ve checked the php.ini and this is what it shows:

upload_max_filesize = 10M
post_max_size = 10M

And I’m checking for the file size to be under 5MB

if ($_FILES['file']['size'] > 5242880)
  echo 'File too big';

As well as

if ($_FILES['file']['error'] != 0)
  echo 'There was an error in your upload';

What I’m noticing is the progress bar in my browser is moving up and when it’s done nothing happens. No error message or anything.

What could it be?

echo $_FILES['file']['error'];

What error is displayed if any?

There is non so when I echo it, it returns 0.

Do you have the display of php error messages enabled in php.ini ?

Yes its turned on.

This is odd, I did the following:

echo '<script type="text/javascript">alert("Before");</script>';
  if ($_FILES['file']['type'] == 'image/jpeg') {
    echo '<script type="text/javascript">alert("jpg");</script>';
    $strExtension = '.jpg';
    $src = imagecreatefromjpeg($_FILES['file']['tmp_name']);
  elseif ($_FILES['file']['type'] == 'image/png') {
    echo '<script type="text/javascript">alert("png");</script>';
    $strExtension = '.png';
    $src = imagecreatefrompng($_FILES['file']['tmp_name']);
  else {
    echo '<script type="text/javascript">alert("gif");</script>';
    $strExtension = '.gif';
    $src = imagecreatefromgif($_FILES['file']['tmp_name']);
  echo '<script type="text/javascript">alert("After");</script>';

Out of the 5 echoes that are possible I got “Before” and the echo for the file type “jpg”. The script never reaches “After” echo.


If it helps, here are some Common File Upload Pitfalls

They include:

[list][]The MAX_FILE_SIZE item cannot specify a file size greater than the file size that has been set in the upload_max_filesize in the php.ini file. The default is 2 megabytes.
]If a memory limit is enabled, a larger memory_limit may be needed. Make sure you set memory_limit large enough.
[*]If max_execution_time is set too small, script execution may be exceeded by the value. Make sure you set max_execution_time large enough.[/list]

For more see

That seems to have done the trick.


Argh. This is so weird its still having problem uploading certain files.

I tried a 5mb file and it uploaded correct. Then I tried a 3.2MB and it just stalled out, no error message, no redirection. The browser progress bar reached 100% and nothing happened.

Then I tried a 2.0MB and it worked fine as well as a 4.3MB picture. In addition, I tried a 1.1MB picture and it stalled out too!


There are many web sites out there that provide assistance regarding this.

For example: Uploading large(big) files in PHP using .htaccess

Thanks for the article I’ve already done that through ini_set() and it’s still giving the same problems.

Are you sure you have set display_errors and error_reporting to an appropriate level? Intentionally create an error, or use ini_get() to double check.


If I add an extra semi-colon it displays an error.

Have you mentioned yet where the trouble is occurring? Is it a server controlled by someone else? Do you have control over the server? What versions of apache and php is it running? Are you dealing with windows or linux? Is php running as suPHP or is it running as a restricted nobody account?

Things like that which might help someone here to help you.

I run a dedicated server so I full access to the server, which runs on Linux.

PHP version 5.2.6 and I’m not running suPHP.

Apache 2.2.3

Thanks for that. This issue you’re facing goes beyond what I can help with at this stage, but the information you have provided should allow someone else here to help you further.