how to give save option to a xls file - file

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;

Related

Symfony 2.8: how to upload an image from URL soruce?

How are you? First, sorry about my english :(
I'm triying to upload a file from a URL source like https://www.somedomain/somefolder/someimage.jpg but I'm getting some troubles...
I get this error when I'm triying to set $myFile->setFile('url'):
Catchable Fatal Error: Argument 1 passed to
MyBundle\Entity\File::setFile() must be an instance of
Symfony\Component\HttpFoundation\File\UploadedFile, string given
I've also tried through $content = get_file_contents('url') and then $myFile->setFile($content), but this returns the same error.
On the other hand, it's not a problem if I upload the file with an inputfile box in a form. But I cannot do it by indicating only one URL source (for a CronJob, by the way).
I've read the documentation about Files on Symfony, but I can't get success :(
Can anyone help me with this issue, please?
Thanks a lot! Greetings from Barcelona!! :)
To get rid of this issue you can do this:
<?php
use Symfony\Component\HttpFoundation\File\UploadedFile;
$file = new UploadedFile('url', 'name of the file');
$myFile->setFile($file);
Take a look at UploadedFile
Be sure to provide the correct path to the path. Not sure it will work like this because you want to set the UploadedFile without uploading the file through an input file and without submitting the form. Have a try.

how to download excel file from php page using phpexcel

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

cannot set right charset when uploading files

Please be patient and check this problem.
I wrote some simple PHP code for uploading images.
Here is the code (snippets)
<?php
header('Content-Type: text/plain; charset=utf-8');
//set encoding for prepared statements
$dbh->exec("SET NAMES UTF8");
$dbh->query('SET NAMES UTF8');
//check if file is actually an image etc.
//send image to "upload" folder
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
//save to the database a string like "upload/myImage.jpg", so I can render it on the site later
$stu = $dbh->prepare("UPDATE multi SET m_place=:name WHERE m_id = :id");
$stu->bindParam(':name', $n, PDO::PARAM_STR);
$n= "upload/".$_FILES["file"]["name"];
$stu->execute();
If the name of the image is in English, everything is fine. If is in Greek, it saved ok in the database , but not in the folder. In the database I see χωρις τιτλο.jpg (which is right) and in the folder χωΟΞ―Ο‚ τίτλο.jpg which is wrong.
I've tried everything and cannot fix this. To get the right titles in the folder.
The encoding of the database (postgreSQL 9.1) is UTF8, the Collation and the Character Type are Greek_Greece.1235. The collaction in table's column which I save the image's title is pg_catalog."C".
I use DreamWeaver. The file that handles the uploads is a php file. Encoding is utf8, Unicode Normalization Form is C and the Include Unicode Signature (BOM) is unchecked.
The default language is Greek in Region and Language in the control panel. (Windows 7 Home Premium)
The encoding of the browser is utf8
I also use Apache 2.2.22. Is it Apache's fault? Or its the php file? Or the database?
I dont know what else to do...What am I missing? Please please please advice
This seems to be a StackOverflow issue and not related to ServerFault. However, you should take a look at this post:
For naming files in UTF-8:
Can a PHP file name (or a dir in its full path) have UTF-8 characters?
For writing files in UTF-8:
How to write file in UTF-8 format?
Turns out , I had not tried everything.
Thanks to this I found the solution.
It has to do with PHP and how "PHP filesystem functions can only handle characters that are in system codepage"
So I used the iconv function.
I changed the code like so
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . iconv('UTF-8', 'Windows-1253',$_FILES["file"]["name"]));
For the application using japanese, the code page is cp932
This can be comfirm by using chcp command in commandline.
Sample Code
$fullPath = $uploadFolderPath."\\".iconv("utf-8", "cp932", $fileName);
move_uploaded_file($this->request->data["file_name"]['tmp_name'], $fullPath);

Javascript edit external file

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.

how to avoid .html extension being appended when downloading or opening my file

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.

Resources