我不知道这段代码有什么错误

时间:2018-07-11 10:37:32

标签: php mysql image upload

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

2 个答案:

答案 0 :(得分:2)

我已经检查了您的代码,这很好。您需要注意以下几件事。

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>

让我知道是否有帮助?

答案 1 :(得分:0)

首先,您应该发布所得到的结果以及期望的结果,也应该发布html表单代码。

两个if似乎相关,但是没有连接……也许第二个应该在第一个里面?

在第一个if中,您将错误的参数传递给move_uploaded_file方法,第二个似乎不错。

确保“目标目录”存在于正确的位置并具有正确的权限。检查apache的error_log以获取可能有用的消息。

相关问题