拉拉维尔:在本地主机上上传文件时遇到问题


Laravel: Trouble with uploading files on localhost

我在Windows 7系统上与Laravel一起上传文件时遇到了问题。上传文件没有问题,但是当我看到目标目录时,上传的文件不存在。

在谷歌和论坛中搜索后,我发现"临时"目录可能有问题。

dd(sys_get_temp_dir())的输出是C:'Users'RAGHAV~1'AppData'Local'Temp

但是没有名为RAGHAV~1的目录(我已启用以查看隐藏文件夹)。php.ini中,upload_tmp_dir设置为 C:'xampp'tmp

这些设置之间是否存在任何冲突?你能帮我让文件上传工作吗?

控制器中处理上传文件的代码:

$validator = $this->brandValidator($request->all());
if ($validator->fails()) {
    $this->throwValidationException(
        $request, $validator
    );
}
$image_directory = public_path() . '/Uploads/Products/';
$result = $request->file('image')->move($image_directory);
$brand_name = $request->input('brand_name');
$image = $image_directory . $request->file('image')->getClientOriginalName();
$id = Brand::create([
    'brand_name' => $brand_name,
    'image' => $image,
]);

您尚未指定文件的路径。只需替换此

$image_directory = public_path() . '/Uploads/Products/';
$result = $request->file('image')->move($image_directory);

有了这个

$file = $request->file('image');
$filename =  $file->getClientOriginalName().'.' . $file->getClientOriginalExtension();
$file->move(public_path('Uploads/Products/'), $filename);