我正在尝试上传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>
答案 0 :(得分: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>