php - 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?
解决方案
推荐阅读
- c++ - 以抽象类为声明的基于范围的 for 循环 C++
- python - 有效地指定具有相同前缀的多个列名称
- azure-service-fabric - Microsoft.Azure.ServiceBus.ServiceBusException - 向 Azure 服务总线主题队列发送消息
- c++ - C++如何运行多个可以随时调用的后台函数线程?
- c# - 你应该调用 HttpRequest.Abort() 吗?
- visual-studio-2012 - 为“_MSC_VER”检测到错误 LNK2038 不匹配:值“1700”与 xxxx.obj 中的值“1900”不匹配
- javascript - 在 React 中做两件事 onClick
- java - 为了安全起见,将 new String(char[]) 作为密码参数传递给只接受字符串作为参数的方法是否安全?
- entity-framework - EntityFramework Core Timeout 已过期。操作完成前超时时间已过或服务器无响应
- java - 保存前重命名多部分文件