PHP foreach($images as $image) not uploading images - php

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);

Related

How to upload image from one project to another project laravel

I have two laravel project i use code in my Myfirstproject to upload to Mysecondproject public\src\img\upload directory.
I try these code in Myfirstproject:
if ($request->hasFile('images')) {
$destinationPath='Mysecondproject\public\src\img\upload';
if ($files = $request->file('images')) {
foreach ($files as $file) {
$name = $file->getClientOriginalName();
$file->move($destinationPath, $name);
$images[] = $name;
}
}
}
But it's not working, any solution for these?
Please try absolute path in $destinationPath as C:\Mysecondproject\public\src\img\upload or D:\Mysecondproject\public\src\img\upload.

Upload multiple files and save in database laravel 4.2

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;
}

Files upload in a loop

I have an application where i have to upload files through a loop. It uploads the first file but it gives an error that the file "" does not exist. Following is the array where I am getting files.
and following is the trace for the error.
Any idea what am I missing or what could be the problem?
UPDATE:
for($i=0; $i <=count($form_data['image']['files'])-1; $i++) {
$file = $form_data['image']['files'][$i];
$file_name = $file->getClientOriginalName();
$file->move("campaigns/$campaign->id/$package/images", $file_name);
}
Finally fixed it. Here is my previous controller action which was not working.
public function done(Request $request){
$packages = ['instagram', 'facebook', 'twitter'];
foreach($packages as $package){
// this is the line which was creating the problem
$form_data = $request['contents'][$package];
for($i=0; $i <=count($form_data['image']['files'])-1; $i++) {
$file = $form_data['image']['files'][$i];
$file_name = $file->getClientOriginalName();
$file->move("campaigns/$campaign->id/$package/images", $file_name);
}
}
}
To fix the problem just assign the request variable to another variable. This is because the request processes the files in the first loop and then it cannot find the files for other attributes in the next iteration.
public function done(Request $request){
$packages = ['instagram', 'facebook', 'twitter'];
// Fixed it with this line
$contents = $request['contents'];
foreach($packages as $package){
$form_data = $contents[$package];
for($i=0; $i <=count($form_data['image']['files'])-1; $i++) {
$file = $form_data['image']['files'][$i];
$file_name = $file->getClientOriginalName();
$file->move("campaigns/$campaign->id/$package/images", $file_name);
}
}
}

how to save multiple images in table using laravel php

Hi please help me am new to laravel
I want to store multiple images in table... With this code am unable to save.
Help me for this..
Here in my View
{{Form::open(array('url'=>'businessdirectory/business', 'files'=>true))}}
{{Form::label('image','Upload Image')}}
<div class="form-group">{{Form::file('image[]',array('multiple'=>true))}}
</div>
{{Form::close()}}
In my Controller
if(Input::file('image'))
{
$image = Input::file('image');
foreach($image as $img) {
$destination = 'images';
$filename = $img->getClientOriginalName();
$path = 'images/'.$filename;
$uploadSuccess = $img->move($destination,$filename);
}
}
else
{
$path='images/default.JPG';
}
$business = new Business();
$business->image = $path;
It's not advisable to store images on database. Just save the path of the images instead.
If you really need to store image on db. Make sure you set the column to blob as it need more space. Then, get the image content and type, then save it.
<?php
$image = fopen($image_path, 'rb');
// or
$image = file_get_contents($image_path);
$business = new Business;
$business->image = $image;
$business->imageType = "image/gif"; //
$business->save();
// ...
You need to put below code inside the foreach loop. In every loop you need to insert image path inside a database
$business = new Business();
$business->image = $path;
This is a working code in laravel 5.2.
$files = $request->file('file');
if($request->hasfile('file')){
$destinationPath = 'uploads';
foreach($files as $file){
$image = new Image;
$filename = $file->getClientOriginalName();
$image->name = $filename;
$image->save();
$file->move($destinationPath,$filename);
}

PHP - Loop through all files in a directory, in reverse order

I am currently using this code to loop through each image in a folder:
$directory= "../images/uploads/*.jpg";
$images = glob( $directory );
foreach ($images as $image){
$filename = $file = basename($image);
echo("$image <br/>");
}
Each file is uploaded with an incremental ID number at the beginning so I need it to display these with the latest ID first. (Reverse alphabetical).
Is this possible to do?
You can use array_reverse to do that:
$directory= "../images/uploads/*.jpg";
$images = glob( $directory );
foreach (array_reverse($images) as $image){
$filename = $file = basename($image);
echo("$image <br/>");
}

Categories