upload file in public laravel - laravel

Hello guys when I use file upload and save link in database it save this "/tmp/phpkPhygr|public/allFiles/files/26657d5ff9020d2abefe558796b99584/69adc1e107f7f7d035d7baf04342e1ca.gif" so why "tmp/php..." show in this link or I want to remove this to work this url.
$files = array();
if($files = $request->file('files')){
foreach($files as $file){
$fileName = md5(rand(100,1000)) . "." . strtolower($file->getClientOriginalExtension());
$folder='public/allFiles/files/' . md5(rand(100,10));
$file_url = $folder . "/" . $fileName;
$file->move($folder, $fileName);
$files[] = $file_url;
}
}
$filesReq = $files;
if($filesReq == ' '){
$filesReq = 'no files';
}else{
$filesReq = implode('|', $files);
}

Related

How to read file Laravel?

I load file using this:
public function fileUpload(Request $req)
{
$req->validate([
'file' => 'required|mimes:csv,txt,xlx,xls,pdf|max:2048'
]);
$fileModel = new File();
if ($req->file()) {
$fileName = time() . '_' . $req->file->getClientOriginalName();
$filePath = $req->file('file')->storeAs('uploads', $fileName, 'public');
$fileModel->name = time() . '_' . $req->file->getClientOriginalName();
$fileModel->file_path = '/storage/' . $filePath;
$fileModel->save();
$this->read($fileModel->file_path);
return back()
->with('success', 'File has been uploaded.')
->with('file', $fileName);
}
}
Then I tried to read file after upload file:
public function read($path)
{
$file = FileStorage::get($path);
dd($file);
}
But I get this error:
File does not exist at path /storage/uploads/1654468183_test.csv.
How to specify path properly?
I would do this way:
if ($req->file()) {
$fileName = time() . '_' . $req->file->getClientOriginalName();
$filePath = $req->file('file')->storeAs('uploads', $fileName, 'public');
$fileModel->name = $fileName; //<--set the right filename
$fileModel->file_path = '/storage/app/public/' . $filePath;
$fileModel->save();
$this->read($fileModel->file_path);
return back()
->with('success', 'File has been uploaded.')
->with('file', $fileName);
}
change this line
$filePath = $req->file('file')->storeAs('uploads', $fileName, 'public');
to this
$filePath = $req->file('file')->storeAs('uploads/', $fileName, 'public');

how to add uniqid in image upload in laravel

I have a function to insert data in a table and this data includes image/file . I think my form didn't have problem but after I added this uniqid(), it can't be submitted with error "Call to a member function getClientOriginalExtension() on null"
public function store_pelatihan(Request $request)
{
$this->validate($request,[
// 'title' => 'required|min:5',
// 'description' => 'required|min:5|max:14'
] );
if($request->hasfile('file_scan'))
{
$file = $request->file('file_scan');
$name=$file->getClientOriginalName();
$extension = $request->image->getClientOriginalExtension();
$fileName = $file.'.'.uniqid().'.'.$extension;
$file->move(public_path().'/files/', $fileName);
$data = $fileName;
}
$users = new Master_seminar_pelatihan;
$users->user_id = $request->user_id ;
$users->nama_pelatihan = $request->nama_pelatihan ;
$users->nomor_pelatihan = $request->nomor_pelatihan ;
$users->tanggal = $request->tanggal ;
$users->uraian = $request->uraian ;
$users->tempat = $request->tempat ;
$users->file_scan = $data;
dd($data);
// $users->save();
// return redirect ('pelatihan')->with('success', 'Input Succes');
}
Previously, I was running this code and it was running without error:
$extension = $request->image->getClientOriginalExtension();
$fileName = $file.'.'.uniqid().'.'.$extension;
change this line :
$request->image->getClientOriginalExtension()
to :
$file->getClientOriginalExtension()
If you're concatenating with '.' before uniqid() it'll take extension so either you use '-' or '_'.
$fileName = $file.'_'.uniqid().'.'.$extension;
Or
$fileName = $file.'-'.uniqid().'.'.$extension;
Check file isValid() first
Get getClientOriginalExtension() of the $file (not $request->image)
Do not concatenate $file (it is UploadedFile) variable with strings. $name should be instead
if ($request->hasfile('file_scan') && $request->file('file_scan')->isValid()) {
$file = $request->file_scan;
$name = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$fileName = $name . '.' . uniqid() . '.' . $extension;
$file->move(public_path() . '/files/', $fileName);
$data = $fileName;
}

Laravel saves temp file as image

I have 3 image field for my products, on save method all working fine, but in update method first image saves temp file in database and the other two update without issue.
Code
Here I share all my 3 images on update method:
//saves temp file instead of file name
if ($request->hasFile('imageOne')) {
$imageOne = $request->file('imageOne');
$filename = 'productone' . '-' . time() . '.' . $imageOne->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename);
Image::make($imageOne)->resize(1200, 600)->save($location);
if(!empty($product->imageOne)){
Storage::delete('images/' . $product->imageOne);
}
$product->imageOne = $imageOne;
}
//works with no issue
if ($request->hasFile('imageTwo')) {
$imageTwo = $request->file('imageTwo');
$filename = 'producttwo' . '-' . time() . '.' . $imageTwo->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename);
Image::make($imageTwo)->resize(1200, 600)->save($location);
if(!empty($product->imageTwo)){
Storage::delete('images/' . $product->imageTwo);
}
$product->imageTwo = $filename;
}
//works with no issue
if ($request->hasFile('imageThree')) {
$imageThree = $request->file('imageThree');
$filename = 'productthree' . '-' . time() . '.' . $imageThree->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename);
Image::make($imageThree)->resize(1200, 600)->save($location);
if(!empty($product->imageThree)){
Storage::delete('images/' . $product->imageThree);
}
$product->imageThree = $filename;
}
Any idea?
Issue is here:-
$product->imageOne = $imageOne;
You are saving $imageOne. But you need to save $filename. Use following code:-
$product->imageOne = $filename;

Laravel deploy: storage image doesn't work correctly

After deploying my laravel project from local to Apache web server, all works correctly except images link. Here the code:
Images are stored in:
storage/app/public/photos
after i've run command:
php artisan storage:link
Images are linked at:
public/storage/photos
Controller:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photo = $file->storeAs('public/photos', 'foto-' . time() . '.' . $extension);
$user->photo = $photo;
$user->save();
}
Images are uploaded correctly on storage/app/public/photos and correctly linked in public/storage/photos but it doesn't display on frontend.
in blade, i've tried to use Storage::url to retrieve the path
{{Storage::url($user->photo)}}
and asset()
{{asset($user->photo)}}
in both cases, image doesn't exist
The public path of image is:
http://mywebsite.com/storage/photos/foto-1522914164.png
You should Use url function to show your image like below way.
url($user->photo);
I'd suggest to change the controller code as follows:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photoFileName = 'foto-' . time() . '.' . $extension;
$photo = $file->storeAs('public/photos', $photoFileName);
$user->photo = 'photos/' . $photoFileName;
$user->save();
}
Then you can use {{asset($user->photo)}} in your blade.
on my webspace, it seems that the only way to display image correctly is to create a custom route that read and serve the image.
i'm solved like this:
i'm storing only image name in db:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photoFileName = 'photo-' . $model->id . '.-' . time() . '.' . $extension;
$photo = $file->storeAs('public/photos', $photoFileName);
$store = $photoFileName;
}
then, i've create custom route that read images and display them:
Route::get('storage/{filename}.{ext}', function ($filename, $ext) {
$folders = glob(storage_path('app/public/*'), GLOB_ONLYDIR);
$path = '';
foreach ($folders as $folder) {
$path = $folder . '/' . $filename . '.' . $ext;
if (File::exists($path)) {
break;
}
}
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;
});
in blade, i'm using Storage to display image:
{{ Storage::url($photo->photo) }}}

i m saving the multiple images at a single time with differnt ID in database in Laravel

and it is saved but the issue is all the images have different auto
increment ids in database i want to save the images with the same ids
which is save at the same time,kindly guide me.
****strong text** the code is below;**
$field = Input::file('image');
$location = public_path('uploads/');
foreach ($field as $k => $fd) {
$filename = str_random(2).'-' . time() . "." . $fd->getClientOriginalExtension();
$fd->move($location , $filename);
$product->image = $filename;
}
$user->save();
I think bellow code helps you..
You can try.
$picture = '';
if ($request->hasFile('image')) {
$files = $request->file('image');
foreach($files as $file){
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$picture = date('His').$filename;
$destinationPath = base_path() . '\public\images';
$file->move($destinationPath, $picture);
}
}
if (!empty($product['images'])) {
$product['images'] = $picture;
} else {
unset($product['images']);
}

Resources