php - 我不知道这段代码有什么错误
问题描述
if(isset($_POST['submit'])) {
$img = $_FILES['image']['name'];
$target = 'img/'.basename($img);
$sql = move_uploaded_file($img, $target);
if ($sql) {
echo $img." uploaded";
}else {
echo $img." Failed";
}
}
if(!empty($_FILES['image'])) {
$path = "img/";
$path = $path . basename( $_FILES['image']['name']);
if(move_uploaded_file($_FILES['image']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['image']['name'])." has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
解决方案
我已经检查了你的代码,它非常好。您需要注意以下几件事。
1 - img 目录应该有 777 权限。2 - 正确检查您的表单标签。
下面是给你的样本。
<?php
if(!empty($_FILES['image'])) {
$img = $_FILES['image']['name'];
$target = 'img/'.basename($img);
//$sql = move_uploaded_file($img, $target); //replace $img with ($_FILES['image']['tmp_name']), you are not provide the tmp path of the image
$sql = move_uploaded_file(($_FILES['image']['tmp_name']), $target);
if ($sql) {
echo $img." uploaded";
}else {
echo $img." Failed";
}
}
?>
<form name="frm" id="id" action="" enctype="multipart/form-data" method="post">
<input type="file" name="image" id="image" >
<input type="submit" value="Upload" name="Upload">
</form>
让我知道它是否有帮助?
推荐阅读
- html - 通过 R 笔记本渲染 kable 对象时的白色字体
- python - Python、Django:原始 sql 查询的替代方案
- flutter - Flutter中如何实现多层循环ListView?
- python - AttributeError:“Line3D”对象没有属性“_verts3d”
- powerbi - 在 DAX(不是 powerquery)中,根据列删除重复项
- c# - FTPS 失败,但仅限于某些机器上的某些命令
- java - 在 Java swing 窗口中设置换行符
- java - 如何将一个 Maven 插件的输出转换为另一个?
- javascript - 更新到 Jest 26 后模拟中断
- json - 如何将地址转换为坐标以放置在 SwiftUI 中的地图上?