Laravel - Could not move the file

2019-09-20 09:37发布

问题:

I'm trying to upload a file but, I getting this error : Could not move the file.

My code:

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

Error:

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

if i put $fullname = $directory . $fname . '.' . $fext; it create a folder inside \bck\ with the file name and put the file inside this new folder. But i don't want to create a folder with the file name.

Thank you

回答1:

You need to set $directory = "C:\your_project\public\uploads";

And Setting your uploads folder have write permission


Or you can try this way. Edit your disk at: config/filesystems.php

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

And handle at your Controller

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));
}