In our cakephp application there have a excel file downloading..
I just prepared a view page for that .
but i need to download that..
How can i do this?
I want to use the PHPExcel plugin for that....
This is the code I'm used for that:
/** PHPExcel_IOFactory */
include '../Vendor/PHPExcel/Classes/PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('HTML');
$objPHPExcel = $objReader->load('../View/Events/settlement_report_excel.html');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("myExcelFile.xlsx");
But it displayed ../View/Events/settlement_report_excel.ctp is an Invalid HTML file.
How can i overcome this issues...
You can create desired csv, xls using simple PHP in CakePHP environment. On execution of following code, desired excel file will be downloaded.
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=names.xls");
header("Pragma: no-cache");
header("Expires: 0");
$schema_insert = $firstname."\t".$lastname."\t".$fullname;
echo ($schema_insert . "\r\n");
Related
I want block the direct access to some of public files from my directory. For example, I have the file image.png and I have to fill a captcha to download that file, if I fail the captcha I don't want that the user could access the file. Is there any way to do that in Symfony?
First of all, create an .htaccess file inside the image directory to prevent the direct access of files inside the directory.
Deny from All
Now, give customised path, through controller to download the file, where you can easily integrate a captcha by using some bundle like Gregwar's CaptchaBundle.
On successful captcha validation, download the file through Response.
$filename = __DIR__ . "../path_to_file/image.png";
// Generate response
$response = new Response();
// Set headers
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', mime_content_type($filename));
$response->headers->set('Content-Disposition', 'attachment; filename="' . basename($filename) . '";');
$response->headers->set('Content-length', filesize($filename));
// Send headers before outputting anything
$response->sendHeaders();
$response->setContent(file_get_contents($filename));
Note : Code not tested!
Hope this helps!
Im using cakepdf for generating pdf files using wkhtmltopdf and it works, the issue is when i wanted to create pdf files inside a folder it shows me this exception :
System error <pre>Exit with code 1 due to network error: ContentNotFoundError </pre>
Here's the code Im using for generating the pdf file :
$CakePdf = new CakePdf();
$CakePdf->template('mynote','default');
//get the pdf string returned
$pdf = $CakePdf->output();
//or write it to file directly
$pdf = $CakePdf->write(APP . 'webroot'. DS .'files' . DS . 'mynote.pdf');
and for the pdf template i used mynote.ctp that only contains this code inside pdf folder
<div>Hello</div>
After some hustle i disbaled this error by commenting the code inside output functions but still i can't get the style and the images,like i use to.
Thanks in advance.
Okay so I'm trying to make a script that can edit an external .txt file. I want to be able to do something like /name John Doe and it saves that name in the file that the command is supposed to edit.
Another example would be I have a file called List.txt associated with the command /todo, whenever I do /todo * it adds whatever came after the command to the List.txt file.
Is there any way I can do this in javascript?
You're in luck, it appears that HTML5 actually supports this. Of course you'll have to run it through a browser, I don't know if you can hack it somehow to work from bash.
Yes, its possible to do this by creating an ajax http request to an server server side script that edits the file based on the http request's content.
Heres an example PHP serverside script the handle the ajax request:
Note: This example has a lot of security issues and is untested
<?php
$command = $_POST['command'];
$argument = $_POST['argument'];
if ($command == "name") {
$file = fopen("names.txt", "a");
fwrite($file, $argument."\n");
fclose($file);
} else if ($command == "todo") {
$file = fopen("todo.txt", "a");
fwrite($file, $argument."\n");
fclose($file);
}
?>
There is also a great tutorial on AJAX requests here
They also have a php tutorial on here
ps. sorry it took so long.
When I am saving a file in a img/upload folder the file is saved with the correct file-extension.
However, when I try to download the file, a .htm file-extension is appended.
How can I avoid this? I've added my code below;
view.ctp
<?php echo $this->Form->label("Resume:");?>
<?php echo $this->Form->input("resume",array("class"=>"input_boxstyle_select","label"=>"","type"=>"file","id"=>"file"));?>
<?php echo $editEmpPros[0]['prospective_employee']['resume']?>
Inside my controller:
public function download_resume($id=null)
{
$LUser = $this->Session->read('username');
$this->disableCache();
if (!$LUser) {
$this->redirect(array("action"=>"../"));
}
$path="../webroot/img/upload/$id";
header('Content-Disposition: attachment'); readfile($path);
//print_r(readfile($path));
exit;
}
Handling file-downloads in CakePHP 2.x
While other solutions may work, CakePHP handles the response via the CakeResponse object. This chapter in the manual describes how to send (download) files; Sending Files
The response will automatically attempt to set the right mime-type, based on the file-extension
To output the file (inside the browser);
$this->response->file(WEBROOT_DIR . '/img/upload/' . $filename);
//Return reponse object to prevent controller from trying to render a view
return $this->response;
To download the file (and, optionally, specify a custom filename)
To force downloading the file and specify a custom filename (if desired), use this code. CakeResponse object will automatically set the right headers, so manually specifying a custom filename should not be necessary
// To force *downloading* the file and specify a custom filename (if desired)
$this->response->file(
WEBROOT_DIR . '/img/upload/' . $filename,
array(
'download' => true,
'name' => 'custom-filename-for-downloading'
)
);
return $this->response;
You can give the filename in the Content-Disposition header too, like this:
Content-Disposition: attachment; filename="foo.bar"
Depending on the browsers you have to support you could also use the download attribute of HTML5:
my link
you have to set some parameter as here i am giving you one example to download PDF file
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
Please make parameter as you need..
let me know if i can help you more.
I need to generate a report form mysql table in xls format. I got
Saving a .xls file with fwrite
this code and it is working like charm. But the thing is I need to have a save option, download and browse the directory to save the file. Can anybody helpme out? I tried changing the header but i didn't get any success.
Thanks in advance
Vinay
Assuming you're using PHP:
header('Content-Type: application/vnd.ms-excel;');
header('Content-type: application/x-msexcel');
header('Content-Disposition: attachment; filename="your_xls_on_the_server.xls";');
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize('your_xls_on_the_server.xls'));
ob_clean();
flush();
readfile('your_xls_on_the_server.xls');
exit;