php - unlink throws error:资源暂时不可用

时间:2017-03-29 15:25:32

标签: php amazon-s3 unlink

这是一段代码:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
   <book id="bk104">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
   </book>
   <book id="bk105">
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-09-10</publish_date>
      <description>The two daughters of Maeve, half-sisters, 
      battle one another for control of England. Sequel to 
      Oberon's Legacy.</description>
   </book>
   <book id="bk106">
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-09-02</publish_date>
      <description>When Carla meets Paul at an ornithology 
      conference, tempers fly as feathers get ruffled.</description>
   </book>
   <book id="bk107">
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>A deep sea diver finds true love twenty 
      thousand leagues beneath the sea.</description>
   </book>
   <book id="bk108">
      <author>Knorr, Stefan</author>
      <title>Creepy Crawlies</title>
      <genre>Horror</genre>
      <price>4.95</price>
      <publish_date>2000-12-06</publish_date>
      <description>An anthology of horror stories about roaches,
      centipedes, scorpions  and other insects.</description>
   </book>
   <book id="bk109">
      <author>Kress, Peter</author>
      <title>Paradox Lost</title>
      <genre>Science Fiction</genre>
      <price>6.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>After an inadvertant trip through a Heisenberg
      Uncertainty Device, James Salway discovers the problems 
      of being quantum.</description>
   </book>
   <book id="bk110">
      <author>O'Brien, Tim</author>
      <title>Microsoft .NET: The Programming Bible</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-09</publish_date>
      <description>Microsoft's .NET initiative is explored in 
      detail in this deep programmer's reference.</description>
   </book>
   <book id="bk111">
      <author>O'Brien, Tim</author>
      <title>MSXML3: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-01</publish_date>
      <description>The Microsoft MSXML3 parser is covered in 
      detail, with attention to XML DOM interfaces, XSLT processing, 
      SAX and more.</description>
   </book>
   <book id="bk112">
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>49.95</price>
      <publish_date>2001-04-16</publish_date>
      <description>Microsoft Visual Studio 7 is explored in depth,
      looking at how Visual Basic, Visual C++, C#, and ASP+ are 
      integrated into a comprehensive development 
      environment.</description>
   </book>
</catalog>

我正在尝试做以下事情:

  1. 从网址获取照片并将其保存在我服务器的临时文件夹中。这很好用。创建图像文件,并在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); }
  2. 时回显If file exists: 1
  3. 将该文件传递给另一个将文件上传到Amazon s3存储桶的功能。该文件存储在我的s3存储桶中。
  4. 删除存储在临时文件夹中的照片。 这不起作用!我收到错误消息:
  5.   

    unlink(../ storage / temp / image.jpg):资源暂时不可用

    如果我使用echo("If file exists: ".file_exists('../storage/temp/image.jpg'));代替rename($filename,'../storage/temp/renimage.jpg');,我会收到错误消息:

      

    重命名(../ storage / temp / image.jpg,.. / storage / temp / renimage.jpg):进程无法访问该文件,因为它正由另一个进程使用。 (代码:32)

    如果我删除了函数调用unlink($filename);,一切都运行正常。

    如果该文件正由另一个进程使用,如何在进程完成后取消链接,并且该文件不再被任何进程使用?我不想使用计时器。

    请帮忙!提前谢谢。

5 个答案:

答案 0 :(得分:4)

只需处理类似的错误。

由于某种原因,您的$photoService似乎正在抓住图像...... 由于您没有共享$photoService的代码,我的建议是做这样的事情(假设您不再需要$photoService):

[...]
echo("If file exists: ".file_exists($filename));
unset($photoService);
unlink($filename);
}

unset()方法将销毁给定的变量/对象,因此它不能使用&#34;使用&#34; (或者它可以做任何文件)。

答案 1 :(得分:2)

我在这个问题上坐了一两个小时,终于意识到“暂时不可用”的确意味着“暂时”。

在我的例子中,并发PHP脚本访问该文件,无论是写入还是读取。当unlink()进程的时机不佳时,整个事情就失败了。

解决方案非常简单:使用(通常不太合适)@来防止向用户显示错误(当然,还可以阻止beinf打印错误),然后再试一次:

$gone = false;
for ($trial=0; $trial<10; $trial++) {
    if ($gone = @unlink($filename)) {
        break;
    }
    // Wait a short time
    usleep(250000);
    // Maybe a concurrent script has deleted the file in the meantime
    clearstatcache();
    if (!file_exists($filename)) {
        $gone = true;
        break;
    }
}
if (!$gone) {
    trigger_error('Warning: Could not delete file '.htmlspecialchars($filename), E_USER_WARNING);
}

在解决了这个问题并进一步推动我的运气后,我还可以触发file_put_contents()的“资源暂时不可用”问题。同样的解决方案,现在一切正常。

如果我足够聪明和/或取消链接将来会失败,我会将@替换为ob_start(),因此错误消息可以告诉我确切的错误。

答案 2 :(得分:1)

我遇到了同样的问题。在执行取消链接之前,S3客户端似乎不想解锁。如果将内容提取到变量中并将其设置为putObject数组中的“body”:

$fileContent = file_get_contents($filepath);
$result = $s3->putObject(array(
    'Bucket'       => $bucket,
    'Key'          => $folderPath,
    'Body'         => $fileContent,
     //'SourceFile'   => $filepath,
    'ContentType'  => 'text/csv',
    'ACL'          => 'public-read'
));

请参阅此答案:How to unlock the file after AWS S3 Helper uploading file?

答案 3 :(得分:0)

最简单的解决方案:

gc_collect_cycles();
unlink($file);

为我做! 将文件上传到Amazon S3之后,就可以直接删除服务器上的文件。

参见此处:https://github.com/aws/aws-sdk-php/issues/841

  

GuzzleHttp \ Stream对象保留一个资源句柄,直到其   调用__destruct方法。通常,这意味着一旦流超出范围,资源就会被释放,但是有时,这取决于   在PHP版本上,以及脚本是否尚未填充垃圾   收集器的缓冲区,可以推迟垃圾回收。   gc_collect_cycles将强制收集器运行并调用__destruct   在所有无法访问的流对象上。

:)

答案 4 :(得分:-2)

unlink方法返回bool值,因此您可以构建一个循环,使用一些wait()和retries限制来等待您的进程完成。

另外放&#34; @&#34;在unlink上,隐藏访问错误。

如果达到重试次数,则抛出另一个错误/异常。