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

Open in new window

kcalderAsked:
Who is Participating?
 
joep1978Connect With a Mentor Commented:
setFilePermission only changes the permissions if the file cannot be opened.  The chmod is not performed if it can be opened, and actually I think it's unlikely you could perform the chmod if the file was not readable to the webserver!

If you want to perform the chmod regardless, change the function as follows:-
function setFilePermission ($theFile)
{
        $fr = fopen($theFile, 'r');
 
        // Must be 0777 (octal), NOT just 777 
        if(!chmod($theFile, 0777)) 
        {
                        echo "Error - could not open ".$theFile." for reading!";
                        exit;
        } 
 
        $fr = fopen($theFile, 'r');
        $myvalue = fgets($fr, 1024);
        fclose($fr);
        
        return;
}

Open in new window

0
 
shobinsunCommented:
0
 
kcalderAuthor Commented:
Of course! I should pay more attention to code snippets produced by others before using them. Lesson learned, thanks very much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.