无法将img上传到文件夹

时间:2017-10-09 02:38:25

标签: php laravel laravel-5

我正在使用laravel。并且img不会上传到我的public / imgs文件夹。它一直说抱歉,上传文件时出错。

// PHP CODE
$target_dir = "public/imgs/";
$target_file = $target_dir . basename($_FILES["img"]["name"]);


if (move_uploaded_file($_FILES["img"]["name"], $target_file)) {
     echo "success";
}
else {
    echo "Sorry, there was an error uploading your file.";
}

// HTML CODE
<form method="POST" action="/listings" enctype="multipart/form-data">
    <input type="file" name="img">
</form>

3 个答案:

答案 0 :(得分:0)

也许您应该将原来的move_uploaded_file功能更改为

move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)

See the diff between name and tmp_name

答案 1 :(得分:0)

首先确保上传目录是现有的和可写的;然后在php.ini中启用了file_uploads

答案 2 :(得分:0)

<form method="POST" action="/listings" enctype="multipart/form-data">
    <input type="file" name="img">
</form>

HTML

$target_dir = "public/imgs/";
$target_file = $target_dir . basename($_FILES["img"]["name"]);
if ($_FILES['img']['error'] == 0) {
    if (move_uploaded_file($_FILES['img']['tmp_name'], $target_file)) {
        echo 'success';
    } else {
        echo 'upload failed<br>';
        echo '<pre>';
        echo 'tmp name: ';
        print_r($_FILES['img']['tmp_name']);
        echo "\n target: ".$target_file;
        echo "\n\n";
        print_r($_FILES['img']);
        echo '</pre>';
    }
} else {
    echo 'upload failed: ' . $_FILES['img']['error'];
}

PHP

此代码应该可以工作并显示所有数据以进行调试。