Sahil Gera Sahil Gera - 2 months ago 24
PHP Question

PHP + S3: Permission denied while deleting a file using unlink()

I am trying to solve an extremely trivial issue since long but no luck.

I want to delete a file immediately after uploading it to AWS S3 from a PHP WebServer. Following are the steps:

//Upload file to S3 using PHP SDK's S3Client::putObject method:
$result = $s3_client->putObject( array(
'Bucket' => AWS_BUCKET_NAME,
'Key' => $file_name,
'SourceFile' => $file_path,
'Metadata' => array(
'metadata_field' => 'metadata_value'

//Poll the object until it is accessible
$s3_client->waitUntil('ObjectExists', array(
'Bucket' => AWS_BUCKET_NAME,
'Key' => $file_name

//Delete the file
unlink( $file_path );

These steps work perfectly in case I upload a small file (~500KB).
However, if I upload a larger file (5MB-10MB), I get the following error:

Warning: unlink(<Complete Path to File>): Permission denied in <Complete path to uploader.php> on line N

I am working on Windows and have tried elevating user permissions for the directory and file. (using chmod, chown php commands and made sure that the directory is writable and accessible)

It seems that AWS S3 PutObject method is not releasing the file handle (in case of large files only). I have also tried adding sleep() but not luck.!

Moreover, in case I skip uploading the file to S3 (just to test my delete workflow), the file gets deleted without any issue.

Please help.!


In case anybody else is also stuck on this, I moved nginx server deployment to CentOS and this issue was not observed.