Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change permissions of file uploaded by PHP

Tags:

php

apache

I have created a small scale CMS for a website I am working on and have a form that uploads image files to be used on the website. It uploads the files successfully but the permissions it sets do not allow the file to be viewed in a browser.

Here is my current PHP code to upload the files

$typepath = $_POST['filetype'];

$target_path = "../../images/uploads/".$typepath."/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "<p>The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded</p>\n<p>To the directory:  <span style=\"font-weight:bold;\">".substr($target_path, 6)."</span></p>";
} else{
    echo "There was an error uploading the file, please try again!";
}
like image 608
Max Avatar asked Jan 17 '11 15:01

Max


People also ask

How do I change folder permissions in PHP?

chmod("/somedir/somefile", 0750);

What is the use of chmod 777?

The command chmod -R 777 / makes every single file on the system under / (root) have rwxrwxrwx permissions. This is equivalent to allowing ALL users read/write/execute permissions.

What permissions should PHP files have?

Set php files to 640. For maximum security you should set minimum permissions, which is 640. The owner 6 would be the one uploading the files.


1 Answers

PHP Manaual chmod http://php.net/manual/en/function.chmod.php

chmod("/somedir/somefile", 0755);

In context;

$typepath = $_POST['filetype'];

$target_path = "../../images/uploads/".$typepath."/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    chmod($target_path, 0755);
    echo "<p>The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded</p>\n<p>To the directory:  <span style=\"font-weight:bold;\">".substr($target_path, 6)."</span></p>";
} else{
    echo "There was an error uploading the file, please try again!";
}
like image 105
Matt Lowden Avatar answered Sep 19 '22 16:09

Matt Lowden