I am trying to download an image from website using php with the following code.
Currently it idendtify and downloads the file. but the file downloaded is not opening and it shows as corrupted. what is the issue in the below code ?
Also I am embedding this page in one of my mobile app. will it work in mobile android devices too
<?php
$file = "http://example.com/animals/1.jpg";
// Parse Info / Get Extension
$fsize = filesize($file);
$path_parts = pathinfo($file);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext)
{
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: die('Wrong Extension');
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"Test".basename($file)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);
ob_clean();
flush();
readfile($file);
exit();
?>
You need to send data stream to browser.
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream'); // change this will work for you.
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
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.