问题描述
这是一段代码:
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);
}
我正在尝试做以下事情:
I am trying to do the following things:
- 从URL获取照片,并将其保存在服务器的临时文件夹中.这很好.创建图像文件,并在
echo("If file exists: ".file_exists('../storage/temp/image.jpg'));
时回显If file exists: 1
. - 将该文件传递给另一个函数,该函数将文件上传到Amazon s3存储桶.该文件存储在我的s3存储桶中.
- 删除存储在temp文件夹中的照片. 这行不通!我收到一条错误消息:
- Get a photo from a URL and save it in a temp folder in my server. This works fine. The image file is created and echoes
If file exists: 1
whenecho("If file exists: ".file_exists('../storage/temp/image.jpg'));
. - Pass that file to another function that hanldes uploading the file to Amazon s3 bucket. The file gets stored in my s3 bucket.
- Delete the photo stored in the temp folder. This doesn't work! I get an error saying:
如果我使用rename($filename,'../storage/temp/renimage.jpg');
而不是unlink($filename);
,则会收到错误消息:
If I use rename($filename,'../storage/temp/renimage.jpg');
instead of unlink($filename);
i get an error:
如果我删除函数调用$photoService->uploadPhoto($filename);
,则一切正常.
If I remove the function call $photoService->uploadPhoto($filename);
, everything works perfectly fine.
如果该文件正在被另一个进程使用,那么在该进程完成并且该文件不再被任何进程使用后,如何取消链接?我不想使用计时器.
If the file is being used by another process, how do I unlink it after the process has been completed and the file is no longer being used by any process? I do not want to use timers.
请帮助!预先感谢.
推荐答案
只需处理类似的错误.
您的$photoService
似乎由于某种原因而保留了该图像...既然您没有共享$photoService
的代码,我的建议是做这样的事情(假设您不再需要$photoService
):
It seems your $photoService
is holding on to the image for some reason...Since you didn't share the code of $photoService
, my suggestion would be to do something like this (assuming you don't need $photoService
anymore):
[...]
echo("If file exists: ".file_exists($filename));
unset($photoService);
unlink($filename);
}
unset()
方法将销毁给定的变量/对象,因此它不能使用"(或无论如何使用)任何文件.
The unset()
method will destroy the given variable/object, so it can't "use" (or wharever it does) any files.
这篇关于PHP的-取消链接抛出错误:资源暂时不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!