To have an address as a wordpress page like https://example.com/download
, I created a php file in custom plugin to download test.pdf
file and use it as wordpress shortcode inside the mentioned page, additionally my pdf file is in followed address: https://example.com/wp-content/uploads/test.pdf
. Even though browser loads this url well, in script environment the file_exist returns false and I can not load the pdf file by loading https://example.com/download
address
$upload_dir = wp_upload_dir();
$upload_basedir = $upload_dir['basedir'];
$file = $upload_basedir . '/test.pdf';
if(file_exists($file)) {
// header("Content-Type: application/octet-stream");
header("Content-type : application/pdf");
header("Content-Disposition : attachment; filename='test.pdf'");
header("Content-Length: " . filesize("test.pdf"));
readfile($file);
die;
}