Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP检查.tar中是否存在文件_Php_File_Pear_Tar - Fatal编程技术网

使用PHP检查.tar中是否存在文件

使用PHP检查.tar中是否存在文件,php,file,pear,tar,Php,File,Pear,Tar,在我的程序中,我需要从.tar文件中读取.png文件 我正在使用pear Archive_Tar类() 如果我要查找的文件存在,则一切正常,但如果它不在.tar文件中,则函数将在30秒后超时。在类文档中,它声明如果找不到文件,则应返回null $tar = new Archive_Tar('path/to/mytar.tar'); $filePath = 'path/to/my/image/image.png'; $file = $tar->extractInString($fileP

在我的程序中,我需要从.tar文件中读取.png文件

我正在使用pear Archive_Tar类()

如果我要查找的文件存在,则一切正常,但如果它不在.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";
}