PHP下载不起作用

时间:2011-05-18 12:06:05

标签: php download warnings

我编写了一个简单的php脚本,允许用户在通过表单提交数据后下载文件。它工作正常几天,今天出现了这个错误消息

Warning: Cannot modify header information - headers already sent by (output started at /home/psitinc/public_html/mymail.php:6) in /home/psitinc/public_html/mymail.php on line 47

Warning: Cannot modify header information - headers already sent by (output started at /home/psitinc/public_html/mymail.php:6) in /home/psitinc/public_html/mymail.php on line 48

Warning: readfile(/public_html/downloads/gadVibroScreenSingleMotor.pdf) [function.readfile]: failed to open stream: No such file or directory in /home/psitinc/public_html/mymail.php on line 49

当我用Google搜索错误时,他们告诉我检查我检查过的空格,但没有。

即使我提供绝对路径(/public_html/downloads/gadSingleDeckScreen.pdf)文件下载也无法正常工作

<?php 
ini_set('session.bug_compat_warn', 0);
session_start();
$id = isset($_SESSION['id']) ? (int)$_SESSION['id'] : null;
$name = isset($_SESSION['name']) ? (int)$_SESSION['name'] : null;
print_r($_SESSION);
$name = $_POST["name_first"];
    if (empty($name))
        {
            echo "<h3>Enter your name</h3>";
            exit;
        }
$mail = $_POST['email'];
    if (empty($mail))
        {
            echo "<h3>Email field required</h3>";
            exit;
        }
    elseif(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $mail)) 
        {
            echo"<h3>Sorry the email address you entered is invalid please try again</h3>";
            exit;
        }
$number = $_POST['phone_number'];
$email_message = "First name: {$name}   Email ID:  {$mail}      Number   {$number} ";
    mail('vinferrari@gmail.com', 'Form Response', $email_message);  
        if (empty($name) || empty($mail) || empty($number))
                {
                        echo "<h3>Enter all fields</h3>  ";
                        $url = htmlspecialchars($_SERVER['HTTP_REFERER']);
                        header("Location: $url");
                        exit;
                }
            elseif ($id==1002)
                {
                 header('Content-type: application/octet-stream');
                 header('Content-Disposition: attachment; filename="gadSingleDeckScreen.pdf"');
                 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                 readfile('gadSingleDeckScreen.pdf');
                 exit;
                }
            ?>

2 个答案:

答案 0 :(得分:2)

路径很可能 /public_html/downloads/gadVibroScreenSingleMotor.pdf。前导/表示文件系统的根目录。

您的意思是其中之一吗?

  • downloads/gadVibroScreenSingleMotor.pdf
  • /var/www/html/public_html/downloads/gadVibroScreenSingleMotor.pdf

答案 1 :(得分:1)

你正在第5行做一个print_r。这会发送数据,这意味着标题已经发送,因此就是消息。