failed to open stream: Permission denied

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
tolearn
Forum Newbie
Posts: 9
Joined: Wed Nov 29, 2006 5:27 am

failed to open stream: Permission denied

Post by tolearn »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

Im trying to upload a document file in to a remote server. But im getting the following error. I tried changing the permission for the folder also.

It works fine when i tried in the local machine. What may be the problem

Warning: move_uploaded_file(/home/smartchu/public_html/website_content/test1_595446942/test.doc): failed to open stream: Permission denied in /home/smartchu/public_html/upload_test.php on line 26

Warning: move_uploaded_file(): Unable to move '/tmp/php23ki2U' to '/home/smartchu/public_html/website_content/test1_595446942/test.doc' in /home/smartchu/public_html/upload_test.php on line 26

Code: Select all

$id=$nam."_".rand();
$uploaddir="/home/smartchu/public_html/website_content/".$id."/";
$name1 = $_FILES["userfile1"]["name"];
$destination1 = $uploaddir.$name1;
$destination1 = "/home/smartchu/public_html/website_content/".$name1;
move_uploaded_file($_FILES['userfile1']['tmp_name'], $destination1);//line #26

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$id=$nam."_".rand();
$uploaddir="/home/smartchu/public_html/website_content/".$id."/";
A random directory name? What are the bets on this directory really exists?
move_uploaded_file() does not create (missing) directories.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

also touch() the file first, for some reason it helps half the time
tolearn
Forum Newbie
Posts: 9
Joined: Wed Nov 29, 2006 5:27 am

failed to open stream: Permission denied

Post by tolearn »

I tried without creating a directory also. I tried to upload the file directly . But getting the same error
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

And you new code is ....? Does it contain debug code like echo...file_exists...is_readable and so on? If not, why not?
tolearn
Forum Newbie
Posts: 9
Joined: Wed Nov 29, 2006 5:27 am

Post by tolearn »

I tried with file exists() it gives file not exists error.

But i tried the same code uploading in locan server and it works perfectly.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<?php
error_reporting(E_ALL); ini_set('display_errors', true);

function checkpath($p) {
	$path = '';
	echo '<fieldset><legend>', htmlentities($p), "</legend>\n";
	foreach( array_filter(explode('/', $p)) as $d) {
		$path .= '/'.$d;
		echo $path,' ',
			is_file($path) ? 'f':'-',
			is_dir($path) ? 'd':'-',
			is_readable($path) ? 'r':'-',
			is_writable($path) ? 'w':'-',
			is_executable($path) ? 'x':'-',
			"<br />\n";
	}
	echo "</fieldset>\n";
}

$id=$nam."_".rand();
$uploaddir="/home/smartchu/public_html/website_content/".$id."/";
$name1 = $_FILES["userfile1"]["name"];
$destination1 = $uploaddir.$name1;
$destination1 = "/home/smartchu/public_html/website_content/".$name1;

checkpath($_FILES['userfile1']['tmp_name']);
checkpath($destination1);
move_uploaded_file($_FILES['userfile1']['tmp_name'], $destination1);//line #26 

?>
What does it print?
tolearn
Forum Newbie
Posts: 9
Joined: Wed Nov 29, 2006 5:27 am

Post by tolearn »

I got the output as follows:

/tmp/php7AbDG5/tmp -drwx
/tmp/php7AbDG5 f-rw-
/home/smartchu/public_html/website_content/Taj_tender_request.doc/home -----
/home/smartchu -dr-x
/home/smartchu/public_html -drwx
/home/smartchu/public_html/website_content -dr--
/home/smartchu/public_html/website_content/Taj_tender_request.doc
Warning: is_file(): Stat failed for /home/smartchu/public_html/website_content/Taj_tender_request.doc (errno=13 - Permission denied) in /home/smartchu/public_html/dev_test.php on line 10
-
Warning: is_dir(): Stat failed for /home/smartchu/public_html/website_content/Taj_tender_request.doc (errno=13 - Permission denied) in /home/smartchu/public_html/dev_test.php on line 11
----
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

tolearn wrote:/home/smartchu/public_html/website_content -dr--
Your script is not allowed to change to or write to the directory /home/smartchu/public_html/website_content thus it cannot create /home/smartchu/public_html/website_content/Taj_tender_request.doc

perhaps the output of

Code: Select all

<?php
$cmds = array(
		'who am i',
		'whoami',
		'groups',
		'ls -lad  /home/smartchu/public_html/website_content/'
	);
foreach($cmds as $command) {
	echo '<div>', $command, ': ';
	system($command);
	echo "</div>\n";
}
can shed some light on the problem.
tolearn
Forum Newbie
Posts: 9
Joined: Wed Nov 29, 2006 5:27 am

Post by tolearn »

Getting output lke this

who am i:
whoami: nobody
groups: nobody
ls -lad /home/smartchu/public_html/website_content/: drwxr--r-- 2 smartchu smartchu 4096 Feb 27 04:01 /home/smartchu/public_html/website_content/
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

drwxr--r-- 2 smartchu smartchu 4096 Feb 27 04:01 /home/smartchu/public_html/website_content/
the d at the beginning means it's a directory. The next three, green characters mean the owner has read,write,execute permissons. The blue section shows the permissions for the group: read-only. And the red section shows the permissions granted to everybody else (not the owner and not in "owning" group)
The next green text is the name of the owner: smartchu. And there's also a group called smartchu and it's the group associated with this directory.
You script runs as nobody which is only member of the group nobody. So neither the owner nor the group permissions apply when accessing the directory /home/smartchu/public_html/website_content/
see also: http://www.google.de/search?hl=en&q=uni ... tnG=Search
Post Reply