8D 8D 8D
Sometime ago, I mentioned that I was looking for a way to upload files to a web server through an HTML without setting the directory I’m uploading to, to 777 or 757 or without using an FTP client to change it manually. After much searching and picking up on leads (PHP run as CGI could accept uploads to a directory set to 755 and this topic I posted at my host’s forums). Somewhere in all this, I figured one thing out: only the owner or group can change the permissions of a directory, and the owner would be the one who created the directory. Since I couldn’t change owner or group (who happen to be the same person in my case), I would have to create the directory the files would be uploaded to through a PHP script, so I could change permissions through a PHP script. So it was kind of like this:
- Set the parent directory (where the uploads directory will reside) to 777 temporarily.
- Upload script with this:
< ?php mkdir('uploads', 0755); ?>(According to this comment, it might only work in the parent directory.)
- Chmod the parent directory back to 755.
- In the script where the file upload processing takes place, I have something like this:
< ?php // chmod the directory to 777 chmod($upload_dir, 0777); //file processing code, also where you move it to the directory that was just created // because of security issues, it's not a good idea to leave a directory with the permissions as 777 chmod($upload_dir, 0755); ?>
(There’s probably a more more streamlined way of doing this, but this is the only way I know of, that would for me.)
The catch is that I wouldn’t be able to do anything to this files or to the upload directory via an FTP client, since I would be a different user from the web server. But if I wanted to delete them or do anything with them, I would only have to come up with a PHP script and upload it. :3
Modified: July 07th, 2008








You have discovered something that is more Linux than PHP. Yes, only the owner of a file has permission on it. OR!!! If you are logged into root. Then you have full permission over everything. This is why in school I keep the root password something other than what our professor told us to make it simply because I only want myself to be able to log into root in the computer lab.
chmod is not a PHP function. It’s literally the command a person uses when running in linux to change the permissions on a file. You have discovered the command for making a directory. Some other useful ones would be
rm - remove a file
rmdir - remove an empty directory
there is something you can add to rm to recursively remove a non-empty directory. I can’t quite remember what it is. It’s either rm -d or rm -r or I have no idea.
cp - copy a file
mv - move a file
I love your problem solving skills! I probably would have gotten to realizing that apache can’t do what I want if it doesn’t own the dir, and given up! Probably would have taken my husband pointing out the solution!
@Kimmie: Veerrrry interesting… I probably should’ve thought about the Linux part, since I don’t know if the web site I’m doing this for uses Linux or Windows. If some of the server info on the current page mentions something about Unix, does this mean it runs on Linux? And thanks for the info!
@Michelle: Thanks.
Yes, it’s running a Unix environment, which is most likely Linux. Linux servers are much cheaper than Windows ones. One clue that you are on a Linux server is PHP. When you install the Unix OS, PHP gets installed. But yeah, for future reference, if you ever need to move to a new server, you most likely want to be on a Linux/Unix one.
All righty. Thanks!
(I guess I should tell the owner that…)