Saving file into a prespecified directory using FP

2019-03-18 09:03发布

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


$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...

标签: php pdf fpdf
8条回答
神经病院院长
2楼-- · 2019-03-18 09:10

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

查看更多
爷、活的狠高调
3楼-- · 2019-03-18 09:11

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.

查看更多
唯我独甜
4楼-- · 2019-03-18 09:12

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...

查看更多
爷、活的狠高调
5楼-- · 2019-03-18 09:13

Likely permissions of your Apache service:

http://www.php.net/manual/en/function.opendir.php#87479

查看更多
做个烂人
6楼-- · 2019-03-18 09:14

change the 'F' to 'D'. D forces download. So your $pdf->output line should look like this.

$pdf->Output($path,'D');
查看更多
Melony?
7楼-- · 2019-03-18 09:16

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!!

查看更多
登录 后发表回答