How to retrieve uploaded file from a custom file system in Laravel? - php

I have uploaded a file to my custom file system and it just works.
Custom File System:
'private' => [
'driver' => 'local',
'root' => storage_path('app/private'),
],
This is how I upload my files:
if ($request->hasFile('chatattachment')) {
$file = request()->file('chatattachment');
$fname = date('YmdHis');
$ext = $request->chatattachment->extension();
$filename = $fname . '.' . $ext;
$file->storeAs('attachments', $filename, ['disk' => 'private']);
//Trying to get the url but it doest work:
$url = Storage::url('private/attachments/' . $filename);
}
Now, What I'm trying to do is to get the url of the newly uploaded image and return it back to the front end side. How to make it possible? I know how to create a storage:link but I don't want to expose this attachments to the public.

Related

how to not generate hash when uploading image files to ftp server with laravel?

so I have successfully uploaded the image file to the ftp server and the image file is shaped like an auto generated hash file name, so how do I make the file name I upload is the same as the image name?, because when it is saved to the database it is not hashed but the name the file is the same as the uploaded image, but on the ftp server when saving it the file name is shaped like a hash
example upload file in ftp server :
example controller :
$lampiran = Lampiran_tte::where('surat_tte_id', $surat->id);
if ($request->hasfile('lampiran_gambar')) {
$files = [];
foreach ($request->file('lampiran_gambar') as $file) {
if ($file->isValid()) {
$filename = $file->getClientOriginalName();
$file->store('/lampiranSurat', 'ftp') . '/' . $filename;
$files[] = [
'lampiran_gambar' => $filename,
];
}
}
$gambar = '';
foreach ($files as $value) {
$gambar .= $value['lampiran_gambar'].'#';
}
$gambar = substr($gambar, 0, -1);
$lampiran->update([
'isi_lampiran' => $request->isi_lampiran,
'lampiran_gambar' => $gambar,
'update_by' => Auth::user()->name,
]);
}
if it is already stored in the database,
the image file name matches the image file name that was uploaded
example in database :
The store() function is generating the unique ID value for the file. Use the storeAs() function instead which allows you to specify a filename of your choice.
$file->storeAs('/lampiranSurat', $filename, 'ftp');

How can upload videos and save and list in laravel website

I want to upload multiple videos to their site and want to list them there. Please give me a solution to how can manage this.
I want to save it in the database so that could list on the site.
This is my code but not working proper consuming more time to upload and the page is expired
$this->validate($request, [
'filename' => 'required',
'file' => 'mimes:mp4,ts|max:100040|required'
]);
dd($request->all());
if ($request->hasFile('file')) {
//upload file
$file = $request->file('file');
if(!empty($file) && $file != null)
{
$video_file = new VideoPost();
$fileExentsion = $file->extension();
$fileName = $file->getClientOriginalName();
$fileFullName = time()."_".$fileName;
$path = Str::slug($fileFullName).".".$fileExentsion;
$file->move(public_path('files/'), $path);
$fullpath = 'files/'.$path;
$video_file->file = $fullpath;
$video_file->file_name = $request->file_name;
$video_file->save();
}
}

Save and retrieve file in Laravel

I have difficulty understanding the file save and retrieval in Laravel. I managed to get the file saved into correct path
$fileNameWithExt = $request->file('Agreement_file')->getClientOriginalName();
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extention =$request->file('Agreement_file')->getClientOriginalExtension();
$filenameToStore = $fileName . '_' . $lab_id. '.'.$extention;
$request->Agreement_file->storeAs('agreements', $filenameToStore );
However not I want to create an a-tag to download the file, but cannot manage to get to download the file.
{{$filenameToStore}}
The file download but I get the error "Failed- Server Problem". I do not want to use a same link as these files are confidential and should not be able to be downloaded outside the app.
Something like that?
Step 1:
Create table files id | filename | user_id.
Step 2:
Create model File
Step 3:
Add file row in table.
$fileNameWithExt = $request->file('Agreement_file')->getClientOriginalName();
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extention =$request->file('Agreement_file')->getClientOriginalExtension();
$filenameToStore = $fileName . '_' . $lab_id. '.'.$extention;
$request->Agreement_file->storeAs('agreements', $filenameToStore );
File::create([
'filename' => $filenameToStore,
'user_id' => Auth::id()
]);
Step 4:
Create controller method download.
public function download(){
$filename = $request->input('filename');
$file = File::where('user_id', Auth::id())
->where('filename', $filename)
->firstOrFail();
$path = Storage::path('agreements/' . $filename);
if(Storage::exists($path)){
return Response::download($path, $filename);
}
}
Step 5:
Replace your link:
{{$filenameToStore}}
Or you can build it based on the file ID.

Upload File/Image with class Storage Laravel 5.2

First, I'm sorry for my bad English.
I want to upload a file/image from my driver to my project directory using class Storage. I want that every file/image will be uploaded/moved to my public/img directory. I use Form::file('img') on my views and on my post controller, I write this
$img = Input::file('img');
if ($img !== null) {
$filename = $img->getClientOriginalName();
Storage::disk('uploads')->put('filename', $filename);
$jenis->img = $filename;
}
and on my config/filesystem I write this
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/img',
],
But, nothing happen on my public/img directory, no new file/image on there.
Can u help me whats wrong with my code?
and I hope u guys can help me with another good way on how to upload a file/image in laravel
Looks like your problem is you're not storing the file, you're referencing its name not its contents.
Try this:
Storage::disk('uploads') -> put($filename, file_get_contents($img -> getRealPath()));
In my filesystem file I configure my image directory in this way:
'uploads' => [
'driver' => 'local',
'root' => public_path("/img"),
],
I think that you can use your way but is another point.
To get the file from your view you should use File::get Laravel function:
$filename = $img->getClientOriginalName();
Storage::disk('uploads')->put($filename, \File::get($file));
With this would be enough, you save the file with the name of file uploaded in directory specify in filesystem.
if ($request->hasFile('original_pic')) {
$original_pic = $request->file('original_pic');
$file_extension=$original_pic>getClientOriginalExtension();
$filename = time() . '.' . $file_extension;
# upload original image
Storage::put('ArticlesImages/' . $filename, (string) file_get_contents($original_pic), 'public');
# croped image from request.
$image_parts = explode(";base64,", $request->input('article_image'));
$image_base64 = base64_decode($image_parts[1]);
Storage::put('ArticlesImages/croped/' . $filename, (string) $image_base64, 'public');
# get image from s3 or local storage.
$image_get = Storage::get('ArticlesImages/croped/' . $filename);
# resize 50 by 50 1x
$image_50_50 = Image::make($image_get)
->resize(340, 227)
->encode($file_extension, 80);
Storage::put('ArticlesImages/1x/' . $filename, (string) $image_50_50, 'public');
$file_url = Storage::url('ArticlesImages/croped/' . $filename);
return response()->json(['success' => true, 'filename' => $filename, 'file_url' => $file_url], 200);
}

Laravel: Trouble with uploading files on localhost

I've trouble getting the file upload working with Laravel on my Windows 7 system. 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.
The output of dd(sys_get_temp_dir()) is C:\Users\RAGHAV~1\AppData\Local\Temp.
However there is no directory called RAGHAV~1(I've enabled to see the hidden folders). In php.ini the upload_tmp_dir is set to 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);

Categories