Edward Hasted
Edward Hasted

Reputation: 3433

PHP's move_uploaded_file advice

I'm having real problems with the PHP move_uploaded_file command to work properly on a shared Apasche web hosting server.

If I want to create a folder (mkdir) I have to use the full path name, e.g.

$target_path = "/home/myhostingname/public_html/uploads/files/".$lastID;
mkdir($target_path, 0755);

That works, the unique folder is created and using FileZilla I can upload files to it.

However when I try and use the full path with the move_uploaded_file command nothing ever gets uploaded. E.g.

move_uploaded_file($tmp_file, $target_path);

where, e.g. :

$tmp_file = "/home/myhostingname/public_html/tmp/php8MR5Qv/test.gif"
$target_path = "/home/myhostingname/public_html/uploads/files/130/"

Any ideas what I'm doing wrong. The script is accepted but there would appear to be a mismatch going on...

Upvotes: 2

Views: 527

Answers (2)

Edward Hasted
Edward Hasted

Reputation: 3433

The solution appears to be that the $target_path is actually a file. The documentation doesn't make that particularly clear. However when you start finding code that works the desitination is a path and filename... When I made this change it worked immediately.

Upvotes: 0

Alexander Larikov
Alexander Larikov

Reputation: 2339

File from $tmp_file should be uploaded via HTTP POST request, otherwise move_uploaded_file will not work.

To check if file has been uploaded use is_uploaded_file

So your code might look like this:

$tmp_file = "/home/myhostingname/public_html/tmp/php8MR5Qv/test.gif"
$target_path = "/home/myhostingname/public_html/uploads/files/".$lastID;
if(is_uploaded_file($tmp_file)){
    mkdir($target_path, 0755);
    move_uploaded_file($tmp_file, $target_path);
}

Upvotes: 2

Related Questions