【发布时间】:2017-08-23 04:39:43
【问题描述】:
这是一段代码:
public function uploadPhoto(){
$filename = '../storage/temp/image.jpg';
file_put_contents($filename,file_get_contents('http://example.com/image.jpg'));
$photoService->uploadPhoto($filename);
echo("If file exists: ".file_exists($filename));
unlink($filename);
}
我正在尝试做以下事情:
- 从 URL 获取照片并将其保存在我服务器的临时文件夹中。这工作正常。图像文件已创建并在
echo("If file exists: ".file_exists('../storage/temp/image.jpg'));时回显If file exists: 1。 - 将该文件传递给另一个处理将文件上传到 Amazon s3 存储桶的函数。该文件存储在我的 s3 存储桶中。
- 删除临时文件夹中存储的照片。 这不起作用!我收到一条错误消息:
unlink(../storage/temp/image.jpg):资源暂时不可用
如果我使用 rename($filename,'../storage/temp/renimage.jpg'); 而不是 unlink($filename); 我会收到错误:
rename(../storage/temp/image.jpg,../storage/temp/renimage.jpg):该进程无法访问该文件,因为它正被另一个进程使用。 (代码:32)
如果我删除函数调用$photoService->uploadPhoto($filename);,一切正常。
如果文件正被另一个进程使用,在该进程完成并且文件不再被任何进程使用后,我如何取消链接?我不想使用计时器。
请帮忙!提前致谢。
【问题讨论】:
-
作为旁注,您应该使用绝对文件路径而不是相对文件路径。在文件路径中使用
$_SERVER['DOCUMENT_ROOT']而不是../ -
不要完整阅读,但在
echo("If file exists: ".file_exists($filename));php.net/manual/en/function.clearstatcache.php 之前使用clearstatcache(),也许$photoService->uploadPhoto在你之前取消链接。 -
使用唯一名称而不是
image.jpg会不会更好,也许不同。请求尝试处理同一个文件。 -
@Martin 不是在
unlink之后,而是在file_exists之前,谁知道uploadPhoto做了什么。 ;-) 我粘贴了链接,所以 OP 可以阅读它,获取它。 -
@JustOnUnderMillions,是的。我在我的实际代码中使用了一个唯一的名称生成器。上面给出的代码sn-p是为了贴在这里。