无法上传图片 laravel
Cannot upload images laravel
我在我的服务器上上传了我的文件,但我无法通过托管公司将我的根文件夹更改为 public,所以我将文件从 public 移动到 httpdocs 目录,但是我有现在上传图片有问题,我做了这条路
$this->validate($request, [
'image' => 'image|nullable|max:1999'
]);
if ($request->hasFile('image')) {
$filenameWithExt = $request->file('image')->getClientOriginalExtension();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . Carbon::today()->toDateString() . '.' . $extension;
$request->file('image')->storeAs('/../Supplier/public/images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
当我提交表单时出现此错误
Path is outside of the defined root, path:
为了在上传后显示图像,我在 html
中有此代码
<td><a download="retourmelding_{{$retour->firmaname}}" href="/storage/images/{{$retour->images}}" title="Foto">
<img alt="Foto" src="/storage/images/{{$retour->images}}">
</a></td>
但在本地它完美运行
我会在 "config \ filesystems.php" 中更改磁盘并放置如下内容:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
请试一试:
$request->file('image')->storeAs(storage_path('images'), $fileNameToStore);
您的目录定义不正确。
../httpdocs/storage/images/
意思是 [laravel directory]/httpdocs/storage/images/
使用助手进行目录定义:Helpers - Laravel
您可以使用以下代码执行此操作:
在config/filesystem.php
'my_file' => [
'driver' => 'local',
'root' => storage_path(),
],
在控制器中
$fileNameToStore = $request->file('myfile');
$name = $fileNameToStore->getClientOriginalName();
Storage::disk('my_file')->PutFileAs('images', $fileNameToStore, $name);
用于使用路由检索视图文件中的图像。因为直接无法访问storage/images
文件夹。
您需要在控制器中创建一个新函数。
use Auth, Storage, File, Response;
public function displayImage($filename)
{
$path = storage_path('images/' . $filename);
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
}
新路由添加
Route::get('image/{filename}', 'YOURCONTROLLER@displayImage')->name('image.displayImage');
查看(检索图像)
<img src="{{ route('image.displayImage',$image) }}" alt="" title="">
我在我的服务器上上传了我的文件,但我无法通过托管公司将我的根文件夹更改为 public,所以我将文件从 public 移动到 httpdocs 目录,但是我有现在上传图片有问题,我做了这条路
$this->validate($request, [
'image' => 'image|nullable|max:1999'
]);
if ($request->hasFile('image')) {
$filenameWithExt = $request->file('image')->getClientOriginalExtension();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . Carbon::today()->toDateString() . '.' . $extension;
$request->file('image')->storeAs('/../Supplier/public/images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
当我提交表单时出现此错误
Path is outside of the defined root, path:
为了在上传后显示图像,我在 html
中有此代码<td><a download="retourmelding_{{$retour->firmaname}}" href="/storage/images/{{$retour->images}}" title="Foto">
<img alt="Foto" src="/storage/images/{{$retour->images}}">
</a></td>
但在本地它完美运行
我会在 "config \ filesystems.php" 中更改磁盘并放置如下内容:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
请试一试:
$request->file('image')->storeAs(storage_path('images'), $fileNameToStore);
您的目录定义不正确。
../httpdocs/storage/images/
意思是 [laravel directory]/httpdocs/storage/images/
使用助手进行目录定义:Helpers - Laravel
您可以使用以下代码执行此操作:
在config/filesystem.php
'my_file' => [
'driver' => 'local',
'root' => storage_path(),
],
在控制器中
$fileNameToStore = $request->file('myfile');
$name = $fileNameToStore->getClientOriginalName();
Storage::disk('my_file')->PutFileAs('images', $fileNameToStore, $name);
用于使用路由检索视图文件中的图像。因为直接无法访问storage/images
文件夹。
您需要在控制器中创建一个新函数。
use Auth, Storage, File, Response;
public function displayImage($filename)
{
$path = storage_path('images/' . $filename);
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
}
新路由添加
Route::get('image/{filename}', 'YOURCONTROLLER@displayImage')->name('image.displayImage');
查看(检索图像)
<img src="{{ route('image.displayImage',$image) }}" alt="" title="">