使用 PHP 下载 PDF

Downloading PDF with PHP

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

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

这有效:

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