【问题标题】:uploading zip files with PHP, undefined index使用 PHP 上传 zip 文件,未定义索引
【发布时间】:2015-03-17 19:08:30
【问题描述】:

我在上传 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>

转换.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

【问题讨论】:

    标签: php file-upload zip


    【解决方案1】:

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

    【讨论】:

    • 已经试过这个脚本了。它不会超过第一个 $_FILE["zip_file"],它会抛出同样的错误。
    • 在服务器端脚本中执行 print_r($_POST) 会得到什么?注释掉所有内容并尝试查看它打印的文件项。
    • 同时打印 $_POST 和 $_FILES 会返回一个空数组。
    【解决方案2】:

    我使用的脚本与@unixmiah 发布的脚本非常相似,但在基于 cPanel 的服务器上没有问题。效果很好(并且已经使用了一年),但是在本地与 wamp 一起使用时遇到了错误“PHP,未定义索引”的问题。

    这是对我有用的模组:

    <?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 之外的其他类型。任何有修复的人,不胜感激。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 'tmp_name' 用于获取它的临时位置。 'name' 将回显文件名。这是一个简单的测试,看看文件是否被正确读取。
      【解决方案4】:

      尝试检查和调整 php.ini 文件中的 post_max_size 值,这对我有用,因为默认值为 3M,将值提高到 128M,一切都很好

      【讨论】:

      • 作为评论会更好。
      • 我明白,但我遇到了类似的问题,这就是我的解决方案,如果问题偏离主题,我深表歉意,我实际上是新手,就我自己而言,我得到了一个当我尝试上传时出现类似“未定义索引”错误的空白页面,在进行调整后表示我再也没有上传 zipfile 的问题
      猜你喜欢
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-08
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      • 1970-01-01
      相关资源
      最近更新 更多