Page 1 of 1

Problem writing file uploads.

Posted: Tue Nov 12, 2002 9:49 am
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?

Posted: Tue Nov 12, 2002 1:21 pm
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

Posted: Tue Nov 12, 2002 6:00 pm
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.

chdir

Posted: Tue Nov 12, 2002 6:58 pm
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

Posted: Tue Nov 12, 2002 9:39 pm
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. :?

Posted: Wed Nov 13, 2002 4:55 pm
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.