Reputation: 2214
Can someone help me understand why this is returning false?
if ((move_uploaded_file($_FILES[$field]['tmp_name'], $path))) {
As in, potential causes. I have checked the variables are correct and I have checked permissions on the destination folder which is 777.
Cheers!
For those wanting to see var_dump ($_FILES);
array(1) { ["image"]=> array(5) { ["name"]=> string(14) "accountile.png"
["type"]=> string(9) "image/png" ["tmp_name"]=> string(14) "/tmp/php28IQhv"
["error"]=> int(0) ["size"]=> int(394) } }
Upvotes: 12
Views: 29151
Reputation: 546
I was facing the same problem.
If your error says "Failed to open stream: Permission Denied" it means the PHP Server is not being capable of creating the new file inside your destination directory.
Once you have set the Linux permissions on the directory (wich sounds like you did by making it 777) you should give that special permission to the PHP Server.
If your folder is named "uploads", you should cd to the previous directory and use the next command:
chcon -R -t httpd_sys_rw_content_t uploads
That definetly solved my problem.
Hope it helps.
Upvotes: 0
Reputation: 5406
Check:
enctype="multipart/form-data"
Inside the form itself, without it file upload won't work at all.
Upvotes: 1
Reputation: 9
Loïc Février, thank you, you've saved me a lot of time!
Here is my part... printing possible errors during upload.
First create directory Uploader/UploadedFiles Then use code below...
$destination=$_SERVER[DOCUMENT_ROOT]."/Uploader/UploadedFiles/" . $_FILES["file"]["name"];
if(move_uploaded_file($_FILES["file"]["tmp_name"], $destination)){
echo ("Stored in".$_SERVER[DOCUMENT_ROOT]."/Uploader/UploadedFiles/".$_FILES["file"]["name"]);
}else{
$html_body = '<h1>File upload error!</h1>';
switch ($_FILES[0]['error']) {
case 1:
$html_body .= 'The file is bigger than this PHP installation allows';
break;
case 2:
$html_body .= 'The file is bigger than this form allows';
break;
case 3:
$html_body .= 'Only part of the file was uploaded';
break;
case 4:
$html_body .= 'No file was uploaded';
break;
default:
$html_body .= 'unknown errror';
}
echo ($html_body);
}
Upvotes: 0
Reputation: 970
Did you edit your php.ini to make sure upload_tmp_dir points to a temporary directory?
Upvotes: 1
Reputation: 157839
I have checked the variables
Do not check variables but check error messages.
It's the only thing you need.
Add these lines at the top of your code
ini_set('display_errors',1);
error_reporting(E_ALL);
and see what it says.
If move_uploaded_file failed, it will always raise an error with detailed explanation.
You won't believe it, but reading error messages is way more efficient way to find a problem than guesswork you tried before
I can't believe noone mentioned it already.
Upvotes: 26
Reputation: 7750
Don't use
$path = "http://www.barbadostravelbuddy.co.uk/demo/images/carhire
/accountile10420103260403000000pm.png"
but
$path = "/home/sites/barbadostravelbuddy.co.uk/public_html/demo/images/carhire/
accountile10420103260403000000pm.png"
It needs to be a path on the system, not an URL.
Upvotes: 1
Reputation: 449395
Basic debugging steps:
print_r($_FILES)
look like? $path
look like? My guess is that $path
is only a path to a folder, not to a full file name.
Update: You need to specify a filesystem path as $path.
Upvotes: 0
Reputation: 62356
With move_uploaded_file
you don't need 777 permissions. What is the output of $path
? Have you verified that $path
exists? Have you verified that $field
exists?
Either $field or $path don't exist, or open_basedir is in effect is my guess.
Is open_basedir
restriction enabled? That could prevent the destination of the uploaded file from being written to. Look in your php.ini for open_basedir
, if there's a path specified then it is enabled and you want to see if the destination path of your uploaded file is within this path. If it is, that's why it's failing.
update
$path cannot be a URL, it must be a local path such as /home/user/public_html/
Upvotes: 1
Reputation: 11215
Did you check permission on the temporary upload folder?
What does php tell you if you do:
var_dump($_FILES);
Upvotes: 0