PHP upload permission issue

ive right guys had a problem with file permissions with the following download form. the text file is transferred to upload / dir by global users.

mysite$ ls -l drwxrwxrwx 2 user user 4096 2010-09-24 13:07 upload 

but since I am not registered as root, the new file uploaded to the domain is saved in upload / dir with limited permissions and cannot be changed. eg.

 upload$ ls -l -rw-r--r-- 1 www-data www-data 3067 2010-09-24 13:07 Readme.txt 

this problem is obviously the same for all files added to the download folder by global users. after downloading the file, I need a way to change the rights to the file without introducing the root password in the php script running in the domain. please, help!

Is there a way to associate the same rights with files as the contained folder when adding new files?

send form:

 <html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> 

upload_file.php:

 <?php if ($_FILES["file"]["type"] == "text/plain") { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("/home/user/mysite/upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "/home/user/mysite/upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> 
+4
source share
4 answers

The user who originally writes the files to your / upload directory is the one who launched the Apache instance, which runs the PHP module.

In other words, PHP is the "owner" of all downloaded files, and through a PHP script you can change the permissions of all the corresponding downloaded files without providing any credentials at all:

PHP chmod function

Quick and dirty hack files that are writable by all users will

 else { move_uploaded_file($_FILES["file"]["tmp_name"], $f="/home/user/mysite/upload/" . $_FILES["file"]["name"]); chmod($f, 0777); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } 
+5
source

move_uploaded_file uses umask (600). Use copy($source, $dest) instead of moving.

+2
source

You can use chmod to change file permissions.

To get file permissions, use fileperms .

+1
source

When you start a local server on Linux with xampp or another way, there is a very limited limitation. Since the local host created in the root directory, for example, xampp, is located in (/ opt / lampp), if you want to remove all restrictions, insert the lower code in the terminal

 sudo chmod 777 /opt 

after that

 sudo chmod 777 /opt/* 

after that

 sudo chmod 777 /opt/*/* 

after that

 sudo chmod 777 /opt/*/*/* 

and keep adding (/ *) until you get an error

good luck

-3
source

All Articles