Laravel - 无法移动文件

时间:2017-05-16 12:52:02

标签: php laravel file-upload

我正在尝试上传文件但是,我收到此错误:Could not move the file

我的代码:

$directory = "C:\bck\";
$fname = pathinfo(Input::file('file')->getClientOriginalName(), PATHINFO_FILENAME);
$fext = Input::file('file')->getClientOriginalExtension();
$fullname = $directory;
Input::file('file')->move($fullname, $fname);

错误:

Could not move the file "C:\xampp\tmp\php6D32.tmp" to "C:\bck\aaaa" ()

如果我放$fullname = $directory . $fname . '.' . $fext;它会在文件名\bck\内创建一个文件夹并将文件放在这个新文件夹中。但我不想创建一个文件名的文件夹。

谢谢

1 个答案:

答案 0 :(得分:1)

您需要设置$directory = "C:\your_project\public\uploads";

设置uploads文件夹具有写入权限

enter image description here

或者您可以尝试这种方式。在以下位置修改您的磁盘:config/filesystems.php

'disks' => [
    'uploads' => [
        'driver' => 'local',
        'root'   => public_path() . '/uploads',
    ],
    'custom' => [
        'driver' => 'custom',
        'root'   => 'c:/bck',
    ]
]

并在您的控制器上处理

public function uploadFile(Request $request) {
    $data   = $request->all();
    $fileUpload = $data['file'];
    $originalFileName = $fileName->getClientOriginalName();
    Storage::disk('uploads')->put($originalFileName, file_get_contents($fileUpload));
}

public function customUploadFile(Request $request) {
    $data   = $request->all();
    $fileUpload = $data['file'];
    $originalFileName = $fileName->getClientOriginalName();
    Storage::disk('custom')->put($originalFileName, file_get_contents($fileUpload));
}