How to get file data before upload in codeigniter - codeigniter

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

Related

can't reach page while export pdf/downloading file in dompdf

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

How to upload multiple images at once using cloudinary and lumen

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

Remove unused Images/files from upload folder laravel

I have laravel5.4 application.I want to remove unused images/files from my upload folder which is not available in my database.
For example :
I have 50 images in my upload folder for user profile but some of the image not use for any user.i think he removed or update his image from frontend.
Yes i know we need to code to remove file when user update or remove profile picture at a time also delete from upload folder.but my app run from many time and i want to remove unused file using script not manually beacause i have lot's of files so it's hard to check and remove file manually.anyone can you please help me for create any function for remove file from folder.
Sorry for my bad English.
I use something like this in my AdminController to remove images by clicking on a button.
Maybe you need to change the path or extensions
public function deleteUnusedImages()
{
$file_types = [
'gif',
'jpg',
'jpeg',
'png'
];
$directory = public_path();
$files = File::allFiles($directory);
foreach ($files as $file)
{
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (in_array($ext, $file_types)) {
if(DB::table('users')->where('votes', '=', $file)->count())
continue; // continue if the picture is in use
echo 'removed' . basename($file)."<br />";
unlink($file); // delete if picture isn't in use
}
}
}

codeigniter - How to get embed data in my case

i load data in my controller like
$name['name'] = "some text!";
$data['header'] = $this->load->view('header_view', $name, TRUE);
$this->load->view('myView', $data);
and in my view file: myView.php
echo $header;
how can i get data in $name ? thanks
Try:
$data['name'] = "some text!";
$data['header'] = $this->load->view('header_view', $data['name'], TRUE);
$this->load->view('myView', $data);
You can't because when it comes to myView, the header view is allready processed and so data is allready incoroporated into the output string.
The only way is to copy (merge) the contents of $name into $data.
You can get embed data by parsing the content of $data['header'] but it is a really bad solution.
To solve this issue you can generate header and footer along the content inside your controller like that:
$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');
Or you can use a template engine to include header/footer automaticly with the content template.
Infortunately, there is no buildin way to add an header easily in codeigniter. You'll have to come up with your own functions to do that in a helper or library (but you'll not be able to share data between header and content).

I am using the upload function of codeigniter but how do I attach the image uploaded on the server once I edit that item?

I have been successful in using the File Uploading Library to upload my files on the server (mostly images).
My problem is when I need to update that specific item, I need to browse for photo again. Is there a way I can just attach the image uploaded already in the server to my upload button? I am thinking to make a media page like that of the wordpress but is there any starting point that anyone knows who can point me to? Thanks so much!
You can use a hidden field...check this example
View
<?php echo form_upload("vLogo", $campaign->vLogo , "id='vLogo'"); ?>
<?php echo form_hidden("vLogo_old", $campaign->vLogo , "id='vLogo_old'"); ?>
Controller
(Edit function)
//The way you do when you click on browse and select a file
if(isset($_FILES['vLogo']['tmp_name']) && !empty($_FILES['vLogo']['tmp_name']))
{
$this->load->library('upload', $config);
// Initialaizing Logo upload
$this->upload->initialize($config);
if ( !$this->upload->do_upload('vLogo')){
$logo_error = array('vLogo' => $this->upload->display_errors());
}
else{
$data1 = array('upload_data' => $this->upload->data());
}
}
else
{
//The browse filed is empty..So hidden input is taken
$data1['upload_data']['file_name'] = $post['vLogo_old'];
}

Resources