文件上传php脚本中的未定义索引

时间:2013-08-09 14:28:32

标签: php html forms file-upload

我正在尝试为我正在处理的网站创建一个页面,该页面允许用户为供应商上传徽标图像,同时将有关该供应商的其他信息添加到数据库中。文件名存储在mysql数据库中,图像应该上传到./graphics,以便在网站的其他地方使用。

mysql部分在这一点上运行得很好,但我似乎无法将图像上传到./graphics。

我检查了Apache错误日志,发现了以下内容......

[Fri Aug 09 07:18:50 2013] [error] [client 127.0.0.1] PHP Notice:  Undefined index: VendorLogo in /var/www/labconnect2/addvendor.php on line 72, referer: http://localhost/labconnect2/addvendor.php
[Fri Aug 09 07:18:50 2013] [error] [client 127.0.0.1] PHP Notice:  Undefined index: VendorLogo in /var/www/labconnect2/addvendor.php on line 74, referer: http://localhost/labconnect2/addvendor.php
[Fri Aug 09 07:18:50 2013] [error] [client 127.0.0.1] PHP Notice:  Undefined index: VendorLogo in /var/www/labconnect2/addvendor.php on line 75, referer: http://localhost/labconnect2/addvendor.php
[Fri Aug 09 07:18:50 2013] [error] [client 127.0.0.1] PHP Notice:  Undefined index: VendorLogo in /var/www/labconnect2/addvendor.php on line 76, referer: http://localhost/labconnect2/addvendor.php
[Fri Aug 09 07:18:50 2013] [error] [client 127.0.0.1] PHP Notice:  Undefined index: VendorLogo in /var/www/labconnect2/addvendor.php on line 77, referer: http://localhost/labconnect2/addvendor.php
[Fri Aug 09 07:18:50 2013] [error] [client 127.0.0.1] PHP Notice:  Undefined index: VendorLogo in /var/www/labconnect2/addvendor.php on line 78, referer: http://localhost/labconnect2/addvendor.php
[Fri Aug 09 07:18:50 2013] [error] [client 127.0.0.1] PHP Notice:  Undefined index: VendorLogo in /var/www/labconnect2/addvendor.php on line 79, referer: http://localhost/labconnect2/addvendor.php
[Fri Aug 09 07:18:50 2013] [error] [client 127.0.0.1] PHP Notice:  Undefined index: VendorLogo in /var/www/labconnect2/addvendor.php on line 117, referer: http://localhost/labconnect2/addvendor.php

所以我检查并仔细检查了我的表单代码并验证了表单标记中的name属性。我还检查了以确保$ _FILES正确地引用了该名称,并且它是(据我所知)。

我做了一些研究,发现这可能是php.ini中最大上传大小和最大帖子大小参数的结果,所以我仔细检查了它们,它们都设置为2米。

我确信这个“未定义的索引”错误是文件没有上传的原因,但我似乎无法找到我的代码有什么问题。

<!DOCTYPE>
<html>

<head>
    <title>Add New vendor</title>
    <link rel="stylesheet" type="text/css" href="./css/addnew.css">
</head>

<body>

    <div id="box">
        <div id="exit">
            <h2><a href="./index.php">X</a></h2>        
        </div>

        <div id="form">

            <form name="new_entry" method="post" enctype="multipart/form-data">
                Name 
                <br>
                <input type="text" name="vendor">
                <br>
                <br>
                Support Website for Vendor
                <br>
                <input type="text" name="SupportSite">
                <br>
                <br>
                Phone Number for Support
                <br>
                <input type="tel" name="SupportNum">
                <br>
                <br>
                Main Website for Vendor
                <br>
                <input type="text" name="MainSite">
                <br>
                <br>
                Notes
                <br>
                <textarea rows="4" cols="50" maxlength="255" wrap="hard" name="notes"></textarea>
                <br>
                <br>
                <br>
                <p>Upload Logo:    <input type="file" name="VendorLogo"> </p>
                <p><em>PNG images with transparent backgrounds are best.</em></p>
                <br>
                <br>
                <input type="submit">


            </form>

        </div>

    </div>

    <?php

        $con=mysqli_connect('localhost','ssctechlab','paracon','ssctechlab');

        //check connection
        if(mysqli_connect_errno()) {
            echo "failed to connect to MySQL Server: " . mysqli_connect_error();
        };

        //echo 'Connected... ' . mysqli_get_host_info($link) . "\n";


        //Image Upload
                    $allowedExts = array("gif", "jpeg", "jpg", "png");
            $temp = explode(".", $_FILES["VendorLogo"]["name"]);
            $extension = end($temp);
            if ((($_FILES["VendorLogo"]["type"] == "image/gif")
            || ($_FILES["VendorLogo"]["type"] == "image/jpeg")
            || ($_FILES["VendorLogo"]["type"] == "image/jpg")
            || ($_FILES["VendorLogo"]["type"] == "image/pjpeg")
            || ($_FILES["VendorLogo"]["type"] == "image/x-png")
            || ($_FILES["VendorLogo"]["type"] == "image/png"))
            && ($_FILES["VendorLogo"]["size"] < 20000)
            && in_array($extension, $allowedExts))
              {
              if ($_FILES["file"]["error"] > 0)
                {
                echo "Return Code: " . $_FILES["VendorLogo"]["error"] . "<br>";
                }
              else
                {
                echo "Upload: " . $_FILES["VendorLogo"]["name"] . "<br>";
                echo "Type: " . $_FILES["VendorLogo"]["type"] . "<br>";
                echo "Size: " . ($_FILES["VendorLogo"]["size"] / 1024) . " kB<br>";
                echo "Temp file: " . $_FILES["VendorLogo"]["tmp_name"] . "<br>";

                if (file_exists("upload/" . $_FILES["VendorLogo"]["name"]))
                 {
                 echo $_FILES["VendorLogo"]["name"] . " already exists. ";
                 }
                else
                 {
                 move_uploaded_file($_FILES["VendorLogo"]["tmp_name"],
                 "upload/" . $_FILES["VendorLogo"]["name"]);
                 echo "Stored in: " . "graphics/" . $_FILES["VendorLogo"]["name"];
                 }
                }
              }
            else
              {
              echo "Invalid file";
              }

    //Insert info into VendorInfo Table
        $vendor = $_POST["vendor"];
        $SupportSite = $_POST["SupportSite"];
        $SupportNum = $_POST["SupportNum"];
        $MainSite = $_POST["MainSite"];
        $notes = $_POST["notes"];
        $VendorLogo = ($_FILES["VendorLogo"]["name"]);



        mysqli_query($con,"INSERT INTO VendorInfo VALUES('','$VendorLogo','$vendor','$SupportSite','$SupportNum','$MainSite','$notes')");


        mysqli_close($con);

        //header('Location: ./index.php')

    ?>
</body>

顺便说一句,这是我从w3schools.com获得的代码。我仍然是一个总菜鸟,所以请放轻松。

1 个答案:

答案 0 :(得分:1)

这是因为您使用未设置的数组索引。在使用它之前,您需要确保它存在:

if ( ! isset($_FILES["VendorLogo"])) {
    die('VendorLogo is not set...');
}