File uploading script to /var/www/html -- SOLVED

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
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

File uploading script to /var/www/html -- SOLVED

Post by califdon »

I have a short script to upload one file at a time to a web document directory, using the following syntax:
move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)
$target is a path within the /var/www/html/... part of the file system. I have made sure that the target directory has a file mode of drw-rw-rw- and I thought that I should be able to write the file there, since it's writable by the world (I know, it sounds dangerous, but I don't think it is for this special application) but I get:
Warning: move_uploaded_file(/var/www/html/uploads/horiz2.gif) [function.move-uploaded-file]: failed to open stream: Permission denied
I had previously tried locating the upload directory outside the Apache document root, but I got the same error when I did that.

Maybe I'm misinterpreting the error message. Anyone see what's happening?

TIA,

Don
Last edited by califdon on Sat Nov 11, 2006 12:29 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please try

Code: Select all

<?php
function path_info($path) {
	echo "<pre>\npath: ", $path, "\n";
	foreach( array('file_exists', 'is_file', 'is_dir', 'is_readable', 'is_writable', 'is_executable') as $fn ) {
		echo "$fn($path): ", $fn($path) ? 'true':'false', "\n";
	}
	echo "</pre>\n";
}


$path = '';
foreach (explode('/', 'var/www/html/uploads/horiz2.gif') as $p) {
	$path .= '/'.$p;
	path_info($path);
}
?>
and post the output.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Thanks for the very helpful snippet. Here's the output:

Code: Select all

path: /var
file_exists(/var): true
is_file(/var): false
is_dir(/var): true
is_readable(/var): true
is_writable(/var): false
is_executable(/var): true

path: /var/www
file_exists(/var/www): true
is_file(/var/www): false
is_dir(/var/www): true
is_readable(/var/www): true
is_writable(/var/www): false
is_executable(/var/www): true

path: /var/www/html
file_exists(/var/www/html): true
is_file(/var/www/html): false
is_dir(/var/www/html): true
is_readable(/var/www/html): true
is_writable(/var/www/html): false
is_executable(/var/www/html): true

path: /var/www/html/uploads
file_exists(/var/www/html/uploads): true
is_file(/var/www/html/uploads): false
is_dir(/var/www/html/uploads): true
is_readable(/var/www/html/uploads): true
is_writable(/var/www/html/uploads): true
is_executable(/var/www/html/uploads): false

path: /var/www/html/uploads/horiz2.gif
file_exists(/var/www/html/uploads/horiz2.gif): false
is_file(/var/www/html/uploads/horiz2.gif): false
is_dir(/var/www/html/uploads/horiz2.gif): false
is_readable(/var/www/html/uploads/horiz2.gif): false
is_writable(/var/www/html/uploads/horiz2.gif): false
is_executable(/var/www/html/uploads/horiz2.gif): false
If I understand that correctly, the directory I want to upload to (uploads) is writable, but the parent directories are not. I don't think I want to make the whole /var path writable (!), so what options do I have??

Don
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Your script is not allowed to "enter" the directory /var/www/html/uploads
is_executable(/var/www/html/uploads): false
try

Code: Select all

chmod ugo+x /var/www/html/uploads
to give every- and anyone access to that directory.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

SOLVED!

Post by califdon »

Danke sehr, Volka! I was concentrating on the write permission and didn't realize that it has to be executable also!

Don
Post Reply