SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to upload Zip file

    Hi,

    I am trying to upload a Zip file and extract it using the following code:

    PHP Code:
    class Zip {

        public function 
    extract($filename$targetDir){

            
    $zip zip_open($filename);

            if (
    $zip) {
                
                while (
    $zip_entry zip_read($zip)) {

                    
    $fp fopen($targetDir zip_entry_name($zip_entry), "w");
                    
                    if (
    zip_entry_open($zip$zip_entry"r")) {
                        
    $buf zip_entry_read($zip_entryzip_entry_filesize($zip_entry));
                        
    fwrite($fp,"$buf");
                        
    zip_entry_close($zip_entry);
                        
    fclose($fp);
                    }
                }
                
    zip_close($zip);
            }
        }

    I call the method like so:

    PHP Code:
    if(isset($_POST['add']) && $_POST['add'] == 'Upload'):
        
    $root "../view/documents/";
        
    $target_path $root.basename$_FILES['ZipFile']['name']);

        if(isset(
    $_FILES['ZipFile']['name']) && $_FILES['ZipFile']['name'] != ''):
            
    Zip::extract($_FILES['ZipFile']['name'] ,$target_path);
        endif;
    endif; 
    And finally my input element is like so:

    PHP Code:
    <input type="file" name="ZipFile" id="ZipFile" value=""/> 
    But i get the following error when i try to upload:

    Warning: zip_read() expects parameter 1 to be resource, integer given in public_html/library/Zip.class.php on line 11

    Warning: zip_close() expects parameter 1 to be resource, integer given in public_html/library/Zip.class.php on line 22
    Any ideas what i am doing wrong, i just want to be able to extract the Zip file int a folder called "documents"..

    Am i missing something?
    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    make sure you have the correct value for $filename

    and instead of
    PHP Code:
    if ($zip) { 
    do it like
    PHP Code:
    if (is_resource($zip)) { 
    because it returns the number of error if filename does not exist

  3. #3
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    Thanks, i don't get the error now but it does not seem to be going into the IF statement.

    I tried putting a die($zip); like so:

    PHP Code:
            if (is_resource($zip)) { 
                die(
    $zip);
    .
    ..
    .. 
    But it doesn't seem to firing, so it doesn't recognize this line i presume:

    PHP Code:
    $zip zip_open($filename); 
    I call the method like this:

    PHP Code:
        if(isset($_FILES['ZipFile']['name']) && $_FILES['ZipFile']['name'] != ''):
            
    Zip::extract($_FILES['ZipFile']['name'] ,$target_path);
        endif; 
    I think i maybe doing this wrong?
    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are you sure your $filename has a correct value

    if

    PHP Code:
    if (is_resource($zip)) { 
                die(
    $zip);

    doesn't fire up there is an error, try

    PHP Code:
    if (is_resource($zip)) { 
    //ok
    }
    else{
                echo 
    'my ' $filename ' is incorrect';
                die(
    $zip);


  5. #5
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok i tried that and i got this:

    my Report.zip is incorrect
    Is this correct?
    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no

    make sure your calling Report.zip from it's correct path

  7. #7
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    Ok this is weird..

    i tried doing this:

    PHP Code:
    if(isset($_POST['add']) && $_POST['add'] == 'Upload'):
        
    $root "../view/documents/";
        
    $target_path $root.basename$_FILES['ZipFile']['name']);
        die(
    $target_path);
        if(isset(
    $_FILES['ZipFile']['name']) && $_FILES['ZipFile']['name'] != ''):
            
    Zip::extract($_FILES['ZipFile']['name'] ,$target_path);
        endif;
    endif; 
    die($target_path) gives me this:

    ../view/documents/Report.zip
    Which is correct, that is where i want to upload and extract the ZIP file to?

    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy

  8. #8
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    from where the $filename you are reading from and not where do you want to extract it to - try it with it's absolute path

    and try to upload it on a temp folder first

  9. #9
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    I got it working
    Software Engineer ASP, ASP.NET, VB, PHP

    Kind regards
    Billy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •