Page 1 of 1

Upload zip folder, extract it, rename files inside it, save

Posted: Sat Sep 15, 2012 8:29 pm
by Ranura
I'm trying to create a form that uploads zipped files into the server and extract. BTW it gets saved into MYSQL table.
I want to rename the files inside the extracted folder and it is being the problem now I faced.
Can someone give a solution for this issue?
Following is my code.

Code: Select all

    <?php  
      
    function openZip($file_to_open) {  
        global $target;  
          
        $zip = new ZipArchive();  
        $x = $zip->open($file_to_open);  
        if($x === true) {  
            $zip->extractTo($target);
            $zip->close();  
              
            unlink($file_to_open);  
        } else {  
            die("There was a problem. Please try again!");  
        }  
    }  
    
    
    if(isset($_FILES['fupload'])) {

     
    function findexts ($filename) { 
        $filename = strtolower($filename); 
        $exts = explode("[/\\.]", $filename); 
        $n = count($exts)-1; 
        $exts = $exts[$n]; 
        return $exts; 
    } 
      
    $ext = findexts ($_FILES['fupload']['name']) ; 
     
    $ran = time () ;

    $ran2 = $ran."-";
    
        $source = $_FILES['fupload']['tmp_name']; 
        $type = $_FILES['fupload']['type'];   
        $name = explode('.', $ext);
        $target = "images/";
        $accepted_types = array('application/zip',   
                                    'application/x-zip-compressed',   
                                    'multipart/x-zip',   
                                    'application/s-compressed');  
      
        foreach($accepted_types as $mime_type) {  
            if($mime_type == $type) {  
                $okay = true;  
                break;  
            }   
        }  
              
        $okay = strtolower($name[1]) == 'zip' ? true: false; 
     
        if(!$okay) { 
              die("Please choose a zip file!");        
        } 
         
        $saved_file_location = $target . $ext; 
        if(move_uploaded_file($source, $saved_file_location)) { 
            openZip($saved_file_location);            
        } else { 
            die("There was a problem. Sorry!"); 
        } 
           
        $scan = scandir($target . $name[0]); 
        print '<ul>'; 
        for ($i = 0; $i<count($scan); $i++) { 
            if(strlen($scan[$i]) >= 3) { 
                $check_for_html_doc = strpos($scan[$i], 'html'); 
                $check_for_php = strpos($scan[$i], 'php'); 
                 
                if($check_for_html_doc === false && $check_for_php === false) { 
                    echo '<li>' . '1 - ' . $scan[$i] . '</li>'; 
                } else { 
                    echo '<li><a href="' . $target . $name[0] . '/' . $scan[$i] . '">' . $scan[$i] . '</a></li>'; 
                }     
            } 
        } 
        print '</ul>'; 
    
        $rname = scandir($target . $name[0]); 
        
        for ($j = 0; $j<count($rname); $j++) { 
            rename($target . $name[0], $ran2 . $name[0]); 
        } 
    
        $connect = mysql_connect("localhost", "root", "123") or die ("Error , check your server connection.");
        mysql_select_db("upload_and_unpack_zipped_file") or die("Error , check your database.");
        
        $query = "INSERT INTO table_1 (image1, image2, image3, image4) values('$scan[2]', '$scan[3]', '$scan[4]', '$scan[5]')";
        mysql_query($query)  or die(mysql_error());
    
    }  
      
    ?>  
      
      
      
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
       
    <html>  
      <head>  
        <title>Upload</title>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
      </head>  
      <body>  
        <div id="container">  
        <h1>Upload A Zip File</h1>  
        <form enctype="multipart/form-data" action="" method="post">  
            <input type="file" name="fupload" /><br />  
            <input type="submit" value="Upload Zip File" />  
        </form>
    
        </div>  
      </body>  
    </html>

Re: Upload zip folder, extract it, rename files inside it, save

Posted: Sun Sep 16, 2012 4:42 am
by Saman
I see a semantic issue (bad programming) there in your code. Can you change your function function openZip($file_to_open) to function openZip($file_to_open, $target) and remove the global definition from there. So when you call the function, you call it alone with the target. Can you do that change and resubmit your code. I'll then have a look.

Re: Upload zip folder, extract it, rename files inside it, save

Posted: Sun Sep 16, 2012 8:33 am
by Ranura
Saman wrote:I see a semantic issue (bad programming) there in your code. Can you change your function function openZip($file_to_open) to function openZip($file_to_open, $target) and remove the global definition from there. So when you call the function, you call it alone with the target. Can you do that change and resubmit your code. I'll then have a look.
Ok. I modified it as you mentioned. Please pay your attention to my code now. Many thanks for correcting me.
Following is the modified code.

Code: Select all

    <?php  
      
    function openZip($file_to_open, $target) {  
        
        $zip = new ZipArchive();  
        $x = $zip->open($file_to_open);  
        if($x === true) {  
            $zip->extractTo($target);
            $zip->close();  
              
            unlink($file_to_open);  
        } else {  
            die("There was a problem. Please try again!");  
        }  
    }  
    
    
    if(isset($_FILES['fupload'])) {

     
    function findexts ($filename) { 
        $filename = strtolower($filename); 
        $exts = explode("[/\\.]", $filename); 
        $n = count($exts)-1; 
        $exts = $exts[$n]; 
        return $exts; 
    } 
      
    $ext = findexts ($_FILES['fupload']['name']) ; 
     
    $ran = time () ;

    $ran2 = $ran."-";
    
        $source = $_FILES['fupload']['tmp_name']; 
        $type = $_FILES['fupload']['type'];   
        $name = explode('.', $ext);
        $target = "images/";
        $accepted_types = array('application/zip',   
                                    'application/x-zip-compressed',   
                                    'multipart/x-zip',   
                                    'application/s-compressed');  
      
        foreach($accepted_types as $mime_type) {  
            if($mime_type == $type) {  
                $okay = true;  
                break;  
            }   
        }  
              
        $okay = strtolower($name[1]) == 'zip' ? true: false; 
     
        if(!$okay) { 
              die("Please choose a zip file!");        
        } 
         
        $saved_file_location = $target . $ext; 
        if(move_uploaded_file($source, $saved_file_location)) { 
            openZip($saved_file_location, $target);            
        } else { 
            die("There was a problem. Sorry!"); 
        } 
           
        $scan = scandir($target . $name[0]); 
        print '<ul>'; 
        for ($i = 0; $i<count($scan); $i++) { 
            if(strlen($scan[$i]) >= 3) { 
                $check_for_html_doc = strpos($scan[$i], 'html'); 
                $check_for_php = strpos($scan[$i], 'php'); 
                 
                if($check_for_html_doc === false && $check_for_php === false) { 
                    echo '<li>' . $scan[$i] . '</li>'; 
                } else { 
                    echo '<li><a href="' . $target . $name[0] . '/' . $scan[$i] . '">' . $scan[$i] . '</a></li>'; 
                }     
            } 
        } 
        print '</ul>'; 
    
        $connect = mysql_connect("localhost", "root", "123") or die ("Error , check your server connection.");
        mysql_select_db("upload_and_unpack_zipped_file") or die("Error , check your database.");
        
        $query = "INSERT INTO table_1 (image1, image2, image3, image4) values('$scan[2]', '$scan[3]', '$scan[4]', '$scan[5]')";
        mysql_query($query)  or die(mysql_error());
    
    }  
      
    ?>  
      
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
       
    <html>  
      <head>  
        <title>Upload</title>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
      </head>  
      <body>  
        <div id="container">  
        <h1>Upload A Zip File</h1>  
        <form enctype="multipart/form-data" action="" method="post">  
            <input type="file" name="fupload" /><br />  
            <input type="submit" value="Upload Zip File" />  
        </form>
    
        </div>  
      </body>  
    </html>

Re: Upload zip folder, extract it, rename files inside it, save

Posted: Tue Oct 09, 2012 3:49 pm
by Saman
Opps. I have just seen that you needed further help on this.Thought you have fixed it by yourself after my first tip.

I have tried to re-read your code and I have seen few problems.

One of the problems I see in your code is, you have defined function "findexts" within "if(isset($_FILES['fupload'])) {" scope which is illegal. You must define the function on top of it where it is not under any scope. However you can call the function within a scope.

Code: Select all

    function findexts ($filename) { 
        $filename = strtolower($filename); 
        $exts = explode("[/\\.]", $filename); 
        $n = count($exts)-1; 
        $exts = $exts[$n]; 
        return $exts; 
    } 

    if(isset($_FILES['fupload'])) {

        $ext = findexts ($_FILES['fupload']['name']) ;  
Another problem I see is, your indents are not correct. Be sure to maintain indents so the code becomes more readable.

I don't see any big issue other than that. I generally use "echo" than "print", that's just my php flavour. No functional difference ;)

Report me if you need further help. If there is a delay more than 48-hours, you may PM me as well.