简体   繁体   English

PHP图片上传器问题

[英]PHP image uploader issues

I am having a problem with my PHP image uploader, as for some reason, when I test, despite selecting an image, it is echoing NULL , rather than the image URL. 我的PHP图像上传器遇到问题,由于某种原因,尽管测试了图像,但由于测试原因,它回显了NULL而不是图像URL。 What could be a possible reason for this? 这可能是什么原因?

if (isset($_POST['add_product'])) {
    include 'connect.php';
    $prodname = (isset($_POST['productname']) ? $_POST['productname'] : null);
    $prodtype = (isset($_POST['prodtype']) ? $_POST['prodtype'] : null);
    if (empty($_FILES['prodimage']['name'])) {
        $imageURL = "NULL";
        echo $prodname."<br>".$prodtype."<br>".$imageURL;
    } else if (!empty($_FILES['prodimage']['name'])) {
        if(move_uploaded_file($_FILES['prodimage']['tmp_name'], "../media/images/" .$_FILES['prodimage']['name']))
        $imageURL = "media/images/" .$_FILES['prodimage']['name'];
        echo $prodname."<br>".$prodtype."<br>".$imageURL;
    }
 }

The HTML code: HTML代码:

<h2>Add Products</h2>
<form action='' method="POST">
    <div class='products'>
        <h3>Product Name</h3>
        <input type='text' name='productname' class='title' placeholder='' value=''/>
    </div>
    <div class='products'>
        <h3>Product  Type</h3> 
        <div class='styled-select'>
            <select name='prodtype'><OPTION VALUE='1'>MARMORSKIVOR</OPTION>
                <OPTION VALUE='2'>GRANIT</OPTION>
            </select>
        </div>
    </div>
    <div class='products'>
        <h3>Add image</h3>
        <label class="myLabel" id="prodimage">
            <input type="file" name="prodimage" id="fileToUpload" />
            <span>Select Image</span>
        </label>
    </div>
    <div class="products">
        <button class='add_product' type='submit' name='add_product'>Add Product</button>
    </div>
</form>

We're dealing with files here, so you need to add a proper enctype to your form. 我们在这里处理文件,因此您需要在表单中添加适当的enctype。

<form enctype="multipart/form-data" action='' method="POST">

As per the manual: 按照手册:

Error reporting would have helped you here. 错误报告将在这里为您提供帮助。

However, you are using 2 different path declarations here. 但是,您在这里使用2个不同的路径声明。

  • "../media/images/"
  • $imageURL = "media/images/"

You need to check which one exactly it is that you want to use. 您需要检查要使用的是哪一个。

  • Only you know what that is and one of those is failing, or both. 只有您知道那是什么,而其中之一却失败了,或者两者都失败了。

If you are running your script from your root and that the folders are direct sub-folders of it, then you will need to use $imageURL = "media/images/" . 如果您从根目录运行脚本,并且文件夹是其直接子文件夹,则需要使用$imageURL = "media/images/"

Ie: 即:

- root
  -> public/your_file.php
     - media
     - images
  -> other_folder_in_public

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM