简体   繁体   中英

PHP file download with Wordpress works on localhost but not live server

I'm currently having some problems with forcing a file download on a Wordpress site I'm making. On my localhost it works fine but when I try it to the live server all file downloads are broken. For instance, images are not displaying, video and audio files don't play etc.

This is my code:

$title      = $query[0]->post_title;
$mimetype   = $query[0]->post_mime_type;
$guid       = $query[0]->guid;

$filepath   = str_replace(home_url().'/wp-content/uploads/', '', $guid);
$download   = ABSPATH.'/wp-content/uploads/' . $filepath;
$filesize   = filesize($download);          

//Download file
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.$title);
header('Content-Type: '.$mimetype);
header('Content-Transfer-Encoding: '.$mimetype);
header('Content-Length: '.$filesize);   
ob_clean();
flush();
readfile($download);
exit();

If I inspect the response in Chrome it says "Failed to load response data" both in the preview and response tab.

Any ideas? any help is appreciated.

Would add this as a comment but can't. It will be using ABSPATH that's giving you grief. I'd echo out your $download path and make sure it is correct. Look here - ABSPATH or __FILE__?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM