2010-05-21 23 views

Trả lời

51

Giả sử rằng đó là trên máy chủ:

readfile() - kết quả đầu ra một tập tin

Ví dụ từ http://php.net/manual/en/function.readfile.php

+1

Hoạt động trong firefox nhưng không phải IE –

+0

Hoạt động với tôi trong IE8/Vista. Bạn nhận được một lỗi hoặc nó chỉ đơn giản là tải một trang trắng? – Adirael

+23

Ví dụ này chứa rất nhiều crap. Nội dung mô tả không tồn tại trong HTTP. Loại nội dung phải được đặt thành loại phương tiện thực tế hoặc không có loại nào. Mã cho Nội dung-Bố trí sẽ tạo ra tiêu đề không chính xác cho nhiều tên tệp. Content-Transfer-Encoding không tồn tại trong HTTP. Xem thêm http://blogs.msdn.com/b/ieinternals/archive/2012/05/16/do-not-pollute-your-pages-with-mssmarttagspreventparsing-galleryimg-imagetoolbar-pre-check-post-check. aspx liên quan đến Cache-Control. –

1

Ok, vì vậy tôi không có chuyên gia về PHP, tôi chỉ có thể lấy tín dụng để kết hợp một vài đoạn mã PHP khác để đạt được những gì tôi cần và tôi nghĩ tôi đã có bài đăng tốt hơn giải pháp này lên trong một vài diễn đàn mà hỏi cùng một câu hỏi nhưng tôi không thể làm việc bản thân mình. Có vẻ như không phải là một giải pháp bất cứ nơi nào vì vậy ở đây nó được. Nó hoạt động cho tôi ... Ok vì vậy đầu tiên tôi đã tạo biểu mẫu PDF và thêm nút sau đó gửi biểu mẫu. Trong các hành động của biểu mẫu gửi này, tôi đã yêu cầu nó gửi cho PDF tài liệu hoàn chỉnh. Sau đó, tôi đã cho nó một liên kết URL đến một trang php, chẳng hạn như mail_my_form.php Sau đó, tôi tạo ra một hình thức php, và đặt tên nó giống như trên ... mail_my_form.php Một điều cuối cùng là tạo một thư mục có tên pdfs trong thư mục gốc của mã php này. (Vì vậy, nếu bạn đặt php trong một thư mục có tên email, sau đó bên trong thư mục email, bạn cần một thư mục khác có tên pdf) Bây giờ, tập lệnh này là gì: Lưu tệp PDF vào tệp pdf. Sau đó, nó đính kèm tệp vào một email và gửi nó. Sau đó, nó xóa tệp từ thư mục pdf để tiết kiệm dung lượng. (Bạn có thể đưa ra các chức năng xóa để lưu các hình thức của bạn trên FTP của bạn còn nếu bạn muốn.
Ở đây nó được.

<?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 = "[email protected]"; // 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!"); 
} 
?> 

Hope this helps một số bạn.

Richard Williams

24

Dưới đây là những gì bạn cần để gửi tệp với 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; 
} 

Như Julian Reschke đã nhận xét, câu trả lời đã xác thực CÓ THỂ làm việc, nhưng ' s đầy đủ các tiêu đề vô dụng. Loại nội dung phải được đặt thành loại thực của tệp hoặc một số trình duyệt (đặc biệt là trình duyệt trên thiết bị di động) có thể không tải xuống đúng cách.

+0

Tôi nhìn mãi mãi cho điều này, cảm ơn một triệu. –

+0

Cảm ơn bạn, đặc biệt là cho 'ob_clean(); flush(); ' – GHosT

+1

bạn có thể thêm chú thích vào các bit' ob_clean' và 'flush' không? Những vấn đề tiềm ẩn nào họ giải quyết? – YakovL

Các vấn đề liên quan