PHP 下载无法运行

发布于 2024-11-08 19:48:22 字数 2600 浏览 0 评论 0原文

我编写了一个简单的 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

当我用谷歌搜索这个错误时,他们告诉我检查空格,我已经检查过,但没有。

即使我提供绝对路径(/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('[email protected]', '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;
                }
            ?>

I have written a simple php script for allowing a user to download a file after submitting his data through a form. It was working fine for a few days and today this error message appeared

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

When I googled about the error they told me to check for whitespaces which I have checked and there are none.

Also the file download is not working even if I provide absolute path(/public_html/downloads/gadSingleDeckScreen.pdf) it says file not found on the server

<?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('[email protected]', '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;
                }
            ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落墨 2024-11-15 19:48:22

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

您指的是其中之一吗?

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

The path is very likely not /public_html/downloads/gadVibroScreenSingleMotor.pdf. A leading / means the root of your filesystem.

Did you mean one of these?

  • downloads/gadVibroScreenSingleMotor.pdf
  • /var/www/html/public_html/downloads/gadVibroScreenSingleMotor.pdf
陈甜 2024-11-15 19:48:22

您正在第 5 行执行 print_r。这会发送数据,这意味着标头已经发送,因此消息也已发送。

You are doing a print_r on line 5. This sends data, which means the headers have already been sent, thus the message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文