2017-08-30 38 views
0

我想創建一個.zip壓縮文件,將其上傳到Amazon S3,然後從服務器中刪除創建的.zip文件。步驟1和2是偉大的工作,但刪除步驟返回:什麼會導致取消鏈接返回'資源暫時不可用'?

unlink(temp/file.zip): Resource temporarily unavailable

我試圖unset所有相關變量和資源,但我仍然得到錯誤。

下面的代碼:

$zipFile = 'temp/file.zip'; 

// create the zip archive: 
$z = new \ZipArchive(); 
$z->open($zipFile, \ZipArchive::CREATE); 
$z->addEmptyDir('testdirectory'); 

// add a file 
$filename = 'fileName.txt'; 
$content = 'Hello World'; 
$z->addFromString('testdirectory/' . $filename, $content); 
$z->close(); 

// upload to S3 
$s3 = AWS::createClient('s3'); 
$result = $s3->putObject(array(
    'Bucket'  => 'my-bucket-name', 
    'Key'   => basename($zipFile), 
    'SourceFile' => $zipFile 
)); 

// check to see if the file was uploaded 
if ($result['@metadata']['statusCode'] == "200") { 
    $uploaded = true; 
} 

// delete the temp file 
if ($uploaded) { 
    unset($result); 
    unset($s3); 
    unset($z); 
    if (file_exists($zipFile)) { 
     unlink($zipFile); 
    } 
} 

一些額外的細節:我使用的是5.4流明和AWS-SDK-PHP-laravel包。

任何有識之士將不勝感激!謝謝。

+0

什麼是'$ z-> close()'返回?它返回一個'bool'。檢查它是否成功關閉。 – BugHunterUK

+0

'$ z-> close();'returns'bool(true)' – WayneC

+0

奇怪。我測試了代碼(沒有S3),它爲我工作。棘手的一個。你可以在沒有S3代碼的情況下運行腳本,看看它是否有效。 – BugHunterUK

回答

2

S3持有資源,所以我們必須強制清除gc(垃圾收集器)。

只是在刪除該文件之前做gc_collect_cycles()

+0

沒有幫助我的情況(PHP 7.1)。 – BurninLeo

+0

幫助我的情況(PHP 7.1.1) – DB1500

相關問題