Change folder permission to 777 using PHP temporarily
Asked Answered
S

5

7

I have a Video folder on my server which has 755 permission. The problem is: when someone goes to upload video file, it can't be upload into that folder because of permission error.

If I change the permission to 777, then Video can be uploaded. But I don't want to allow the folder permission to 777 for security reason.

Is there any way in PHP to temporary change the permission to 777 while uploading video?

Sceptre answered 5/3, 2013 at 8:26 Comment(1)
Please give look on #2959102Rickie
G
24

PHP provides a function, chmod() for the task.

Attempts to change the mode of the specified file to that given in mode.

You can put it in an if statement, and if it returns false, you can skip the upload file part.


The usage will be like

if( chmod($path, 0777) ) {
    // more code
    chmod($path, 0755);
}
else
    echo "Couldn't do it.";

As described in the chmod function manual, the $mode must be in octal format - with leading zero, i.e chmod($path, 0777)

Goodness answered 5/3, 2013 at 8:29 Comment(3)
Wouldn't that if-clause already change the permissions to 0777?Triumphal
@Triumphal Yes. But the question was: "Is there any way in PHP to temporary change the permission to 777 while uploading video"?Goodness
To check for file permission, use: if ( substr(sprintf('%o', fileperms($path)), -4) == '0777' ) { chmod($path, 0755); }Premer
N
4

There is a way (PHP provides chmod function) but since PHP is not the owner of the folder, you won't be able to change the permission. And I think you are solving the wrong problem. Add webserver and PHP in the same group and give 775 to the folder.

Nonlegal answered 5/3, 2013 at 8:29 Comment(3)
because PHP is obviously not in the group of the folder's owner :)Nonlegal
@MaximKrizhanovsky PHP user? Can you elaborate more please.Whoremaster
@ElberCM PHP can be run in several ways, including as part of the web server process, or as a CGI program. Most often than not PHP is under the same user as the web server. This is not necessary however. Check out this stackoverflow answerNonlegal
F
2

You have to initialize the config for the upload, like this:

$config['remove_spaces'] = FALSE;
$config['upload_path'] = $path;
$this->upload->initialize($config);
$this->load->library('upload', $config);   
Franza answered 6/12, 2013 at 6:44 Comment(1)
that has to do with codeigniter not what he's working on.Mantooth
O
1

You can use chmod() function.

For more information, try here

Outdoors answered 5/3, 2013 at 8:30 Comment(0)
M
0

Warning: You cannot undo the file permissions that are changed by the script below. Proceed with extreme caution.

Important: this code should only be used if you remember to delete it immediately after use. As above, its use may put your site into an insecure state.


//replace dirname(__FILE__) with desired folder.
file_fix_directory(dirname(__FILE__));

function file_fix_directory($dir, $nomask = array('.', '..')) {
  if (is_dir($dir)) {
     // Try to make each directory world writable.
     if (@chmod($dir, 0777)) {
       echo "

Made writable: " . $dir . "

"; } } if (is_dir($dir) && $handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if (!in_array($file, $nomask) && $file[0] != '.') { if (is_dir("$dir/$file")) { // Recurse into subdirectories file_fix_directory("$dir/$file", $nomask); } else { $filename = "$dir/$file"; // Try to make each file world writable. if (@chmod($filename, 0666)) { echo "

Made writable: " . $filename . "

"; } } } } closedir($handle); } }

or you can use terminal for this

chmod -R 755 public_html/test
Marabou answered 21/11, 2021 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.