move_uploaded_file php没有上传图片

时间:2016-04-02 23:30:02

标签: php

我的move_uploaded_file函数有问题。问题是,当我选择要上传的图像时,它不会上传到product_images中。 请帮我理清我的代码块。

这是我的HTML代码:

<form> action="insert_product.php" method="post" enctype="multipart/form-data">
</form>

这是我的php:

<?php

if(isset ($_POST['insert_post'])) {


    // Getting the text data from the fields
    $product_title = $_POST['product_title'];
    $product_cat = $_POST['product_cat'];
    $product_brand = $_POST['product_brand'];
    $product_price = $_POST['product_price'];
    $product_desc = $_POST['product_desc'];
    $product_keywords = $_POST['product_keywords'];

    // Getting the image from the field

    $product_image = $_FILES['product_image']['name'];
    $product_image_tmp = $_FILES['product_image']['tmp_name'];

    $root = "product_images";


    move_uploaded_file($product_image_tmp,$root.$product_image);

    $insert_product = "insert into products (product_cat,product_brand,product_title,product_price,product_desc,product_image,product_keywords) "
        . "values ('$product_cat','$product_brand',' $product_title','$product_price','$product_desc','$product_image','$product_keywords')";


    $insert_pro = mysqli_query($link, $insert_product);

    if($insert_pro){

        echo "<script>alert('Product has been inserted!')</script>";
        echo "<script>window.open('insert_product.php','_self')</script>";

    }

}

1 个答案:

答案 0 :(得分:0)

我可以看到两个问题:

首先你错过了一个目录分隔符(/或\)

move_uploaded_file($product_image_tmp, $root.$product_image);

所以改为

move_uploaded_file($product_image_tmp, $root.DIRECTORY_SEPARATOR.$product_image);
// the php constant DIRECTORY_SEPARATOR is practical 
// if you're unaware on what system/os that script might be running

第二,该目录可能尚不存在,因此请检查并创建它,如果不存在(在移动之前):

if(!is_dir($root)) { 
    mkdir($root, 0755, true); // third parameter isn't important in your case though)
}