开发者

PHP download not working

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/downl开发者_StackOverflow中文版oads/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('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;
                }
            ?>


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜