用PHP下载PDF

时间:2016-12-09 03:28:19

标签: php pdf file-put-contents

$ext = pathinfo($url, PATHINFO_EXTENSION);
if ( $ext == 'pdf' || $ext == 'doc' || $ext == 'docx') {
  file_put_contents('product-docs/'.$slug.'-'.$r.'.'.$ext, $url);
}

我希望此代码能够下载文档,而不是使用正确的扩展名保存文件,但在编辑器中打开它会显示文件内容是URL本身。

1 个答案:

答案 0 :(得分:0)

这有效:

if ( $ext == 'pdf' || $ext == 'doc' || $ext == 'docx') {
  $doc = file_get_contents($url);
  file_put_contents('product-docs/'.$slug.'-'.$r.'.'.$ext, $doc);
}