我正在尝试将图像保存在存储中。这是我的代码
public function uploadEducationalInstitutionLogo(Request $request)
{
if ($request->hasFile('logo')) {
$file = $request->file('logo');
$fileName = time() . $file->getClientOriginalName();
$img = Image::make($file);
$img->fit(820, 312, function (Constraint $constraint) {
$constraint->upsize();
});
Storage::putFileAs('institution-logo', (string)$img->encode(null, 95), $fileName);
return response()->json([
'filename' => $fileName
]);
}
}
我使用 vue filepond 组件作为我的图片上传器从 vue 前端发出的请求:
<file-pond
name="logo"
allow-multiple="false"
max-files="1"
accepted-file-types="image/jpeg, image/png"
v-bind:files="myFiles"
:label-idle="label"
v-on:init="handleFilePondInit"
v-on:processfile="onload"
:server="server"
/>
<script>
data() {
return {
myFiles: [],
server: {
url: `http://127.0.0.1:8000/api/logo`,
process: {
headers: {
Authorization: localStorage.getItem("access_token"),
},
},
},
};
},
<script>
这里抛出以下错误:
ErrorException: fopen() 期望参数 1 是有效路径,文件中给出的字符串...
我想将我的照片存储在storage/images/institution-logo/image.jpg
另一个问题是最好保存在哪里:
1)存储/图像/..
或
2)公共/存储/图像/... ?
提前致谢!
答案 0 :(得分:1)
$data = $request->all();
foreach ($data['logo'] as $imgFile) {
$imgName = 'logo_' . random_int(111111, 999999) . '.' . $imgFile->getClientOriginalExtension();
$imgFile->move(storage_path('/images/', $imgName);
}
使用 move(storage_path);