[英]Check if file exists in .tar using PHP
在我的程序中,我需要從.tar文件中讀取.png文件。
我正在使用pear Archive_Tar類( http://pear.php.net/package/Archive_Tar/redirected )
如果所要查找的文件存在,則一切都很好,但是如果該文件不在.tar文件中,則該功能會在30秒后超時。 在類文檔中,它聲明如果找不到文件,則應返回null。
$tar = new Archive_Tar('path/to/mytar.tar');
$filePath = 'path/to/my/image/image.png';
$file = $tar->extractInString($filePath); // This works fine if the $filePath is correct
// if the path to the file does not exists
// the script will timeout after 30 seconds
var_dump($file);
return;
關於解決此庫或其他可用於解決問題的庫的任何建議?
listContent方法將返回指定檔案中存在的所有文件(以及有關它們的其他信息)的數組。 因此,如果您首先檢查要提取的文件是否存在於該數組中,則可以避免遇到延遲。
以下代碼未經過優化-多次調用以提取不同的文件,例如$ files數組僅應填充一次-但是這是一種很好的方法。
include "Archive/Tar.php";
$tar = new Archive_Tar('mytar.tar');
$filePath = 'path/to/my/image/image.png';
$contents = $tar->listContent();
$files = array();
foreach ($contents as $entry) {
$files[] = $entry['filename'];
}
$exists = in_array($filePath, $files);
if ($exists) {
$fileContent = $tar->extractInString($filePath);
var_dump($fileContent);
} else {
echo "File $filePath does not exist in archive.\n";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.