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?

Wasim
  • 1,146
  • 11
  • 25
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
  • 1
    Please give look on http://stackoverflow.com/questions/2958602/still-dont-understand-file-upload-folder-permissions – Wasim Mar 05 '13 at 08:44

5 Answers5

23

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)

Sergiu Dumitriu
  • 11,455
  • 3
  • 39
  • 62
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • 1
    Wouldn't that if-clause already change the permissions to `0777`? – AAGD Nov 05 '15 at 14:23
  • 1
    @AAGD Yes. But the question was: "_Is there any way in PHP to temporary change the permission to 777 while uploading video_"? – hjpotter92 Nov 05 '15 at 15:16
  • To check for file permission, use: if ( substr(sprintf('%o', fileperms($path)), -4) == '0777' ) { chmod($path, 0755); } – Amr Jun 17 '21 at 02:46
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.

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
  • because PHP is obviously not in the group of the folder's owner :) – Maxim Krizhanovsky Mar 05 '13 at 08:32
  • @MaximKrizhanovsky PHP user? Can you elaborate more please. – Elber CM Oct 13 '16 at 13:41
  • 2
    @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 answer](http://stackoverflow.com/a/7771662/797303) – Maxim Krizhanovsky Oct 13 '16 at 18:50
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);   
Wilq
  • 2,245
  • 4
  • 33
  • 37
1

You can use chmod() function.

For more information, try here

Edwin Alex
  • 5,118
  • 4
  • 28
  • 50
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
Anjani Barnwal
  • 1,362
  • 1
  • 17
  • 23