PROBLEM CHANGING FILE PERMISSIONS

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

nirma78
Forum Commoner
Posts: 42
Joined: Wed Sep 17, 2003 2:02 pm

PROBLEM CHANGING FILE PERMISSIONS

Post by nirma78 »

Hi !

I am trying to upload a file. It uploads it fine, but the problem is when I try to open the file it says "Access Denied".

I tried to change the permissions for the file but it gives me error.

Please help.

Thanks in advance


My Code :

Code: Select all

<?php

$uploaddir = "check/" . $_FILES['userfile']['name'];
chmod($uploaddir,644);

$up = move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir);
//var_dump($up);

if($up)
{
 echo "File uploaded";
}
else
{
  echo "Not uploaded";
}
?>
?>
User avatar
Leviathan
Forum Commoner
Posts: 36
Joined: Tue Sep 23, 2003 7:00 pm
Location: Waterloo, ON (Currently in Vancouver, BC)

Post by Leviathan »

Would a chmod to 644 on the uploaded file help?
nirma78
Forum Commoner
Posts: 42
Joined: Wed Sep 17, 2003 2:02 pm

Post by nirma78 »

Well my system administrator asked me to change it to 644.

If not that , then what do you think will help.

Thanks for your reply.
nirma78
Forum Commoner
Posts: 42
Joined: Wed Sep 17, 2003 2:02 pm

Post by nirma78 »

I tried changing it from 644 to 777, but still the same error !!

Please help

Thanks in advance
User avatar
Leviathan
Forum Commoner
Posts: 36
Joined: Tue Sep 23, 2003 7:00 pm
Location: Waterloo, ON (Currently in Vancouver, BC)

Post by Leviathan »

Correct me if I'm wrong, but aren't you chmodding the directory that you're putting the file in, not the file itself?
nirma78
Forum Commoner
Posts: 42
Joined: Wed Sep 17, 2003 2:02 pm

Post by nirma78 »

Thanks for your reply.

I think its for the file I think, I mean its like

Code: Select all

<?php
$uploaddir = "check/" . $_FILES['userfile']['name'];
chmod($uploaddir,777);
?>
and $_FILES['userfile']['name'] gives me the name of the file that I am uploading and I want to name it the same where it is getting uploaded .. so isnt it chmodding the file ??

I am not sure

Thanks
Judas
Forum Commoner
Posts: 67
Joined: Tue Jun 10, 2003 3:34 pm
Location: Netherlands

Post by Judas »

b.t.w.
does your script have the right permissions set for this job.
and what kind of os the server is running on.

so...

check chmod on dest. location, exec. script file.

learn more about telnet...
nirma78
Forum Commoner
Posts: 42
Joined: Wed Sep 17, 2003 2:02 pm

Post by nirma78 »

you asked "does your script have the right permissions set for this job."

what does this exactly mean - i mean I m not able to understand permissions for what ?

I do have write permissions on the server
I am changing the permissions for the file I am uploading

what else do I have to know for this

and could you please explain what do you mean when you have written "check chmod on dest. location, exec. script file.
"
I couldnt understand
User avatar
Leviathan
Forum Commoner
Posts: 36
Joined: Tue Sep 23, 2003 7:00 pm
Location: Waterloo, ON (Currently in Vancouver, BC)

Post by Leviathan »

Look again where and when you're calling chmod. You create a variable called $uploaddir, which, at least to me, signifies a directory. If you mean it to be a file, I'd change the name so that you don't confuse yourself. Then, you try to upload the file. What you don't do *after* you upload the file is set the permission on the file.

I'm guessing that $up is set, so the script echoes "File uploaded" but you can't get access to the file? If so, put the chmod statement in after the call to move_uploaded_file.
nirma78
Forum Commoner
Posts: 42
Joined: Wed Sep 17, 2003 2:02 pm

Post by nirma78 »

Thanks for your reply.

Well this is my code :

Code: Select all

<?php
$name = $_FILES['userfile']['name'];

$uploaddir = "check/" . $_FILES['userfile']['name'];
chmod($uploaddir,777);

$up = move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir);
chmod($uploaddir,777);
//chmod($name,0644);
//var_dump($up);

if($up)
{
}
else
{
  echo "Not uploaded";
}
?>
?>
so now tell me if you are telling to chmod for the file how do I do that, as in how will get only the file name..
I tried chmodding after move_uploaded_file() but it gives me a warning message : chmod(): No such file or directory in /home/httpd/htdocs/gbcb/upload_app_files.php on line 16

Please help

Thanks again
User avatar
Leviathan
Forum Commoner
Posts: 36
Joined: Tue Sep 23, 2003 7:00 pm
Location: Waterloo, ON (Currently in Vancouver, BC)

Post by Leviathan »

Right; you can't just chmod the dir, but the dir and filename together. So:

Code: Select all

<?php
chmod($uploaddir.$_FILES['userfile']['tmp_name'],644);
?>
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Something to keep in mind, this is at least true for me..

If you are using an upload script, the file you upload is created (under Linux/Unix) as the owner that the web server runs under. And if you are a different user on the system, then you are not the owner of the uploaded file, even if it is in your home directory.

Ie. If Apache runs under user nobody in group nobody, then PHP will create an uploaded file with the owner nobody and group nobody. You see. Same thing if your web servers runs under user httpd or whatever user/group it is. PHP will create files that way too.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Moved to PHP - Normal, it's not a database question.

Mac
Judas
Forum Commoner
Posts: 67
Joined: Tue Jun 10, 2003 3:34 pm
Location: Netherlands

Post by Judas »

what kind of OS ? (Unix,Windows,Sol....) ???
what kind off permissions does the executing script file has ???
(the executing script file is the file where from your script is running)

when the script file hasn't got the right permissions set . it can not do the job.

web/sample.php

Code: Select all

<?php 

$uploaddir = "check/" . $_FILES&#1111;'userfile']&#1111;'name']; 
chmod($uploaddir,0644); 

$up = move_uploaded_file($_FILES&#1111;'userfile']&#1111;'tmp_name'], $uploaddir); 
//var_dump($up); 

if($up) 
&#123; 
echo "File uploaded"; 
&#125; 
else 
&#123; 
  echo "Not uploaded"; 
&#125; 
?>
:!: note the added "0" in the chmod mode string

  • telnet or ftp to "web/"
    and then chmod the map with "chmod 0777 web" (telnet syntax)

    the location of sample.php
    and then chmod the script file with "chmod 0777 sample.php" (telnet syntax)
this file now has permissions to do what ever.




Dude just chmod the script file with a ftp prog or telnet OK. pfff
:|
nirma78
Forum Commoner
Posts: 42
Joined: Wed Sep 17, 2003 2:02 pm

Post by nirma78 »

Thank you all of you for your replies.

Well this is where I am now :

Levithian :

Code: Select all

<?php
<?php 
chmod($uploaddir.$_FILES['userfile']['tmp_name'],644); 
?> 
?>
I tried doing it but it still gives me the error !!
Warning: chmod(): No such file or directory in /home/httpd/htdocs/gbcb/upload_app_files.php on line 16


Pyrite : I think what you told might be happening with me. The file is not owned by me,the owner is the server where it is running the file. So do you think that might be the problem here since it doesnt allow me to change permissions on some other's owned files ??


Judas :
Well I am on Windows and the server is on Unix. ie I am coding from windows and running on unix server.

also I tried doing :

Code: Select all

<?php
chmod($uploaddir,0644); 
?>
but still not working. now for doing ftp or telnet is it required that I should be the owner of the file, coz in my case the owner of the file is apache, the server where it is running the file.


PLEASE HELP !!
Thanks in advance.
Post Reply