我正在将上传到服务器的文件发送到 Amazon S3 。为此,我:
move_uploaded_file()
将文件发送到temp-uploads文件夹。unlink()
删除文件。unlink()
失败,资源暂时不可用 运行PHP / Apache的Windows Server。
脚本运行完成后,我可以稍后取消链接。在脚本外部调用unlink()
命令会立即从服务器中删除文件。我试图弄清楚如何从move_uploaded_file()
释放文件,但是搜索了一会儿却找不到任何东西。
我确实使用$thumb1 = new Imagick($filetothumbnail);
并创建了一个缩略图。但是我随后致电
$thumb1->clear();
$thumb1->destroy();
也许Imagick仍在打开文件?但是,我已经用一个不会创建缩略图的excel文件进行了测试,该文件仍然无法从服务器中删除。
if(isset($_FILES['file'])){
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$tmp = $_FILES['file']['tmp_name'];
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
$newname = time().'_'.$j['id'].'_'.$name;
$thumbname = 'tn_'.time().'_'.$j['id'].'_'.$name;
move_uploaded_file($_FILES["file"]["tmp_name"], "temp-uploads/".$newname);
//Now, generate thumbnail for the file:
$filetothumbnail = $_SERVER['DOCUMENT_ROOT'].'/temp-uploads/'.$newname;
$thumbnails = $_SERVER['DOCUMENT_ROOT'].'/temp-uploads/thumbs/';
//Send to AWS Bucket
$s3_filepath = 'project-assets/'.$newname;
upload_s3_file($s3_filepath, "temp-uploads/".$newname);
$filepath = s3url.$s3_filepath;
if($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif'){
$thumb1 = new Imagick($filetothumbnail);
$compression_type = Imagick::COMPRESSION_JPEG;
$thumb1->setImageCompression($compression_type);
$thumb1->setImageCompressionQuality(40);
$thumb1->thumbnailImage(500, 0);
$thumb1->setImageFormat('jpg');
$thumb1->writeimage($thumbnails.$thumbname);
$thumb1->clear();
$thumb1->destroy();
//If thumbnail is there. Only for certain file types.
$s3_thumbpath = 'project-thumbnails/'.$thumbname;
upload_s3_file($s3_thumbpath, "temp-uploads/thumbs/".$thumbname);
unlink("temp-uploads/thumbs/".$thumbname); //Delete Thumbnail.
$thumbpath = s3url.$s3_thumbpath;
} else {
$thumbpath = 0;
}
unlink("temp-uploads/".$newname); //Delete Uploaded File.
}
上传到S3功能是:
$s3Client = new S3Client([
'version' => 'latest',
'region' => 'us-east-2',
'credentials' => [
'key' => s3key,
'secret' => s3secret,
],
]);
$result = $s3Client->putObject([
'Bucket' => 'bucketname',
'Key' => $filename,
'SourceFile' => $filepath,
]);
答案 0 :(得分:1)
快速浏览一下文档,表明上传是异步的:
https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_promises.html
您可能应该使用Promise创建一个回调,您可以在其中取消链接文件。链接中有很多代码示例。
答案 1 :(得分:0)
这篇文章为我解决了这个问题:
https://stackoverflow.com/a/41537354/1766536
具体来说,我必须使用fopen
/ fclose
并使用Body
而不是SourceFile