php 使用 FPDF 将文件保存到预先指定的目录中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4339787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:43:50  来源:igfitidea点击:

Saving file into a prespecified directory using FPDF

phppdffpdf

提问by Sharmin

I want to save the PDF file into a user specified directory. I am using FPDF. And the code is as below:

我想将 PDF 文件保存到用户指定的目录中。我正在使用 FPDF。代码如下:

<?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:

现在,即使我"G:\PDF\"输入文件名,它也不会保存!!我尝试了以下方法:

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

请帮助某人...

回答by user2559331

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:

您错误地使用了 F 选项,F 旨在将 PDF 保存在服务器本地而不是用户计算机上的特定目录中。所以你会使用类似的东西:

$filename="/home/user/public_html/test.pdf";
$pdf->Output($filename,'F');

This will save in in the public_html directory of your webserver

这将保存在您的网络服务器的 public_html 目录中

回答by 9finger

Having struggled with this myself, there are three things one has to ensure, two of which are mentioned in other posts on this topic:

我自己为此苦苦挣扎,必须确保三件事,其中两件事在有关此主题的其他帖子中提到:

  1. Command: output('F', "xyz_file");
  2. Permissions on the server's destination directory need to allow writes for non-escalated privileges (i.e. drwxrwxrwx)
  3. Definition of content type: header("Content-type:application/pdf");
  1. 命令:输出('F',“xyz_file”);
  2. 服务器目标目录的权限需要允许写入非升级权限(即 drwxrwxrwx)
  3. 内容类型定义:header("Content-type:application/pdf");

回答by David Najman

Check the syntax here: http://www.fpdf.org/en/doc/output.htmIt is: string Output([string dest [, string name [, boolean isUTF8]]]), so you have to write:

检查这里的语法:http: //www.fpdf.org/en/doc/output.htm是:string Output([string dest [, string name [, boolean isUTF8]]]),所以你必须写:

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

但是,我不确定您是否可以使用 Windows 绝对路径...

回答by Vinicius Santana

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.

这是因为您试图将文件保存在它不希望您保存的地方。很可能是因为您尚未将目录的权限设置为 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.

如果您的 PHP 脚本是从网页(由 Apache 提供服务)执行的,那么此代码将由 Apache(有时称为 www-data)用户执行。

So, your Apache user needs to be able to write to the directory you're trying to write to.

因此,您的 Apache 用户需要能够写入您尝试写入的目录。

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

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.

您用来上传源文件的软件(如果使用 GUI 上传)应该允许您使用几个复选框来执行此操作——您需要为“其他”用户选中“写入”复选框。

回答by Erick Calderin Morales

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.

我将所有目录路径复制到 Output 方法中,并且没有为此设置进一步的权限。

I hope it helps you!!

希望对你有帮助!!

回答by Paul Norman

Likely permissions of your Apache service:

您的 Apache 服务可能的权限:

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

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

回答by J Noel

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

http://php.net/manual/en/features.file-upload.post-method.php

回答by Ashen Deimos

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

将“F”更改为“D”。D强制下载。所以你的 $pdf->output 行应该是这样的。

$pdf->Output($path,'D');