使用PHP上传zip文件,未定义索引


uploading zip files with PHP, undefined index

我在上传zip文件时遇到问题,似乎找不到答案。索引.php

<form id="convertFile" action="convert.php" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="exampleInputFile">File input</label>
        <input name="upload" type="file" id="inputFile">
    </div>
    <div class="form-group">
        <button type="submit">Submit</button>
    </div>
</form>

convert.php:

if(isset($_FILES)){
    echo $_FILES['upload']['name'];
}else{
    echo json_encode(array('status'=>'error'));
}

当我上传一个zip文件时,我得到:注意:未定义的索引:在第3行上的C:''wamp''www''xmlconverter''convert.php中上传

这就是chrome在帖子标题中显示的内容:

    ------WebKitFormBoundaryuFNy5dZtFj7olmD5
Content-Disposition: form-data; name="zip_file"; filename="123.zip"
Content-Type: application/x-zip-compressed

------WebKitFormBoundaryuFNy5dZtFj7olmD5--

这适用于任何其他主要文件格式,但无法读取zip文件。如果我var_dump$_FILES或$_POST,它们是空的。

我错过了什么?为什么所有其他文件都能工作,而zip却不能。

谢谢

使用wamp和php 5.5.12

zip文件类型定义在哪里?

无论如何,假设这是上传zip文件的html表单,你会有以下内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php if($message) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label>Choose a zip file to upload: <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>

在处理帖子的服务器端脚本上:

<?php
if($_FILES["zip_file"]["name"]) {
    $filename = $_FILES["zip_file"]["name"];
    $source = $_FILES["zip_file"]["tmp_name"];
    $type = $_FILES["zip_file"]["type"];
    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach($accepted_types as $mime_type) {
        if($mime_type == $type) {
            $okay = true;
            break;
        } 
    }
    $continue = strtolower($name[1]) == 'zip' ? true : false;
    if(!$continue) {
        $message = "The file you are trying to upload is not a .zip file. Please try again.";
    }
    $target_path = "/home/var/yoursite/httpdocs/".$filename;  // change this to the correct site path
    if(move_uploaded_file($source, $target_path)) {
        //if you also wanted to extract the file after upload
        //you can do the following
        $zip = new ZipArchive();
        $x = $zip->open($target_path);
        if ($x === true) {
            $zip->extractTo("/home/var/yoursite/httpdocs/"); // change this to the correct site path
            $zip->close();
            unlink($target_path);
        }
        $message = "Your .zip file was uploaded and unpacked.";
    } else {    
        $message = "There was a problem with the upload. Please try again.";
    }
}
?>

我在基于cPanel的服务器上使用了一个与@unixmiah发布的脚本非常相似的脚本,但没有出现问题。工作得很好(现在已经一年了),但在本地使用wamp时遇到了错误"PHP,未定义索引"的问题。

这是为我工作的mod:

<?php
function rmdir_recursive($dir) {
foreach(scandir($dir) as $file) {
if ('.' === $file || '..' === $file) continue;
if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
else unlink("$dir/$file");
}
rmdir($dir);
}
if(!empty($_FILES)){
//added above to script and closing } at bottom
if($_FILES["zip_file"]["name"]) {
    $filename = $_FILES["zip_file"]["name"];
    $source = $_FILES["zip_file"]["tmp_name"];
    $type = $_FILES["zip_file"]["type"];
    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach($accepted_types as $mime_type) {
        if($mime_type == $type) {
            $okay = true;
            break;
        } 
    }
    $continue = strtolower($name[1]) == 'zip' ? true : false;
    if(!$continue) {
        $message = "<b>The file you are trying to upload is not a .zip file! Please try again...</b>";
    }
    $target_path = "somedir/somesubdir/".$filename;  // change this to the correct site path
    if(move_uploaded_file($source, $target_path)) {
        $zip = new ZipArchive();
        $x = $zip->open($target_path);
        if ($x === true) {
            $zip->extractTo("somedir/somesubdir/"); // change this to the correct site path
            $zip->close();
            unlink($target_path);
        }
        $message = "<h2>ZIP file was uploaded and content was replaced!</h2>";
    } else {    
        $message = "There was a problem with the upload. Please try again.";
    }
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>QikSoft ZIP Upper</title>
</head>
<body>
<?php if(!empty($message)) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label><h3>Upload Your ZIP:</h3> <input type="file" name="zip_file" /></label>
<br /><br />
<input type="submit" name="submit" value="START UPLOAD" />
</form>
</body>
</html>

还要注意,回波消息已更改:

<?php if(!empty($message)) echo "<p>$message</p>"; ?>

有一点不起作用,那就是文件限制。目前允许ZIP之外的其他类型。任何有修复的人,都将不胜感激。

它是echo$_FILES['pload']['tmp_name'];

试着检查并调整php.ini文件中的post_max_size值,这对我来说很有效,因为默认值是3M,将值提高到128M,一切都很好