-1

我正在尝试为我正在处理的网站创建一个页面,该页面允许用户上传供应商的徽标图像,同时将有关该供应商的其他信息添加到数据库中。文件名存储在 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

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

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

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

<!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 获得的代码。我还是个菜鸟,所以请放轻松。

4

1 回答 1

1

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

if ( ! isset($_FILES["VendorLogo"])) {
    die('VendorLogo is not set...');
}
于 2013-08-09T14:34:43.103 回答