简体   繁体   中英

PHP S3 Permission denied with uplink()

I am trying to unlink a file once I have uploaded it to AWS S3, but I keep getting a Permission denied error. If I remove the code section to upload it to S3 then it works. Also if I hard code a file I previously uploaded, the unlink will remove that file. I cant get it to remove the current file once it is uploaded.

if(isset($_FILES['file'])){
//Get File
$file = $_FILES['file'];
$name = $file['name'];
$tmp_name = $file['tmp_name'];
//Get extension
$ext = explode('.', $name);
$ext = strtolower(end($ext));
//create unique name for file
$key = md5(uniqid());
//create temp
$temp_file_name = "{$key}.{$ext}";
$temp_file_path = "uploads/{$temp_file_name}";
//move to local folder
move_uploaded_file($tmp_name, $temp_file_path);
//    upload to s3
try{
    $s3->putObject([
        'Bucket'    => $config['s3']['bucket'],
        'Key'       => "uploads/{$name}",
        'Body'      => fopen($temp_file_path, 'rb'),
        'ACL'       => 'public-read'
    ]);
} catch (S3Exception $e){
    die("There was some type of uploading issue " . $e->getMessage());
}
unlink($temp_file_path);
}

This is a similar problem as posted here: PHP + S3: Permission denied while deleting a file using unlink() The guy says he solved it by moving nginx server deployment to CentOS This is the first time I am using AWS S3 and could not find what he was talking about. A friend ran this same code with no trouble so I am not sure what could be the issue. There is full file access for this to run, and being that it can unlink the previous file I know that is not the problem.

Any help would be appreciated. Thank you

Turns out I needed to use an fclose().

$body = fopen($temp_file_path, 'rb');

And then:

$s3->putObject([
        'Bucket'    => $config['s3']['bucket'],
        'Key'       => "uploads/{$name}",
        'Body'      => $body,
        'ACL'       => 'public-read'
 ]);

After that:

 fclose($body);

 unlink($temp_file_path);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM