首页 > 解决方案 > How to check if user selected file for upload php

问题描述

I have a table made for updating data for admins of my page. They have a choice to update Pruduct name, price and the image. If they do not select an image the same image is kept. On the other hand, if the image is selected then the image should be uploaded into a folder. Here's my code.

<?php
if (empty($_FILES['image']['name'])) {
     echo "no file mate";
    $uploadImg = $_POST['imgpath'];
}
if ($_FILES['image']['error'] != 0) {
     echo "working";
     $folder = "Images/";
     $uploadImg = $folder . basename ($_FILES['image']['name']);
     if (move_uploaded_file($_FILES["image"]["tmp_name"], $uploadImg)) {
         echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";}
} ?>

my form :

<?php while ($row=$query->fetch()) {?>
      <tr><form action ="InsertScripts/update-product.php" method ="POST">
        <td><input type="text" name="product" value="<?php echo $row['ProductName']?>"/></td>
        <td><input type="text" name="price" value="<?php echo $row['Price']?>"/></td>
        <td><div class ="smallpic"><img src = "InsertScripts/<?php echo $row['Image']?>"></div></td>
	   <input type="hidden" name="imgpath" value="<?php echo $row['Image']?>"/>
	   <input type="hidden" name="id" value="<?php echo $row['ProductId']?>"/>
		<td><input type ="file" name="image"/></td>
		<td><button type ="submit">Update</button></td>
      </form></tr>
    <?php }?>

For some reason, it only displays "no file mate" even when i select a new image for the pruduct. Help anyone?

标签: phpfile-upload

解决方案


推荐阅读