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

时间:2015-03-17 19:08:31

标签: php file-upload zip

我在上传zip文件时遇到问题,似乎无法找到答案。 的index.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

4 个答案:

答案 0 :(得分:0)

您的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.";
    }
}
?>

答案 1 :(得分:0)

我使用的脚本非常类似于@unixmiah发布的脚本而没有问题在我的基于cPanel的服务器上。工作得很好(并且已经有一年了)但在本地使用wamp时遇到了错误“PHP,undefined index”的问题。

这是适用于我的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以外的其他类型。任何有修复的人都会非常感激。

答案 2 :(得分:-1)

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

答案 3 :(得分:-1)

尝试检查并调整php.ini文件中的post_max_size值,这对我来说是默认的3M,将值提高到128M,一切都是桃子