我想解压缩一个文件,这很好用
system('unzip File.zip');
但是我需要通过 URL 传递文件名并且无法使其工作,这就是我所拥有的。
$master = $_GET["master"];
system('unzip $master.zip');
我错过了什么?我知道它一定是我忽略的一些小而愚蠢的东西。
谢谢你,
最佳答案
我只能假设您的代码来自在线某处的教程?在这种情况下,尝试自己解决这个问题是件好事。另一方面,这个代码实际上可以作为解压缩文件的正确方法在线发布,这有点令人恐惧。
PHP 具有处理压缩文件的内置扩展。应该没有必要为此使用 system
调用。 ZipArchive
docs是一种选择。
$zip = new ZipArchive;
$res = $zip->open('file.zip');
if ($res === TRUE) {
$zip->extractTo('/myzips/extract_path/');
$zip->close();
echo 'woot!';
} else {
echo 'doh!';
}
另外,正如其他人所评论的那样,$HTTP_GET_VARS
自 4.1 版以来已被弃用......这是很久以前的事了。不要使用它。请改用 $_GET
超全局。
最后,在接受通过 $_GET
变量传递给脚本的任何输入时要非常小心。
始终清理用户输入。
更新
根据您的评论,将 zip 文件提取到其所在目录的最佳方法是确定文件的硬路径并将其专门提取到该位置。所以,你可以这样做:
// assuming file.zip is in the same directory as the executing script.
$file = 'file.zip';
// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
echo "WOOT! $file extracted to $path";
} else {
echo "Doh! I couldn't open $file";
}
关于php - 用php解压一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8889025/