开发者

Copy extracted files from ZIP to directory

Note: I'm on localhost, so CHMOD'ing is not the answer.


Anyway, I can't get to copy the files which were extracted from a ZIP to a directory, the files are extracted, but they won't be copied... the files just lay in the root folder where upload.php is. I'v开发者_如何学Goe got this:

exec('unzip ' . $_FILES['file']['tmp_name'], $ary, $output);

$img = 0;

$number = count($ary);

foreach($ary as $file)
{
    copy($file, 'i/');
    unlink($file);
    $img++;
}

echo $img . '/' . $number;

It outputs 11/11, so thats good... but the files are not copied and the original files are not deleted. Why isn't it copying them?


The problem is that you are assuming the array $ary has the filenames in them. But it does not.

$ary which hold the output of the unzip command will be something like:

extracting: foo
extracting: bar
...

Try dumping the array contents using var_dump($ary) to confirm this.

My advice would be don't use external unzip command, instead use build in zip functions of PHP as:

$zip = new ZipArchive;
if ($zip->open("$_FILES['file']['tmp_name']") === TRUE) {
    $zip->extractTo('i/');
    $zip->close();
} else {
    // failed to extract.
}


As codaddict already mentioned, try PECL zip module, or PEAR File_Archive. But if you can't install any of those, and stuck with an old PHP, try:

exec('unzip ' . $_FILES['file']['tmp_name'] . ' | tail -n"+2" | cut -d" " -f4 ', $ary, $output);

That should give you just the filenames.


It does not copy the files beause you did not read the manual fully:

$dest - The destination path. If $dest is a URL, the copy operation may fail if the wrapper does not support overwriting of existing files. Warning: If the destination file already exists, it will be overwritten.

You need to specify the full destination path (including filename), not only the directory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜