Jump to content

move_uploaded_file works for some files, but not for some others...


Recommended Posts

Howdy! I have been working in this custom script to manage some people on a group.. It will be really complete, with profiles, related stats and also an simple email client. But recently I have been having an issue with the function move_uploaded_file() to upload a picture to the user's profile.

 

Apparently it works for some files (which means the syntax and paths are correct), but for some others move_uploaded_file() returns FALSE and I just can't work it out.

 

Here is the part of the script that treats the uploading:

$target_path = "/opt/lampp/htdocs/emp/deeper/profiles/avt/"; //>>PATH<< to avt folder

if (!isAllowedAvatar($_FILES['picture']['name'])) { //Check for allowed filetypes
print "<div class='notif error bloc'><strong>Error :</strong> Wrong filetype. Image must be PNG file...</a></div>";
die;
}
if (file_exists($target_path.$basename.".png")) { //Rename old image to prevent loss of it in case of Failure
rename($target_path.$basename.".png",$target_path.$basename."_temp.png");
}

$old_path = $target_path;

$target_path = $target_path . $basename . ".png"; //Build new image path

if(move_uploaded_file($_FILES['picture']['tmp_name'], $target_path)) { //Moves uploaded image
print "File ".  basename($_FILES['picture']['name'])." correctly uploaded";
unlink($old_path.$basename."_temp.png"); //Deletes temp file
} else {
print "There was an error uploading the file, please try again!";
rename($old_path.$basename."_temp.png",$old_path.$basename.".png"); //Reverts the temp file to the original
}

 

I think the comments are enough for understanding it. Also, it looks like move_uploaded_file() is returning FALSE if the file is from Linux and had ownership stuff..

Any tip is welcome! Thanks for reading!

Find out the reason it is failing by displaying all the php detected errors. Add the following two lines of code immediately after the first opening <?php tag on the page -

 

ini_set("display_errors", "1");
error_reporting(-1);

Nope :/ No error messages appearing. It was interesting because when I refreshed the page it showed me a "PHP Strict" note that I shouldn't rely on the server's Timezone, so the setting worked.

But it stills, no error about move_uploaded_file()...

Ok, I feel a kind of stupid now, but the problem wasn't with PHP, but with the HTML form..

 

<input type='hidden' name='MAX_FILE_SIZE' value='10000' />

 

10Kb is too few for a picture ;) Now I have setted it to 1Mb and it works!

 

I hope it helps others too! Thanks anyway for your help :D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.