Saving file into a prespecified directory using FPDF
I want to save the PDF file into a user specified directory. I am using FPDF. And the code is as below:
<?php
//echo "<meta http-equiv=\"refresh\" content=\"0;url=http://www.train2invest.net/useradmin/atest_Khan.php\">";
require('fpdf.php');
//create a FPDF object
$pdf=new FPDF();
//set font for the entire document
$pdf->SetFont('times','',12);
$pdf->SetTextColor(50,60,100);
//set up a page
$pdf->AddPage('P');
$pdf->SetDisplayMode(real,'default');
//Set x and y position for the main text, reduce font size and write content
$pdf->SetXY (10,60);
$pdf->SetFontSize(12);
$pdf->Write(5,'Dear Ms.XYX');
$filename="test.pdf";
//Output the document
$dir = "/G:/PDF/test.pdf/"; // full path like C:/xampp/htdocs/file/file/
$pdf->Output($dir.$filename,'F');
?>
Now even if I put "G:\PDF\"
in the filename it doesn't save it!! I have tried the following:
$filename="G:\PDF\t开发者_如何学Pythonest.pdf";
$pdf->Output($filename.'.pdf','F');
$filename="G:\\PDF\\test.pdf";
$pdf->Output($filename.'.pdf','F');
$filename="G:/PDF/test.pdf";
$pdf->Output($filename.'.pdf','F');
$filename="/G:/PDF/test.pdf/";
$pdf->Output($filename.'.pdf','F');
I have checked that the directory i am trying to write has write/read permission and it's there. IT STILL DOESN'T WORK!
PLEASE help somebody...
You are using the F option incorrectly, F is designed to save the PDF locally on the Server not in a specific directory on the users machine. So you would use something like:
$filename="/home/user/public_html/test.pdf";
$pdf->Output($filename,'F');
This will save in in the public_html directory of your webserver
Check the syntax here: http://www.fpdf.org/en/doc/output.htm
It is: string Output([string dest [, string name [, boolean isUTF8]]])
,
so you have to write:
$pdf->Output('F', $filename, true); // save into the folder of the script
or e.g.:
$pdf->Output('F', '/var/www/html/wp-content/' . $filename, true); // save into some other location
or relative path:
$pdf->Output('F', '../../' . $filename, true); // to parent/parent folder
However, I am not sure if you can use windows absolute path...
Having struggled with this myself, there are three things one has to ensure, two of which are mentioned in other posts on this topic:
- Command: output('F', "xyz_file");
- Permissions on the server's destination directory need to allow writes for non-escalated privileges (i.e. drwxrwxrwx)
- Definition of content type: header("Content-type:application/pdf");
Its because you are trying to save the file somewhere it doesnt want you to. Most probably because your havent set the permissions of the directory to 777.
If your PHP script is executed from a web-page (served by Apache, it is), then this code will be executed by the Apache (sometimes called www-data) user.
So, your Apache user needs to be able to write to the directory you're trying to write to.
Typically, you might have to give the write privilege to the other users of your system, using something like this from a command-line :
chmod o+w your_directory
The software you're using to upload your source files, if doing so using a GUI, should allow you to do that with a couple of chekboxes -- you need to check the "write" checkbox for the "others" users.
Here is a simple solution to help you save the file to specified directory as well as having the url of the file which you can save to database.
$invoice = new phpinvoice();
...
...
$path = $_SERVER["DOCUMENT_ROOT"].'/uploads/';
$url = $_SERVER['HTTP_HOST'].'/uploads/';
$invoice->render($path.'.pdf','F');
return $url.'.pdf'; // print_r($url.'.pdf')
I hope this helps.
Likely permissions of your Apache service:
http://www.php.net/manual/en/function.opendir.php#87479
Have you tried file upload? I think you and I might be trying to do the same thing and this seems to work. I am working on a shared drive as well.
http://php.net/manual/en/features.file-upload.post-method.php
I solved like this:
public functon GeneratePdf(){
...
PDF::Output("C:/xampp/htdocs/MyProject/doc.pdf","F");
}
I copied all directory path into Output method and I did not set further permissions for this.
I hope it helps you!!
change the 'F' to 'D'. D forces download. So your $pdf->output line should look like this.
$pdf->Output($path,'D');
精彩评论