I wrote a code in Lumen to upload multiple images at once using cloudinary, but the code doesn't work only the first image gets uploaded.
$images = $request->file('picture');
$uploaded = [];
foreach ($images as $image) {
error_log('for statement fires.');
Cloudder::upload($image, null, [
'folder' => '/dog-lovers',
'discard_original_filename' => true,
]);
$image_uploaded = Cloudder::getResult();
array_push($uploaded, $image_uploaded['url']);
}
return array('message'=>'successful', 'file_url'=>$uploaded);
In the above code, the for statement doesn't get called, because $images isn't an array i guess, so I made $images an array
$images[] = $request->file('picture');
Now the for statement fires up, but only the first selected image from postman gets uploaded
this is a screenshot from my postman just in case anyone is wondering how i'm uploading via postman
Does anyone have any idea why the other 4 images don't get uploaded and how to fix this?
This solution to this is to make the picture parameter on postman an array, like this picture[]
decided to post the solution incase anyone ran into similar problems
Related
Consider the case where a user uploaded his profile image and it was placed in the project as public/userprofileimage.jpg when the repository was at version 1.0.0.
The project was then pulled from GitHub, changed, or new code was added, and it was pushed to the repository, becoming version 2.0.0.
How can I add the latest uploaded photos to the repository each time a user uploads something?
Will the user-uploaded picture be in 2.0.0?
or the image will be lost
What should I do with the user-uploaded images if that's the case so they don't get lost in version control?
if($request->hasFile('image1') && $request->hasFile('image2') && $request->hasFile('image3') && $request->hasFile('image4')){
$formFields['media'] =
$request->file('image1')->store('postUploads','public') . ','
. $request->file('image2')->store('postUploads','public') . ','
. $request->file('image3')->store('postUploads','public') . ','
. $request->file('image4')->store('postUploads','public');
}
and did that
php artisan storage:link
l retrieve the image like that
<img src="{{asset('storage/' . $image)}}" class="md:w-48 m-2 rounded" alt="">
is that the right way?
thanks
A better approach
use Illuminate\Support\Facades\Validator;
public function store(Request $request){
$validate = Validator::make($request->all(), [
$request->file('image1') => 'required|mimes:jpg,png,jpeg',
$request->file('image2') => 'required|mimes:jpg,png,jpeg',
$request->file('image3') => 'required|mimes:jpg,png,jpeg',
$request->file('image4') => 'required|mimes:jpg,png,jpeg',
]);
if( $validate->fails() ){
return response($validate->errors(), 400);
}
//anything below here means validation passed. You can then store your images
$path1 = $request->file('image1')->store('profile_pictures','public');
$path2 = $request->file('image2')->store('profile_pictures','public');
$path3 = $request->file('image3')->store('profile_pictures','public');
$path4 = $request->file('image4')->store('profile_pictures','public');
}
Note that this can even be further simplified by saving your images to an array. I did not use that approach as I am not sure whether all the images are profile images or will be used differently.
Your images will be stored in /storage/profile_pictures and Laravel will automatically generate an image name for you.
On your view you can call the images using the asset helper as below
<img src="{{ asset($path1) }}"/>
This is assuming you are sending the image paths individually, which also can be simplified based on your application. Hope this give you an idea.
after completing a registration formular, when user clicks submit button, I want to first download a pdf and then redirect user to a view. Here is my code :
public function formularSave(Request $request) {
if(!isset($_REQUEST['token'])) {
abort(404);
}
$token = $_REQUEST['token'];
$upd_app = Application::where('token', $token)->update([
'status' => 22
]);
$result = "Registration complete.";
$html .= 'Some test code here
<br>
<p>Date: '.date('d.m.Y H:i:s').'</p>
';
$pdf = PDF::loadHTML($html);
$filename = substr(md5(uniqid().time()), 0, 17) . '.pdf';
$pdf->save(storage_path().'/app/public/uploads/rezolutii/'.$filename);
//code for download pdf HERE!!!!
return view('site.pages.registercomplete', compact('result'));
}
How can I download the pdf, after I create it?
It's impossible on Laravel.
This will not work. Your browser operates on simple requests one goes out one comes in.
There is no way for browser to know if user finished downloading the file and saved it somewhere. The final response from browser if file to be downloaded, nothing can follow that as far as I understand.
Now I'm not sure how that can be handled in javascript but in pure html requests it will not work.
Check this https://laracasts.com/discuss/channels/laravel/redirect-after-download
I want to export data tables to PDF.
I'm using Codeigniter Framework, and use dompdf plugin.
I think the code is correct, because it doesn't show any error information when I click the button to export to PDF, the page spends a long time loading, and in the end, it just displays "can't reach the page".
This problem also happens when I try to download files from my local directory.
Here is the code:
actually issue happened when i try to download more number of records
The controller:
$this->load->library('pdf'); // change to pdf_ssl for ssl
$data = array();
$data['result'] = $finaldata;
$data['search_header'] = $search_header;
$html = $this->load->view('admin/report/school_pdf', $data, TRUE);
$this->pdf->create($html, $filename);
I am receiving multiple images in base64 from rich text editor. My idea was to upload all the images and replace base64 img src in article content with newly created image path. I am using spatie media library and Laravel.
foreach ($data['images'] as $image) {
$article->addMediaFromBase64($image)->toMediaCollection('article-images');
$mediaItems = $article->getMedia('article-images');
$article->content = str_replace($image, $mediaItems[count($mediaItems) - 1]->getFullUrl(), $article->content);
$article->save();
}
The problem I have is that $article->getMedia('article-images') always returns only the first created image and the count is always one. So what ends up happening is no matter how many images I upload it will replace all their src tags with the url of the first image.
This is the final solution I went with. The relationship was probably cached after the first image upload and that's probably why I was always getting the first image. After loading media relation on the model, I was able to retrieve all images in the collection properly.
foreach ($data['images'] as $image) {
$article->addMediaFromBase64($image)->toMediaCollection('article-images');
$mediaItems = $article->load('media')->getMedia('article-images');
$article->content = str_replace($image, $mediaItems[count($mediaItems) - 1]->getFullUrl(), $article->content);
}
I want to get file data before upload happens. I have tried this with the following command order
$this->upload->get_data()
$this->upload->do_upload()
but this gave empty results. (It works the other way round)
So my question is: how is possible to get file data in codeigniter before upload happens?
var_dump($_FILES); die; OR
$upload_data = $this->upload->data();
var_dump($upload_data); die;
Your file is not being uploading
you need to check this
$error = $this->upload->display_errors();
print_r($error);
and one thing more
$this->upload->do_upload('input_filed_name');