3 回答

TA貢獻1848條經驗 獲得超10個贊
我遇到過類似的問題并通過以下方式解決了
要將文件上傳到任何路徑,請按照以下步驟操作。
創建一個新磁盤config/filesystem.php并將其指向您喜歡的任何路徑,例如D:/test或其他
'disks' => [
// ...
'archive' => [
'driver' => 'local',
'root' => 'D:/test',
],
// ...
請記住archive磁盤名稱,您可以將其調整為任何名稱。之后做config:cache
然后將文件上傳到指定目錄執行類似的操作
$request->file("image_file_identifier")->storeAs('SubFolderName', 'fileName', 'Disk');
例如
$request->file("image_file")->storeAs('images', 'aa.jpg', 'archive');
現在要獲取文件,請使用以下代碼
$path = 'D:\test\images\aa.jpg';
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;

TA貢獻1820條經驗 獲得超9個贊
我通常直接將圖像保存到公共文件夾這是我的項目之一的工作代碼
$category = new Categories;
//get icon path and moving it
$iconName = time().'.'.request()->icon->getClientOriginalExtension();
// the icon in the line above indicates to the name of the icon's field in
//front-end
$icon_path = '/public/category/icon/'.$iconName;
//actually moving the icon to its destination
request()->icon->move(public_path('/category/icon/'), $iconName);
$category->icon = $icon_path;
//save the image path to db
$category->save();
return redirect('/category/index')->with('success' , 'Category Stored Successfully');
經過一些修改以適應您的代碼,它應該可以正常工作

TA貢獻1845條經驗 獲得超8個贊
我希望你在這里使用圖像干預。如果沒有,您可以運行命令:
? ? ? composer require intervention/image
這將對您的項目進行干預?,F在使用以下代碼將圖像上傳到本地。
您可以這樣調用該函數來保存圖像:
if ($request->hasFile('image')) {
? ?$image = $request->file('image');
? ?//Define upload path
? ?$destinationPath = public_path('/storage/img/bathroom/');//this will be created inside public folder
? ?$filename = round(microtime(true) * 1000) . "." .$image->getClientOriginalExtension();
? //upload file
? $this->uploadFile($image,$destinationPath,$filename,300,300);
//put your code to Save In Database
//just save the $filename in the db.?
//put your return statements here? ??
}
? ??
public function? uploadFile($file,$destinationPath,$filename,$height=null,$width=null){
? ??
//create folder if does not exist
if (!File::exists($destinationPath)){
? ?File::makeDirectory($destinationPath, 0777, true, true);
}
? ??
//Resize the image before upload using Image? ? ? ? Intervention
? ? ? ? ? ??
if($height!=null && $width!=null){
? ?$image_resize = Image::make($file->getRealPath());
? ?$image_resize->resize($width, $height);
? ?//Upload the resized image to the project path
? ?$image_resize->save($destinationPath.$filename);
}else{
? ? ?//upload the original image without resize.?
? ? ?$file->move($destinationPath,$filename);
? ? ?}
}
如果您無論如何想使用 Storage Facade,我已經在使用 Storage::put() 之前使用 Image->resize()->encode() 修改了您的代碼。請看看下面的代碼是否有效。(抱歉,我沒時間測試)
public function addPicture(Request $request, $id)
{
? $bathroom = Bathroom::withTrashed()->findOrFail($id);
? $validatedData = Validator::make($request->all(), [
? ? 'afbeelding' =>'required|image|dimensions:min_width=400,min_height=400']);
if($validatedData->fails())
{
? ? return Response()->json([
? ? ? ? "success" => false,
? ? ? ? "errors" => $validatedData->errors()
? ? ]);
}
if ($file = $request->file('afbeelding')) {
? ? $img = Image::make($file);
? ? $img->resize(3000, 3000, function ($constraint) {
? ? ? ? $constraint->aspectRatio();
? ? ? ? $constraint->upsize();
? ? });
? ? //Encode the image if you want to use Storage::put(),this is important step
? ? $img->encode('jpg',100);//default quality is 90,i passed 100
? ? $uid = Str::uuid();
? ? $fileName = Str::slug($bathroom->name . $uid).'.jpg';
? ? $this->createImage($img, 3000, "high", $bathroom->id, $fileName);
? ? $this->createImage($img, 1000, "med", $bathroom->id, $fileName);
? ? $this->createImage($img, 700, "thumb", $bathroom->id, $fileName);
? ? $this->createImage($img, 400, "small", $bathroom->id, $fileName);
? ? $picture = new Picture();
? ? $picture->url = '-';
? ? $picture->priority = '99';
? ? $picture->alt = Str::limit($bathroom->description,100);
? ? $picture->margin = 0;
? ? $picture->path = $fileName;
? ? $picture->bathroom_id = $id;
? ? $picture->save();
? ? return Response()->json([
? ? ? ? "success" => true,
? ? ? ? "image" => asset('/storage/img/bathroom/'.$id.'/small/'.$fileName),
? ? ? ? "id" => $picture->id
? ? ]);
}
return Response()->json([
? ? "success" => false,
? ? "image" => ''
]);
}
? public function createImage($img, $size, $quality, $bathroomId,$fileName){
$img->resize($size, $size, function ($constraint) {
? ? $constraint->aspectRatio();
? ? $constraint->upsize();
});
Storage::put(? $this->getUploadPath($bathroomId, $fileName, $quality), $img);
}
public function? getUploadPath($bathroom_id, $filename, $quality ='high'){
$returnPath = asset('/storage/img/bathroom/'.$bathroom_id.'/'.$quality.'/'.$filename);
return $returnPath; //changed echo to return
}
使用來源:Intervention repo,Github
另請注意,Storage 的put方法適用于圖像干預輸出,而putFile方法適用于 Illuminate\Http\UploadedFile 以及 Illuminate\Http\File 和實例。
- 3 回答
- 0 關注
- 195 瀏覽
添加回答
舉報