Knowledgebase
Uploaded files receiving incorrect permissions
Posted by zz-James Moir on 21 June 2016 04:07 PM

Issue

When uploading files to a Unix server, you may receive errors about file permissions similar to these below.

Warning: move_uploaded_file(<Filename>) [function.move-uploaded-file]:
failed to open stream: Permission denied in <PathToYourScript> on line XXX

Warning: move_uploaded_file() [function.move-uploaded-file]:
Unable to move '<TemporaryFilename' to '<Filename>' in <PathToYourScript> on line XXX

Cause

This results from a bug in the way the CGI version of PHP (which we use for enhanced security) handles permissions when using the move_uploaded_file function.

Solution

There is no solution at present however we do offer a workaround.

For all instances where the move_uploaded_file function is being used, insert a chmod function immediately after the function. See the example below

if(@move_uploaded_file($uploaded_file_name,$destination_file_name))
{
      @chmod($destination_file_name,<NewPermissionsInUnixFormat>);
      # Code that follows a successful upload;
}
else
{
       #Code to handle move_uploaded_file function error;
}