Working with PHP to Adjust the Permissions on a Folder
Posted in WordPress on Jul 15, 2014
Often it is useful to allow readers to your internet site to upload content, for instance, pictures, which can then be shown on the website.
Folders on a internet site, for case in point, an visuals folder will usually have permissions of 755, which usually means that normal site visitors to the web-site will be unable to upload material into the folder. They will receive an error concept when they attempt to do so.
In order to enable the add, just one alternative is to temporarily improve the permissions on the folder to 777 prior to the upload is created, and then to alter them back to 755 soon after the add has been manufactured.
The next PHP script shows how you can do this making use of PHP’s ftp_web-site command. This script only modifications the permissions 1 way (to 777), it would not adjust them back. You would have to have to increase the script or publish a further 1 to do this. The line that reads $mod = ‘0777’ // permissions to be established is the 1 that determines what the permissions are going to be set to.
Note: Make sure you eliminate the house amongst the ” at the stop of the script.
http://www.xyz.com’ // host (web site) that incorporates the folder you want to adjust
$person = ‘username’ // username to log onto the host
$password = ‘password’ // password to log onto the host
$folder = ‘public_html/exam/’ // folder title to adjust
$mod = ‘0777’ // permissions to be established
// join to FTP web page
$conn = ftp_hook up(“$host”)
echo ‘Error: Could not link to ftp server’
// log in to FTP web page
@ $end result = ftp_login($conn, $consumer, $password)
if (!$end result)
echo “Error: Could not log on as $consumer”
// test to improve the permissions on the listing
if (ftp_web page($conn, ‘CHMOD ‘.$mod.’ ‘.$folder))
echo “Correctly changed permissions”
echo “There was a issue transforming the permissions”
// near the relationship
The script is pretty straight ahead. You in the beginning set up a several variables for the host name and things like that, you then link to the FTP site, log on to the web-site, improve the permissions of the folder, and then ultimately log off.
The route to the folder (assigned to $folder) will be one thing like ‘public_html/…/’. Test with your ISP if you’re not absolutely sure what this is.
By John Dixon