在移动文件上的move_uploaded_file()之后调用PHP unlink()失败 [英] Calling PHP unlink() after move_uploaded_file() on moved file fails

查看:85
本文介绍了在移动文件上的move_uploaded_file()之后调用PHP unlink()失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将上传到服务器的文件发送到 Amazon S3 .为此,我:

I am sending files uploaded to my server to Amazon S3. To do this, I:

  • 使用move_uploaded_file()将文件发送到temp-uploads文件夹.
  • 我使用S3 SDK将文件作为对象上传到S3.
  • 我使用unlink()删除文件.
  • unlink()失败,资源暂时不可用
  • Use move_uploaded_file() to send file to a temp-uploads folder.
  • I use the S3 SDK to upload the file as an object to S3.
  • I use unlink() to delete the file.
  • unlink() fails with Resource temporarily unavailable

运行PHP/Apache的Windows Server.

Windows Server running PHP/Apache.

脚本运行完毕后,我可以稍后取消链接.在脚本外部调用unlink()命令会立即从服务器中删除文件.我试图弄清楚如何从move_uploaded_file()释放文件,但是搜索了一会儿之后找不到任何东西.

I can unlink later after the script is done running. Calling the unlink() command outside of the script deletes the file from the server immediately. I was trying to figure out how to maybe release the file from move_uploaded_file(), but can't find anything after searching for a while.

我确实使用$thumb1 = new Imagick($filetothumbnail);并创建了缩略图.但是我随后致电

I do use $thumb1 = new Imagick($filetothumbnail); and create a thumbnail. But I then call

$thumb1->clear();
$thumb1->destroy();

也许Imagick仍在打开文件?但是,我已经用一个不会创建缩略图的excel文件进​​行了测试,并且该文件仍然无法从服务器中删除.

Maybe Imagick still has the file open? However, I have tested this with an excel file which does not make a thumbnail, and the file still fails to delete from the server.

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,
]);

推荐答案

这篇文章为我解决了这个问题:

This post helped solve this for me:

https://stackoverflow.com/a/41537354/1766536

具体来说,我必须使用fopen/fclose并使用Body而不是SourceFile

Specifically, I had to use fopen/fclose and upload using Body instead of SourceFile

这篇关于在移动文件上的move_uploaded_file()之后调用PHP unlink()失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆