PHP para descargar PDF arroja error

I have a PHP file to force download a PDF file, on one server (linux) it worked fine, but when I moved the site to another server (Win) it started to give this error:

PHP Warning:  readfile(./forms/Form.pdf)
[<a href='function.readfile'>function.readfile</a>]:
failed to open stream: No such file or directory in
D:\CustomerData\webspaces\webspace\wwwroot\Form.php
on line 4

The PHP file has this:

<?php
header('Content-disposition: attachment; filename=./forms/Form.pdf');
header('Content-type: application/pdf');
readfile('./forms/Form.pdf');
?>

¡Gracias!

preguntado el 31 de julio de 12 a las 10:07

Quizas filename=../forms/Form.pdf? -

It's probably just backslash vs (forward)slash? -

Maybe the file doesn't exist in the froms path or your web server identity doesn't have permissions on the path? Have you tried to use the absolute path to the form? Does the error persist? -

ok I just tried the forward/backslash, and I tried absolute paths, both give me the same thing, and in the browser title it reads 'Access to the php file denied'. -

i just realised the contact form is also not working like it should, so there must be something with the PHP on the server, what could it be? -

1 Respuestas

Try to use absolute file location. I thought this error because of file path

Prueba el siguiente código.

$file = <absolute file path>;
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
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));
print_r(headers_list());
readfile($file);

Respondido 31 Jul 12, 10:07

Be careful when setting the Content-length header - I've had trouble with it in the past when used in conjunction with HTTP compression. - Mateo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.