tags:

views:

56

answers:

3

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've 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?

+1  A: 

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.
}
codaddict
+1 for a good answer, although probably should note that ZipArchive was added in a later revision of PHP 4. If you're on an old system you may be forced to just parse the output of a shell exec() line.
steven_desu
A: 

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.

racetrack
A: 

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.

cweiske