A.Rouze
A.Rouze

Reputation: 51

Class ZipArchive(), some errors with function

I'm looking for a mistake in my code for 2 days now :

<?php

echo $_FILES['import']['name']; ?> <br/> <?php
echo $_FILES['import']['type']; ?> <br/> <?php
echo $_FILES['import']['size']; ?> <br/> <?php
echo $_FILES['import']['tmp_name']; ?> <br/> <?php
echo $_FILES['import']['error'];

if ($_FILES['import']['error'] > 0) {
    echo "Erreur lors du transfert";
}  
else  {
    echo "Transfert Ok" ;?> <br/> <?php

    $nom = $_FILES['import']['name'];
    $localisation = $_FILES['import']['tmp_name'];

    echo $nom; ?> <br/> <?php
    echo $localisation; ?> <br/> <?php

    $zip = new ZipArchive();

    $res = $zip->open($localisation."/".$nom);
//  echo $res;
    if($res === TRUE)   {
        echo 'ok';
    }
    else {
        echo 'erreur';
    }
}
?>

My problem is with the ZipArchive class. As for me, open() and extractTo() functions don't work.

This code is always edited to find a solution. Now i'm trying to "open", it doesn't work.
I don't know why. As for me, the path is not correct, but when I see the path (using echo $localisation."/".$nom), it actually is.

Does any one have a solution or an advice for a debutant developper ?

Upvotes: 3

Views: 49

Answers (1)

Professor Abronsius
Professor Abronsius

Reputation: 33823

The reason perhaps that your function does not work might be the line $res = $zip->open($localisation."/".$nom); - this would result in a string such as /tmp/a23mt.tmp/somefile.zip which would be wrong. Perhaps the following might help though you might wish to designate a particular folder path to save uploaded files to rather than the current working directory of the script.

/* for convenience, create an object representation of the uploaded file */
$file=(object)$_FILES['import'];
/* assign variables */
$name=$file->name;
$type=$file->type;
$size=$file->size;
$tmp=$file->tmp_name;
$error=$file->error;



if( $error==UPLOAD_ERR_OK ){
    if( is_uploaded_file( $tmp ) ){

        /* need to store the file somewhere */
        $target = __DIR__ . DIRECTORY_SEPARATOR . $name;

        /* move the uploaded file before processing */
        $result = move_uploaded_file( $tmp, $target );

        /* was the file moved successfully? */
        if( file_exists( $target ) ){
            clearstatcache();

            /* do stuff - zip */
            $zip = new ZipArchive();
            $result = $zip->open( $target );

            echo $result ? 'ok' : 'erreur';
        } else {
            echo "Error moving/saving file!";
        }

    } else {
        echo "Error! Not an uploaded file...attempted hack!"
    }
} else {
    echo "Erreur lors du transfert!"
}

Upvotes: 1

Related Questions