1

zip ファイルをアップロードしようとしてもアップロードできませんが、他のタイプの拡張子ファイルをアップロードしようとすると、正しくアップロードされます。以下は、ファイルを zip ファイルにアップロードするためのコードです。

<?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>  
4

1 に答える 1

1

使用する必要があります

if(is_dir($target . $name[0]))
{

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

}else{

 $scan = scandir($target);
} 

のはめ込み

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

と増加memory_limit, 'upload_max_filesize, post_max_size, max_input_time and max_execution_timeなど。

以下のコードは、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);

または、以下のコードを.htaccessファイルに設定します

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

コードを変更しましたので、お試しください。

 <?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>
于 2012-09-18T10:13:42.000 に答える