在PHP中上传文件时的未定义索引

时间:2016-12-20 09:55:51

标签: php file-upload

enter image description here

这两行出现问题:

$product_image =  $_FILES['product_image']['name'][0];

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

以下是代码:

<?php

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

      $product_title = $_POST['product_title'];
      $product_cat = $_POST['product_cat'];
      $product_brand = $_POST['product_title'];
      $product_price = $_POST['product_price'];
      $product_desc = $_POST['product_desc'];
      $product_keywords = $_POST['product_keywords'];

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

      move_uploaded_file($product_image_tmp,"product_images/$product_image");

       echo $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($con,$insert_product);

     if($insert_pro){
         echo "<script>alert('Product Has Been Inserted')</script>";
         echo "<script>window.open('insert_product.php','_self')</script>";

     }

    }
?>

2 个答案:

答案 0 :(得分:0)

  1. 确保您已将enctype="multipart/form-data"添加到form元素

  2. 并确保您的文件类型输入名为product_image

答案 1 :(得分:0)

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

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

试试吧