How do I set the individual upload file names? - laravel-5

I have a Laravel 5.3 app that has a form which users can upload multiple files using multiple file fields. The form work in that the files can be uploaded and moed to the destinationPath as I expect but I can't seem to change each of the files 'filename' values. It keeps saving the filename value as the php**.tmp.
Here is the foreach in my controller;
$files = $request->files;
foreach($files as $file){
$destinationPath = 'images/forms'; // upload path
$filename = $file->getClientOriginalName(); // get image name
$file->move($destinationPath, $filename); // uploading file to given path
$file->filename = $filename;
}
If I dd($filename) and dd($file->filename) within the foreach I do get the value (original name) I am looking for but if I dd($files) outside that foreach, the filename is set as the temp php value.
What am I missing? Thanks.
EDIT
The file object looks like this;
-test: false
-originalName: "sample_header_1280.png"
-mimeType: "image/png"
-size: 51038
-error: 0
path: "C:\xampp\tmp"
filename: "php7240.tmp"
basename: "php7240.tmp"
pathname: "C:\xampp\tmp\php7240.tmp"
extension: "tmp"
realPath: "C:\xampp\tmp\php7240.tmp"
I am trying to save the originalName to the db but it seems to default to saving the filename.

Turns out using a foreach for Input::file is not he approach here. If uploading multiple files from the same field - then you'd use a foreach to loop, move and save.
To upload files from multiple file inputs on the same form all you need to do is treat each input individually - as you might with any other form.
In my example I did this in my controller;
$data['image1'] = Input::file('image1')->getClientOriginalName();
Input::file('image1')->move($destinationPath, $data['image1']);
$data['image2'] = Input::file('image2')->getClientOriginalName();
Input::file('image2')->move($destinationPath, $data['image2']);
Not sure this is the best approach (there's always another way) but it worked for me.

Related

How do create a file from ascii, then create a link to download the file with Laravel

I'm using a shipping api, that has a method that spits out a pdf in this format (ascii, I believe):
%PDF-1.3\n1 0 obj\n
<<\n
/Type /Pages\n
/Count 1\n
many lines removed here
startxref\n
982\n
%%EOF\n
How do I..
Convert this code into a downloadable .pdf file?
Create a link to download the file?
Note - I do not need to store the file.
ADDENDUM
So, this is moving the correct file to the correct place, but is giving me an error "Call to a member function move() on string".
$pdf_raw = $this->create_label2($data->label_url);
$filename = 'label'.auth()->user()->id.'.pdf';
file_put_contents($filename, $pdf_raw);
$filename->move(public_path().'/img/pdf', $filename);
How can that be?
Here is the solution:
use Illuminate\Support\Facades\Storage;
$pdf_raw = $this->create_label2($data->label_url); //this is raw ascii data
$filename = 'label'.auth()->user()->id.'.pdf'; //can be any name .pdf
Storage::disk('pdf')->put($filename,$pdf_raw); //creates file and moves to correct path
$pdf_link = "/mypath/$filename"; //url to file

laravel how to save files with predefined name

in laravel controller i am trying to give each video file i uploading should be rename as
**myfile.mp4**
and save in public folder.
but my present code makes random number for my files but i need to give name as myfile
my controller
$input['file_id'] = time() . '.' . $request->file_id->getClientOriginalExtension();
$folder1 = public_path('/public');
$path1 = $folder1 . $input['file_id']; // path 1
$request->file_id->move($folder1, $input['file_id']);
What you want is :
$input['file_id'] = 'myfile.'.$request->file_id->getClientOriginalExtension();
But you cannot give the same name 'myfile' to all your videos because each recording will be overwritten by the previous one, you need unique names.
For this you can do, for example:
$input['file_id'] = 'myfile'.time().'.'.$request->file_id->getClientOriginalExtension();
// here, time() represents the time at which the video was saved in your '/public' file
or
$input['file_id'] = 'myfile'.date().'.'.$request->file_id->getClientOriginalExtension();
// here, date() represents the date the video was saved in your '/public' file

How can I upload image using Storage::put on the laravel?

My code to upload image like this :
$file = $file->move($path, $fileName);
The code works
But I want to change it using Storage::put like this reference :
https://laravel.com/docs/5.6/filesystem#storing-files
I try like this :
Storage::put($fileName, $path);
It does not works
I'm confused, where I must put $file on the code
How can I solve this problem?
Update :
$file = file of image
$path = storage_path('/app/public/product/')
$fileName = chelsea.jpg
So I want to save the file with name chelsea.jpg on the /app/public/product/
Easy Method
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
This will automatically store the files in your default configuration.
This is another example
Storage::put($fileName, $path);
Hope this helps

Saving Intervention Image In Owners Folder in Laravel 5

I can change my code to save the uploaded image in the public dir but not when I want to their uploaded image in a folder as their company's name. For example of what works:
/public/company_img/<filename>.jpg
If the user's company name is Foo, I want this when they save save their uploaded image:
/public/company_img/foo/<filename>.jpg
This is in my controller:
$image = Input::file('company_logo');
$filename = $image->getClientOriginalName();
$path = public_path('company_img/' . Auth::user()->company_name . '/' . $filename);
// I am saying to create the dir if it's not there.
File::exists($path) or File::makeDirectory($path); // this seems to be the issue
// saving the file
Image::make($image->getRealPath())->resize('280', '200')->save($path);
Just looking at that you can easily see what it's doing. My logs shows nothing and the browser goes blank after I hit the update button. Any ideas
File::exists($path) or File::makeDirectory($path);
This line does not make sense, as you check if a file exists and if not you want to attempt to create a folder ( in your $path variable you saved a path to a file not to a directory )
I would do something like that:
// directory name relative to public_path()
$dir = public_path("company_img/username"); // set your own directory name there
$filename = "test.jpg"; // get your own filename here
$path = $dir."/".$filename;
// check if $folder is a directory
if( ! \File::isDirectory($dir) ) {
// Params:
// $dir = name of new directory
//
// 493 = $mode of mkdir() function that is used file File::makeDirectory (493 is used by default in \File::makeDirectory
//
// true -> this says, that folders are created recursively here! Example:
// you want to create a directory in company_img/username and the folder company_img does not
// exist. This function will fail without setting the 3rd param to true
// http://php.net/mkdir is used by this function
\File::makeDirectory($dir, 493, true);
}
// now save your image to your $path
But i really can't say your behaviour has something to do with that... Without error messages, we can only guess.

Check an image if existing already in the folder before uploading - Codeginiter

Do you have any sample codes or functions to check if an image name is existing already in the folder before uploading?
I've tried using file_exists() but it doesn't work, here is my sample code:
$path = FCPATH . "images2/";
$filename=$_FILE['userfile'];
$full_path = $path .$filename;
if(file_exists($filename))
{
///display error message///
}
Here is the simplest way to check if a file exist:
if(is_file($filename){
return true; //the file exist
}else{
return false; //the file does not exist
}
I'm assuming you are not getting the correct result with file_exists() because you don't include the full path (even tho you define it).
Try using the following: file_exists($full_path)
Also consider using some CI helper functions for handling files like images, or uploads. They are there to make this 'easier'.
File helper:
http://ellislab.com/codeigniter/user-guide/helpers/file_helper.html

Resources