简体   繁体   English

Laravel:在本地主机上上传文件时遇到问题

[英]Laravel: Trouble with uploading files on localhost

I've trouble getting the file upload working with Laravel on my Windows 7 system.我在 Windows 7 系统上无法使用 Laravel 上传文件。 There is no issue with uploading the files but when I see the destination directory the uploaded file is not present.上传文件没有问题,但是当我看到目标目录时,上传的文件不存在。

After searching in Google and forums I found that there may be a problem with the 'Temp' directory.在谷歌和论坛中搜索后,我发现“Temp”目录可能有问题。

The output of dd(sys_get_temp_dir()) is C:\\Users\\RAGHAV~1\\AppData\\Local\\Temp . dd(sys_get_temp_dir())的输出是C:\\Users\\RAGHAV~1\\AppData\\Local\\Temp

However there is no directory called RAGHAV~1 (I've enabled to see the hidden folders).但是没有名为RAGHAV~1目录(我已启用查看隐藏文件夹)。 In php.ini the upload_tmp_dir is set to C:\\xampp\\tmp .php.iniupload_tmp_dir设置为C:\\xampp\\tmp

Is there any conflict between these settings?这些设置之间有冲突吗? Can you please help me to get the file upload working?你能帮我让文件上传工作吗?

The code in the controller that process the uploaded files:控制器中处理上传文件的代码:

$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,
]);

You have not specified the path for the file.您尚未指定文件的路径。 Simply replace this只需更换这个

$image_directory = public_path() . '/Uploads/Products/';

$result = $request->file('image')->move($image_directory);

with this有了这个

$file = $request->file('image');

$filename =  $file->getClientOriginalName().'.' . $file->getClientOriginalExtension();

$file->move(public_path('Uploads/Products/'), $filename);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM