首页 > 解决方案 > 我不知道这段代码有什么错误

问题描述

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!";
    }
}

标签: phpmysqlimageupload

解决方案


我已经检查了你的代码,它非常好。您需要注意以下几件事。

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>

让我知道它是否有帮助?


推荐阅读