是否可以将PHP readfile()与其他服务器上的文件一起使用


Is it possible to use PHP readfile() with a file on a different server?

如果PDF文件不在我的服务器上,而是在具有不同IP的不同服务器上,我可以使用下面的PHP代码吗?如果没有,最好的替代品是什么?

这个代码片段取自这个PHP教程:http://www.devshed.com/c/a/PHP/Simple-and-Secure-PHP-Download-Script-with-Limits-Tutorial/

//Define the content type and show force download attachment dialog
header("Content-type:application/pdf");
header('Content-Disposition: attachment; filename="http://www.yourdomain.com/ebookfordownloads/ebook.pdf"');
//Stream the PDF file for downloading using PHP readfile function
//The readfile is using absolute server path
//You can determine this path by uploading a php script to the folder containing the download material.
/*
<?php
echo $_SERVER['SCRIPT_FILENAME'];
?>
*/
readfile("/your/absolute/server/path/html/ebookfordownloads/ebook.pdf");

如果PDF文件不在我的服务器上,而是在具有不同IP的不同服务器上,我可以使用下面的PHP代码吗?如果没有,最好的替代品是什么?

是的,这是可能的,具体取决于您的服务器设置。它必须允许连接到其他服务器(allow_url_fopen应该为true)才能做到这一点

是的,fopen包装器也适用于readfile,除非管理员将allow_url_fopen设置为false。您可能需要提供$context参数来微调HTTP行为。