I have a website in codeigniter and I need to give an option to user to download multiple images.
Below is simplified code. Now when I run this code it generates only the first iframe and downloads only first file. What correction is required, so that it downloads both images?
downloadimages.php contains following code:
$addtest = base_url().'admin/test_multiple_downfile/img1.jpg';
echo '<iframe src="'.$addtest.'">/iframe>';
$addtester = base_url().'admin/test_multiple_downfile/img2.jpg';
echo '<iframe src="'.$addtester.'">/iframe>';
admin.php under controller folder contains following code:
public function test_multiple_downfile()
{
$this->include_all_helpers_database_session();
$id['id'] = $this->uri->segment('3');
$this->load->view("multiple_downfile",$id);
}
multiple_downfile.php contains following code:
<?php
$this->load->helper('download');
$file_data = file_get_contents(base_url()."attachments/ur136/".$id);
$file_name = $id;
force_download($file_name, $file_data);
?>
I am new to php and codeigniter, so I might be missing something obvious here.
Related
i am trying to create a chart in PHP through pChart 2.3 class.
My doubt is on this function:
function autoOutput(string $FileName = "output.png", int $Compression = 6, $Filters = PNG_NO_FILTER)
{
if (php_sapi_name() == "cli") {
$this->Render($FileName, $Compression, $Filters);
} else {
$this->Stroke(TRUE, $Compression, $Filters);
}
}
Html Code:
<div id="filtri">
<?php include($_SERVER['DOCUMENT_ROOT'] . "/reportingBug/generaGrafico1.php");?>
<?php include($_SERVER['DOCUMENT_ROOT'] . "/reportingBug/recuperaTempiMediLavBug.php");?>
<?php include($_SERVER['DOCUMENT_ROOT'] . "/reportingBug/recuperaTempiMediBug.php");?>
<img src="temp/example.drawBarChart.spacing.png">
</div>
Php Script:
/* Render the picture (choose the best way) */
$myPicture->autoOutput("temp/example.drawBarChart.spacing.png");
?>
If i force the code in order to follow the if branch with Render function everything is working right
because it creates a PNG image in root folder and i can retrieve it from HTML.
but it goes through the else branch, so the stroke function is called.
In the case, i have two problems:
i don't know how to retrieve the PNG from HTML
i have a problem because the browser shows following:
I tried to use a phisical image (PNG file create on the server folder) but i think it's not correct because many user will access the application concurrently.
What I want to do is upload an image to my sql database using an html form and then retrieving it from the database to display it. I`ll show you some code so you can understand what I am trying to do.
1st EDIT: Just linking an image to show the database table I am uploading the image to. The row type is blob --> https://imgur.com/a/KBlNWWN in the code below I edited out the other rows of the DB table, but code for them is actually in.
This is the form that is uploading the image:
<form method="POST" action="/~gd339/ci/index.php/Add/postEvent/<?php echo $societyId ?>" enctype="multipart/form-data">
Event image: <input type="file" name="image"/> <br>
<input type="submit" name="POST" value="Post Event!" class = "unique"/>
What it does is that it posts the data to the postEvent controller:
public function postEvent($society_id) {
$this->load->model('Events_model');
$image = file_get_contents($_FILES['image']['tmp_name']);
$this->Events_model->addEvent($image);
}
Note that to get the image from the form, I am using $image = file_get_contents($_FILES['image']['tmp_name']); data retrieved using post is then sent to addEvent in my model:
public function addEvent ($image) {
$image1 = $this->db->escape($image);
$sql = "INSERT INTO events (event_image) VALUES ($image1)";
$query = $this->db->query($sql);
}
To retrieve the image I am using an image tag with a src that calls a function in the controller:
<img src="http://dragon.maple.as.re/~gd339/ci/index.php/add/getEventImage/<?php echo $row['event_id'] ?>">
public function getEventImage($event_id){
$this->load->model('Societies_model');
$image = $this->Societies_model->getEventImage($event_id);
header("Content-type: image/jpeg");
print($image);
}
code for getEventImage:
public function getEventImage($event_id) {
$data = '';
$sql = "SELECT event_image FROM events WHERE event_id = $event_id";
$query = $this->db->query($sql);
if ($query->num_rows()) {
$data = $query->row_array();
$data = $data['MA_PHOTO'];
$query->free_result();
}
return $data;
}
This results in the image just not showing up. I have been reading other examples here on stackoverflow and all of them, except for 1, use the CI upload library to do what I am trying to do. Is the library the only way to retrieve images using CI or is there something wrong in my code preventing the images to appear when called?
Thanks a lot to whoever will have the patience to go through my code. I know it is a lot but it is the first time I am using CI so im still learning.
I have done a similar process without using CI_Upload.
A useful debug is, using some db tool, save the blob to a file and try to display it (using mspaint, image viewer, etc...)
Before I save the image on database, I use the following function to resize (as I don't need it big) and convert(ensure) that it is a jpeg.
private function get_image_resized($data){
$data = smart_resize_image (
null,
$data,
250,
0,
true,
'return');
$temp_file = tempnam(sys_get_temp_dir(), 'ImageResize');
imagejpeg($data, $temp_file, 100);
$data = file_get_contents($temp_file);
unlink($temp_file);
return $data;
}
smart_resize_image can be found here:
To display the image
function imagem($cd_produto){
$im_produto = $this->produto_model->get_imagem ( $cd_produto );
header ( "Content-type:image/jpeg" );
echo $im_produto;
}
Hope it helps.
I am using HTML2PDF library in codeigniter.I am trying to generate bulk pdf using it.
In which i am facing issue like same content in every pdf or pdf have no content.I have already did my homework.Yeah but there is always showing perfect for generated first pdf (For account :3)
As per me there is must be issue of below code :
ob_start();
require_once($template_config.'template.php'); //
$content = ob_get_contents();
ob_clean();
Issue : It works for first time but for second time it flush all the content of content variable and due to that duplicate PDF or without content PDF generate.
I have tried like below
1) create object in generatetemplate.php and pass to common.php
2) tried with include_once //getting same conent in every pdf and if i am doing echo then showing no content for 2nd and 3rd pdf
File structure :
application
controllers
generatetemplate.php
libraries
common.php
html2pdf
html2pdf.php
template.php
common.php :
function print_content($customerdata){
$this->load->library('/html2pdf/html2pdf');
$template_config=$this->config->item('template');
ob_start();
require_once($template_config.'template.php'); //
$content = ob_get_contents();
ob_clean();
$content = str_replace("<CUSTOMER_ADDRESS>",$CUSTOMER_ADDRESS,$content);
$this->CI->html2pdf->pdf->SetDisplayMode('fullpage');
$this->CI->html2pdf->writeHTML($content);
$this->CI->html2pdf->Output($download_path,"F");
}
generatetemplate.php
function __construct() {
parent::__construct();
$this->load->library("common");
$this->load->library('html2pdf');
}
function get_customer_data(){
$this->db->order_by("id","DESC");
$this->db->where('id IN (1,2,3)');
$query = $this->db->get("customers")->result_array();
foreach($query as $key=>$accountdata){
$this->common->print_content($accountdata);
}
}
Any help and ideas will be appreciated.
I have tried below code and its work for me.
Common.php
function print_content($customerdata){
$this->load->library('/html2pdf/html2pdf');
$template_config=$this->config->item('template');
ob_start();
require_once($template_config.'template.php'); //
$content = ob_get_contents();
ob_clean();
$content = str_replace("<CUSTOMER_ADDRESS>",$CUSTOMER_ADDRESS,$content);
$this->CI->html2pdf = new HTML2PDF('P','A4','en'); // Just added this line and its work for me.
$this->CI->html2pdf->pdf->SetDisplayMode('fullpage');
$this->CI->html2pdf->writeHTML($content);
$this->CI->html2pdf->Output($download_path,"F");
}
I'm using a plugin called jQuery file upload to upload images to a page. Currently it uploads with the original image name as the file name (IMG_1234). I need a specific format for the image name on the server (eg 1.123456.jpg)
I found this PHP code that works for changing the image name:
class CustomUploadHandler extends UploadHandler
{
protected function trim_file_name($name, $type) {
$name = time()."_1";
$name = parent::trim_file_name($name, $type);
return $name;
}
}
When I upload an image, it is named correctly, but the link for the image preview is undefined. This prevents me from deleting the image via the plugin.
The variable data.url is undefined... If I go back to the original code that doesn't rename the image, everything works fine.
Has anyone had any experience with this plugin that could help? Thanks!
EDIT:
I've found part of the problem at least...the function to return the download link (which is also used for deletion) is giving the original file name, not the updated one. I am really new to PHP classes, so I'm not sure where the variable originates and how to fix it. I'd really appreciate any help I can get!
Here's the PHP code for that function:
protected function get_download_url($file_name, $version = null, $direct = false) {
if (!$direct && $this->options['download_via_php']) {
$url = $this->options['script_url']
.$this->get_query_separator($this->options['script_url'])
.'file='.rawurlencode($file_name);
// The `$file_name` variable is the original image name (`IMG_1234`), and not the renamed file.
if ($version) {
$url .= '&version='.rawurlencode($version);
}
return $url.'&download=1';
}
if (empty($version)) {
$version_path = '';
} else {
$version_url = #$this->options['image_versions'][$version]['upload_url'];
if ($version_url) {
return $version_url.$this->get_user_path().rawurlencode($file_name);
}
$version_path = rawurlencode($version).'/';
}
return $this->options['upload_url'].$this->get_user_path()
.$version_path.rawurlencode($file_name);
}
EDIT 2: I think it has something to do with 'param_name' => 'files', in the options. Anyone know what that does?
Fixed it by editing the trim_file_name function inside UploadHandler.php instead of extending the class in index.php.
Since it's not a large number of images that my db will take, i'm uploading them directly to database. However, i'm having problems displaying them, i don't want to download them, i want to see them on the page. I'm trying to display with the following code but it's not working:
function display() of MyFiles Controller:
function display($id)
{
$file = $this->MyFile->findById($id);
$this->set('image',$file['MyFile']['data']);
}
MyFile Model:
<?php
class MyFile extends AppModel {
var $name = 'MyFile';
}
?>
function add() of MyFilesController
function add() {
if (!empty($this->data) &&
is_uploaded_file($this->data['MyFile']['File']['tmp_name'])) {
$fileData = fread(fopen($this->data['MyFile']['File']['tmp_name'], "r"),
$this->data['MyFile']['File']['size']);
$this->request->data['MyFile']['name'] = $this->data['MyFile']['File']['name'];
$this->request->data['MyFile']['type'] = $this->data['MyFile']['File']['type'];
$this->request->data['MyFile']['size'] = $this->data['MyFile']['File']['size'];
$this->request->data['MyFile']['data'] = $fileData;
$this->MyFile->save($this->request->data);
$this->redirect(array('controller'=>'posts','action'=>'index'));
}
}
EDIT:
display.ctp of MyFiles View
<?php
echo '<img src="/MyFilesController/display/4" />';
?>
Saving images in a database is generally not a good idea.
You are far better off (and I assume you have done this for 'security') saving the files in a folder that is not web accessible and using Media Views to render them. This way you can still keep your security checks as you are using a controller to render the image.
Cakes media views are designed for streaming files to the browser and can easily be configured for images.
2.3 has a new feature for this
If you are not doing this for security reasons just save your self the problems and put them in webroot.