Eugene
Eugene

Reputation: 11280

Can't find tmp folder in php. (Ubuntu 10.10, apache2)

I have a form, which uploads an image. I try to get it by $_FILES:

$filename = $_FILES['screenshot']['name'];
$source = $_FILES['screenshot']['tmp_name']."/".$filename;
$target = GL_UPLOADPATH.$filename;

echo "TEST0";        

if (move_uploaded_file($source, $target)) {
    //connect to DB and so on, what I need

    echo "TEST1";
}

So I get echoed TEST0 but don't get echoed TEST1. If I echo every variable - it's normal. I see my $target - it's something like /tmp/phpoLqYdj/test2.jpg

So, I think PHP can't move_uploaded_file because it can't find /tmp/phpoLqYdj/test2.jpg

But where is /tmp/phpoLqYdj/? I am testing on localhost. My document root is /var/www/. PHP has default settings in php.ini (upload_tmp_dir is commented in php.ini).

In my /tmp/ folder (in system) I don't have such folder like php***. In /var/tmp/ either.

(Ubuntu 10.10, LAMP was installed by "tasksel")

Upvotes: 1

Views: 3195

Answers (2)

Ted Kulp
Ted Kulp

Reputation: 1433

When you uploads files via PHP, it stores them in as a tmp file that's not named anything related to the filename. Appending $filename to $_FILES['screenshot']['tmp_name'] is the incorrect way to handle it... $_FILES['screenshot']['tmp_name'] IS the file.

Also, the tmp file is removed at the end of the request, so you'll never have a chance to actually see it in the file manager. Either you move it in the same request, or it's gone. It's all done in the name of security.

Anyway, just use this instead and you'll be good.

$filename = $_FILES['screenshot']['name'];
$source = $_FILES['screenshot']['tmp_name'];
$target = GL_UPLOADPATH.$filename;

echo "TEST0";        

if (move_uploaded_file($source, $target)) {
    //connect to DB and so on, what I need

    echo "TEST1";
}

Upvotes: 1

TheDeveloperAcadmey
TheDeveloperAcadmey

Reputation: 11

I had the exact same problem. What I did to fix it was change the permissions of folder that you are uploading to, to read and write for all.

Upvotes: 1

Related Questions