kcalder
asked on
Setting file permissions on a web server with php
I am trying to ensure that files I upload onto a web server are available for editing by users who need to download them. I don't want to manually set file permissions as there are too many to do, but I need to set the file permission once the file has been uploaded. I am using the code shown but the routine is not having any effect. The upload folder has 777 permission but the files themselves prior to upload could have a variety of permission settings. The upload process renames the files prior to upload according to a naming convention I have put in place but this should make no difference to the permission setting function.
<!-- This is the pertinent code from the body of the calling file -->
if (!file_exists($uploaddir . $_FILES["file"]["name"]))
{
// Proceed with file upload
if (is_uploaded_file($_FILES['file']['tmp_name']))
{
// Filename with extension
$savedFilename = renameFile();
// File was uploaded to the temp dir, continue upload process
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir.$savedFilename))
{
// Now log the uploaders IP adress date and time
$date = date("d.m.Y");
$time = date("h:i:s A");
$fp = fopen($log,"ab");
fwrite($fp,"$date | $time | $userName | $ip | ".$savedFilename." | UPLOAD OK"."\r\n");
fclose($fp);
// Set the global flag
$_SESSION['fileUploadStatus'] = true;
// Filename without extension (always the first 11 characters)
$savedFilenameNoExt = substr($savedFilename, 0, 11);
// Set the appropriate file permission
setFilePermission($uploaddir.$savedFilename);
// Insert the data into the database
insertData($savedFilenameNoExt, $ext);
// Return
header("Location: mgtAddDoc.php");
}
else
{
echo "There was an unspecified error while uploading the file, Please contact an Intranet administrator.";
unset($_FILES['file']['tmp_name']);
}
}
else
{
// File was NOT uploaded to the temp dir
switch ($_FILES['file']['error'])
{
case 1:
print 'The file is too large for the server, please contact an Intranet administrator).'; // php installation max file size error
break;
case 2:
print 'The file is larger than the permissible maximum (25 MB), please hit the back button and try again.'; // form max file size error
break;
case 3:
print 'Only part of the file was uploaded, please hit the back button and try again.';
break;
case 4:
print 'No file was uploaded, please hit the back button and try again.';
break;
case 6:
print "Missing a temporary folder, please contact an Intranet administrator.";
break;
case 7:
print "Failed to write file to disk, please contact an Intranet administrator.";
break;
case 8:
print "File upload stopped by extension, please contact an Intranet administrator.";
break;
}
}
}
else
{
echo "Filename already exists, Please rename the file and retry.";
unset($_FILES['file']['tmp_name']);
}
}
<!-- This is the file permissions function -->
function setFilePermission ($theFile)
{
$fr = fopen($theFile, 'r');
if(!$fr)
{
// Must be 0777 (octal), NOT just 777
if(!chmod($theFile, 0777))
{
echo "Error - could not open ".$theFile." for reading!";
exit;
}
else
{
$fr = fopen($theFile, 'r');
}
}
$myvalue = fgets($fr, 1024);
fclose($fr);
return;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Of course! I should pay more attention to code snippets produced by others before using them. Lesson learned, thanks very much.
Look at these:
http://www.thaicreate.com/php-manual/function.chmod.html
http://phpsense.com/php/php-ftp-functions.html
Hope this will help you.
Regards