tags:

views:

65

answers:

5

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) } }
A: 

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/

Webnet
$path and $field return expected strings. I can post the most recent output if it'll help.
YsoL8
@Yoi you are aware that $path needs to be a file name, not a path, right?
Pekka
open_basedir has no value in phpinfo
YsoL8
$path = http://www.barbadostravelbuddy.co.uk/demo/images/carhire/accountile10420103260403000000pm.png most recently
YsoL8
A: 

Basic debugging steps:

  • What does a print_r($_FILES) look like?
  • Does the source file actually exist?
  • Is the "error" flag of the file upload zero (will be visible when doing the print_r)
  • What does the target $path look like?
  • Are you specifying a full file name in the target path?

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.

Pekka
All of those are as they should be (I assume you mean an absolute target path)
YsoL8
@YsoL8 `$path` needs to be a full file name that doesn't exist yet, not only a path to a directory.
Pekka
$path = http://www.barbadostravelbuddy.co.uk/demo/images/carhire/accountile10420103260403000000pm.png most recently
YsoL8
@YsoL8 that won't work. It needs to be a filesystem path
Pekka
A: 

Did you check permission on the temporary upload folder?

What does php tell you if you do:

var_dump($_FILES);
ITroubs
A: 

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.

Loïc Février
I tried /public_html/demo/images/carhire which hasn't worked. My directory struture is root > public_html > demo > images > carhire
YsoL8
Ok. Try to do a mkdir (http://php.net/manual/en/function.mkdir.php) in that directory. That will check that the directory is correct and that you have rights on it.
Loïc Février
Sorry but that hasn't worked
YsoL8
Then you either don't have the right or your path is not correct. Can you do a phpinfo() and give us the `_SERVER["DOCUMENT_ROOT"]` ? Can you give us the permissions the directory has ?
Loïc Février
document_root is /home/sites/barbadostravelbuddy.co.uk/public_html/ and permission is 755
YsoL8
Then why did you tell "My directory struture is root > public_html > demo > images > carhire" ? It's not... Answer updated with correct path.
Loïc Février
A: 

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.

Col. Shrapnel
Good to know, I've never had problems with move_uploaded_file so I've never needed to know.
Webnet