Problem writing file uploads.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Problem writing file uploads.

Post by Bill H »

This is the script recording the file:

Code: Select all

<?php
if (is_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name']))
&#123;
   $DestDir = "/data/web/72590/www/utility/images/";
   if (@move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name'], $DestDir) == FALSE)
        echo "File write failed: " . $_FILES&#1111;'userfile']&#1111;'name'];
   else echo "Uploaded file: " . $_FILES&#1111;'userfile']&#1111;'name'];
&#125;
else
&#123;
   echo "File upload failed: " . $_FILES&#1111;'userfile']&#1111;'name'];
&#125;
?>
The server is unix, PHP 4.2.2, Apache/1.3.26
The server root is "/data"
My website root is "/data/web/72590/www"
The script is located in "/data/web/72590/www/utility"
The "images" subdirectory of "utility" has been CHMOD to 777.
It echos "File write failed: image.gif" no matter what $DestDir I use.
I have tried:

Code: Select all

$DestDir = "/data/web/72590/www/utility/images/";
$DestDir = "images/";
$DestDir = "./utility/images/";
$DestDir = "./web/72590/www/utility/images/";
Any help with what I'm doing wrong?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

For debugging I'd probably remove the @ from in front of move_uploaded_file() to make sure that no error is being thrown there that the @ is suppressing.

Mac
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Yeah, good point. Usually I don't want the warnings because if it fails I don't particularly care why and I hanndle the outcome myself.

Anyway I removed the @ and got two errors:
Warning: Unable to create './web/72590/www/utility/images/': No such file or directory in /data/web/72590/www/utility/filerec.php on line 14
and
Warning: Unable to move '/tmp/phpWde3xL' to './web/72590/www/utility/images/' in /data/web/72590/www/utility/filerec.php on line 14
The file does not exist, of course. The directory does.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

chdir

Post by phpScott »

you are getting this error
Warning: Unable to create './web/72590/www/utility/images/': No such file or directory in /data/web/72590/www/utility/filerec.php on line 14
because php is not currently pointing to the directory you want to upload the page to.
Try

Code: Select all

&lt;?php
$DestDir = "/data/web/72590/www/utility/images/";
chdir($DestDir);
?&gt;
in your code chdir will change where php is pointing to and probaly fix the first error, which will more than likely solve the second.

http://www.php.net/manual/en/function.chdir.php
for more info on chdir() and other directory/file functions
You might also want to check to see if the dir exists first by using file_exists($DestDir) as well

phpScott
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Thanks for the input. I am willing to try that, but.....

It is not consistent with the php documentation, which doesn't say anything about needing to set the unix current directory. Plus if the current directory is what is controlling the write, then what is the second function parameter for?

Also, what would changing the current directory do to all the other function calls on the site? I have a feeling it would cause them to fail, since the calls (and includes and links) are filenames only and/or relative url's.

Not trying to be argumentative, just trying to understand. :?
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Embarrassing to answer my own question, but:

The second parameter is actually the file name, not just the path.
So

Code: Select all

$DestDir = "/data/web/72590/www/utility/images/";
should be

Code: Select all

$DestDir = "/data/web/72590/www/utility/images/" . $_FILES&#1111;'userfile']&#1111;'name'];
Works now.
Post Reply