用php解压缩文件 [英] Unzip a file with php

查看:100
本文介绍了用php解压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解压缩文件,这很好

I want to unzip a file and this works fine

system('unzip File.zip');

但是我需要通过URL传递文件名,但无法使其正常工作,这就是我所拥有的.

But I need to pass in the file name through the URL and can not get it to work, this is what I have.

$master = $_GET["master"];
system('unzip $master.zip'); 

我想念什么?我知道这一定是我忽略的小而愚蠢的事情.

What am I missing? I know it has to be something small and stupid I am overlooking.

谢谢

推荐答案

我只能假设您的代码来自在线某个地方的教程?在这种情况下,请尝试自己解决这个问题.另一方面,该代码实际上可以在线发布为解压缩文件的正确方法,这有点令人恐惧.

I can only assume your code came from a tutorial somewhere online? In that case, good job trying to figure it out by yourself. On the other hand, the fact that this code could actually be published online somewhere as the correct way to unzip a file is a bit frightening.

PHP具有用于处理压缩文件的内置扩展.为此,无需使用system调用. ZipArchive docs 是一种选择.

PHP has built-in extensions for dealing with compressed files. There should be no need to use system calls for this. ZipArchivedocs is one option.

$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超全局变量.

Also, as others have commented, $HTTP_GET_VARS has been deprecated since version 4.1 ... which was a reeeeeally long time ago. Don't use it. Use the $_GET superglobal instead.

最后,要非常小心接受通过$_GET变量传递给脚本的任何输入.

Finally, be very careful about accepting whatever input is passed to a script via a $_GET variable.

更新

根据您的评论,将zip文件提取到其所在目录中的最佳方法是确定该文件的硬路径并将其专门提取到该位置.因此,您可以这样做:

As per your comment, the best way to extract the zip file into the same directory in which it resides is to determine the hard path to the file and extract it specifically to that location. So, you could do:

// 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解压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆