Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change folder permission to 777 using PHP temporarily

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?

like image 759
Sumit Bijvani Avatar asked Mar 05 '13 08:03

Sumit Bijvani


People also ask

How do I set 777 permissions on a specific folder?

Easiest way to set permissions to 777 is to connect to Your server through FTP Application like FileZilla, right click on folder, module_installation, and click Change Permissions - then write 777 or check all permissions. Save this answer.

How do I change my chmod 777?

root user run the chmod -R 777 / command and all file permissions for the entire system have read/write/execute for every user.


2 Answers

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)

like image 150
hjpotter92 Avatar answered Sep 23 '22 19:09

hjpotter92


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.

like image 22
Maxim Krizhanovsky Avatar answered Sep 24 '22 19:09

Maxim Krizhanovsky