Here is my controller code:
$images = Input::file('images');
foreach($images as $image){
$filenames = date('Y-m-d-H-i-s') . '-' . $image->getClientOriginalName();
$imageFolder = 'clients';
$image->move($imageFolder , $filenames);
echo $filenames;
}
I was trying to upload multiple image files and could do that. Now I want to save all the file names as an array or as whatever is the best in the database.
I tried to use serialize() laravel 4.2 says serialization is not allowed for uploading.
Any idea ? Thanks.
Try this ::
$images = Input::file('images');
$filenames = array();
foreach($images as $image){
$filenames [] = date('Y-m-d-H-i-s') . '-' . $image->getClientOriginalName();
$imageFolder = 'clients';
$image->move($imageFolder , $filenames);
echo $filenames;
}
Related
I tried uploading multiple files into my database but I keep getting just one file, Am using laravel 5.5 all files show on my file directory path but store only one file into my database. Here is my code
my view
<label for="image">{{ __(' image') }}</label>
<input type="file" name="image[]" class="form-control" value="{{ $employee['image'] }}" multiple>
my route
Route::post('/people/employees/test/{id}', 'EmplController#test');
my controller
if($request->hasfile('image')){
foreach ($request->image as $image) {
$path = $image->getClientOriginalName();
$filename = time() . '-' . $path;
$image->storeAs('employees', $employee->id . '/' . $filename);
$image->move(public_path('employees'),$filename);
i inserted image here->$employee->image = $filename;
$employee->save();
}
}
Make an array, and implode it before insert to table, like :
$files = []; // an empty array
foreach($a as $b) {
$files[] = $b->name; // insert name to array
}
$files = implode(",", $files); // insert $files into your table
So you need to change on your code :
if($request->hasfile('image')){
$files = []; // make an array
foreach ($request->image as $image) {
$path = $image->getClientOriginalName();
$filename = time() . '-' . $path;
$files[] = $filename; // insert to array
$image->storeAs('employees', $employee->id . '/' . $filename);
$image->move(public_path('employees'), $filename);
}
$files = implode(",", $files); // insert $files into your table
}
i try to delete some image file in laravel project. i follow tutorial on website, and its work, but tutorial only deleted image based name in column image. the thumbnail on folder image didnt deleted. how to delete image also the thumbnail image? pls help.
this is the store function.
if ($request->hasFile('image'))
{
$file = $request->file('image');
$image = $file->getClientOriginalName();
$destination = public_path() . '/galeri/';
$successUploaded = $request->file('image')->move($destination, $file->getClientOriginalName());
if($successUploaded)
{
$extension = $file->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $image);
Image::make($destination . '/' . $image)
->resize(250, 170)
->save($destination . '/' . $thumbnail);
}
$photo->image = $image;
}
$photo->save();
and this the destroy function
$photo = Photo::where('id', $id)->first();
File::delete('galeri/'.$photo->image);
if($photo){
$photo->delete();
}
Delete a single file
File::delete($filename);
Delete multiple files
File::delete($file1, $file2, $file3);
Delete an array of files
$files = array($file1, $file2);
File::delete($files);
I am trying to upload multiple images to my server, however it is not working. It gives me status code 200 but it doesn't upload images.
This is what I do:
if($request->hasFile('post_image')){
$images = $request->post_image;
$i = 0;
foreach($images as $image){
$i++;
$filename = $post->id.'.'.$i.'jpg';
$location = '/var/www/site/html/'.$post->id;
$image->move($location, $filename);
}
}
If I am removing the foreach() then it uploads image but only one.
Why it is not uploading them? What I am doing wrong?
Edit:
Answering my own question here also. The problem was that my key "post_image" had to be "post_image[]" instead.
You forgot to add . before 'jpg' extension in your filename:
Should be like this:
$filename = $post->id.'.'.$i.'.jpg';
I am trying this way upload multiple images:
if(!empty(Input::file('image'))){
$files= Input::file('image');
$destinationPath= 'images';
$images=array();
foreach($files as $file){
$fullname= $file->getClientOriginalName();
$hashname = $fullname;
$upload_success =$file->move($destinationPath, $hashname);
$images[]=$fullname;
$has= implode(",",$images);
}
$categories = new CategoryType;
$categories->name = Input::get('name');
$categories->image_attachment = $has;
$categories->save();
}
Follow this awnser you can loop on the all uploaded files
Get all files in a foreach loop in Laravel
like $images = Input::file('post_image');
The problem was that my key "post_image" had to be "post_image[]" instead.
$file_ary = array();
$file_count = count($request->file('image') );
$a=($request->file('image'));
$finalArray=array();
for ($i=0; $i<$file_count; $i++) {
$fileName = time().$a[$i]->getClientOriginalName();
$destinationPath = $request->input('path') ;
$finalArray[$i]['image']=$destinationPath.$fileName;
$a[$i]->move($destinationPath,$fileName);
}
return json_encode($finalArray);
I need to rename images in a folder i.e. give them unique names. Using this code images are not renamed but deleted !
$path = "../b-300x250/";
$items = glob($path . '*.jpg');
foreach($items as $img) {
$uniq = uniqid() . '.jpg';
rename("$img", "$uniq");
}
Your code is moving your images to a different directory.
Add the path to your unique name.
$path = "../b-300x250/";
$items = glob($path . '*.jpg');
foreach($items as $img) {
$uniq = $path . uniqid() . '.jpg';
rename("$img", "$uniq");
}
Here you must provide complete path or a valid path. In your code you are trying to rename files in current working directory but actually you have to work on ../b-300x250/ directory, So you should append this to make your code to rename a file correctly.
Change this:
rename("$img", "$uniq");
This:
rename("$img", $path.$uniq);
PHP code:
$path = "../b-300x250/";
$items = glob($path . '*.jpg');
foreach ($items as $img)
{
$uniq = uniqid() . '.jpg';
rename("$img", $path.$uniq);
}
I am trying to upload a file using laravel Storage i.e
$request->file('input_field_name')->store('directory_name'); but it is saving the file in specified directory with random string name.
Now I want to save the uploaded file with custom name i.e current timestamp concatenate with actual file name. Is there any fastest and simplest way to achive this functionality.
Use storeAs() instead:
$request->file('input_field_name')->storeAs('directory_name', time().'.jpg');
You can use below code :
Use File Facade
use Illuminate\Http\File;
Make Following Changes in Your Code
$custom_file_name = time().'-'.$request->file('input_field_name')->getClientOriginalName();
$path = $request->file('input_field_name')->storeAs('directory_name',$custom_file_name);
For more detail : Laravel Filesystem And storeAs as mention by #Alexey Mezenin
Hope this code will help :)
You also can try like this
$ImgValue = $request->service_photo;
$getFileExt = $ImgValue->getClientOriginalExtension();
$uploadedFile = time()'.'.$getFileExt;
$uploadDir = public_path('UPLOAS_PATH');
$ImgValue->move($uploadDir, $uploadedFile);
Thanks,
Try with following work :
$image = time() .'_'. $request->file('image')->getClientOriginalName();
$path = base_path() . '/public/uploads/';
$request->file('image')->move($path, $image);
You can also try this one.
$originalName = time().'.'.$file->getClientOriginalName();
$filename = str_slug(pathinfo($originalName, PATHINFO_FILENAME), "-");
$extension = pathinfo($originalName, PATHINFO_EXTENSION);
$path = public_path('/uploads/');
//Call getNewFileName function
$finalFullName = $this->getNewFileName($filename, $extension, $path);
// Function getNewFileName
public function getNewFileName($filename, $extension, $path)
{
$i = 1;
$new_filename = $filename . '.' . $extension;
while (File::exists($path . $new_filename))
$new_filename = $filename . '_' . $i++ . '.' . $extension;
return $new_filename;
}