php 将文件发送给用户

23c0lvtd  于 2023-02-03  发布在  PHP
关注(0)|答案(4)|浏览(189)

我有一个pdf文件在磁盘上,我需要发送给用户时,他们提出了一个PHP脚本的请求,什么是最好的方式来做到这一点?

d5vmydt9

d5vmydt91#

假设它在服务器上:
readfile()-输出文件

:仅写

readfile($file);

这将使客户端永远等待响应。您需要定义标头,以便它按预期方式工作。See this example from the official PHP manual

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>
agxfikkp

agxfikkp2#

下面是使用PHP发送文件所需的内容:

$filename = "whatever.jpg";

if(file_exists($filename)){

    //Get file type and set it as Content Type
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    header('Content-Type: ' . finfo_file($finfo, $filename));
    finfo_close($finfo);

    //Use Content-Disposition: attachment to specify the filename
    header('Content-Disposition: attachment; filename='.basename($filename));

    //No cache
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');

    //Define file size
    header('Content-Length: ' . filesize($filename));

    ob_clean();
    flush();
    readfile($filename);
    exit;
}

正如Julian Reschke评论的那样,验证后的答案可能有效,但它充满了无用的头文件。content-type应该设置为文件的真实的类型,否则一些浏览器(尤其是移动的浏览器)可能无法正确下载。

0qx6xfy6

0qx6xfy63#

如果你使用的是Apache或Lighty,那么从性能的Angular 来看,最好的方法就是使用X-Sendfile头文件。https://www.h3xed.com/programming/how-to-use-x-sendfile-with-php-apache

sd2nnvve

sd2nnvve4#

好吧,我不是PHP的Maven,我只能把一些PHP的其他片段放在一起来完成我需要它做的事情,我想我最好把这个解决方案贴在一些论坛上,这些论坛问了同样的问题,但我自己却无法工作。似乎任何地方都没有解决方案,所以就在这里。它对我很有效...首先我创建了一个PDF表单,并添加了一个按钮,然后提交表单。在提交表单的操作中,我告诉它PDF完整的文档。然后我给它一个URL链接到一个php页面,如mail_my_form. php。然后我创建了一个php表单,最后一件事是在php代码所在的根目录下创建一个名为pdfs的文件夹(所以如果你把php放在一个名为email的文件夹中,那么在email文件夹中,你需要另一个名为pdfs的文件夹)现在这个脚本所做的是:将PDF保存为文件名pdfs。然后将文件附加到电子邮件中并发送。然后将文件从文件夹pdfs中删除以节省空间。(如果您愿意,也可以使用删除功能将表单保存到FTP上。
在这里。

<?php 
$fileatt = date("d-m-Y-His") . ".pdf";  // Creates unique PDF name from the date 
copy('php://input',"pdfs/".$fileatt); // Copies the pdf form data to a folder named pdfs 
$fileatt = "pdfs/".$fileatt; // Path to the file gives the pdfs folder plus the unique file name we just assigned
$fileatt_type = "application/pdf"; // File Type 
$fileatt_name = "Application Form_".$fileatt.".pdf"; // Filename that will be used for the file as the attachment when it is sent

$email_from = "mywebsite"; // Who the email is from 
$email_subject = "Completed online Applications"; // The Subject of the email 
$email_message = "Please find a recent online application attached.
";
 $email_message .= "Any problems please email me...
"; // Message that the email has in it 

$email_to = "youremail@yourserver.com"; // Who the email is to 

$headers = "From: ".$email_from;

//no need to change anything else under this point

$file = fopen($fileatt,'rb'); 
$data = fread($file,filesize($fileatt)); 
fclose($file); 

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

$headers .= "\nMIME-Version: 1.0\n" . 
"Content-Type: multipart/mixed;\n" . 
" boundary=\"{$mime_boundary}\""; 

$email_message .= "This is a multi-part message in MIME format.\n\n" . 
"--{$mime_boundary}\n" . 
"Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" . 
$email_message .= "\n\n"; 

$data = chunk_split(base64_encode($data)); 

$email_message .= "--{$mime_boundary}\n" . 
"Content-Type: {$fileatt_type};\n" . 
" name=\"{$fileatt_name}\"\n" . 
//"Content-Disposition: attachment;\n" . 
//" filename=\"{$fileatt_name}\"\n" . 
"Content-Transfer-Encoding: base64\n\n" . 
$data .= "\n\n" . 
"--{$mime_boundary}--\n"; 

$ok = @mail($email_to, $email_subject, $email_message, $headers); 

if($ok) { 
unlink($fileatt); //NOW WE DELETE THE FILE FROM THE FOLDER pdfs 
Header("Location: nextpage.php"); //where do we go once the form has been submitted.

} else { 
die("Sorry but the email could not be sent. Please go back and try again!"); 
} 
?>

希望这对你们中的一些人有帮助。
理查德德·威廉斯

相关问题