doq91130 2012-09-18 09:15
浏览 35
已采纳

Zip文件未在php上传

I am trying to upload the zip file but it is not able to upload but when I try uploading other type extension file, it is uploaded correctly. Following is the code for uploading files the zip files:

<?php  

    if(isset($_FILES['fupload'])) {  
        $filename = $_FILES['fupload']['name'];  
        $source = $_FILES['fupload']['tmp_name'];  
        $type = $_FILES['fupload']['type'];  
        $name = explode('.', $filename);  
        $target = 'extracted/' . $name[0] . '-' . time() . '/';  
        // Ensures that the correct file was chosen  
        $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;  
            }  
        }  
      //Safari and Chrome don't register zip mime types. Something better could be used here.  
        $okay = strtolower($name[1]) == 'zip' ? true: false; 
        if(!$okay) { 
              die("Please choose a zip file, dummy!"); 
        } 
        mkdir($target); 
        $saved_file_location = $target . $filename; 
        if(move_uploaded_file($source, $saved_file_location)) { 
            openZip($saved_file_location); 
        } else { 
            die("There was a problem. Sorry!"); 
        } 
    // This last part is for example only. It can be deleted. 
        $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>';  
    }  
    ?>  


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
<html>  
   <head>  
      <title>How to Upload and Open Zip Files With PHP</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><!--end container-->  
    </body>  
</html>  
  • 写回答

1条回答 默认 最新

  • doushangxianq07480 2012-09-18 10:13
    关注

    you have to use

    if(is_dir($target . $name[0]))
    {
    
      $scan = scandir($target . $name[0]);
    
    }else{
    
     $scan = scandir($target);
    } 
    

    inseted of

     $scan = scandir($target . $name[0]);
    

    and increase memory_limit, 'upload_max_filesize, post_max_size, max_input_time and max_execution_timeetc.

    Below code use in your php file.

    ini_set( 'memory_limit', '128M' );
    ini_set('upload_max_filesize', '128M');  
    ini_set('post_max_size', '128M');  
    ini_set('max_input_time', 3600);  
    ini_set('max_execution_time', 3600);
    

    Or set below code in .htaccess file

    php_value upload_max_filesize 128M  
    php_value post_max_size 128M  
    php_value max_input_time 3600  
    php_value max_execution_time 3600 
    

    I have modify you code please try it.

     <?php  
    
    
    
    ini_set( 'memory_limit', '128M' );
    ini_set('upload_max_filesize', '128M');  
    ini_set('post_max_size', '128M');  
    ini_set('max_input_time', 3600);  
    ini_set('max_execution_time', 3600);
    
    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'])) {  
            $filename = $_FILES['fupload']['name'];  
            $source = $_FILES['fupload']['tmp_name'];  
            $type = $_FILES['fupload']['type'];  
            $name = explode('.', $filename);  
            $target = 'extracted/' . $name[0] . '-' . time() . '/';  
            // Ensures that the correct file was chosen  
            $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;  
                }  
            }  
          //Safari and Chrome don't register zip mime types. Something better could be used here.  
            $okay = strtolower($name[1]) == 'zip' ? true: false; 
            if(!$okay) { 
                  die("Please choose a zip file, dummy!"); 
            } 
            mkdir($target); 
            $saved_file_location = $target . $filename; 
            if(move_uploaded_file($source, $saved_file_location)) { 
                openZip($saved_file_location); 
            } else { 
                die("There was a problem. Sorry!"); 
            } 
    
            if(is_dir($target . $name[0]))
            {
    
              $scan = scandir($target . $name[0]);
    
            }else{
    
                $scan = scandir($target);
            } 
            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>';  
        }  
        ?>  
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
        <html>  
          <head>  
            <title>How to Upload and Open Zip Files With PHP</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><!--end container-->  
          </body>  
        </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算