Frage

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?

War es hilfreich?

Lösung

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)

Andere Tipps

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.

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);   

You can use chmod() function.

For more information, try here

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top