I am downloading a file from Ftp folder after upload.
Problem is when i open the txt file it show s html page source appending with file content
If i preview image file(jpg or jpeg) it shows image is corrupted
If i open pdf error: Failed to load Pdf document
Please let me know where i am wrong.
Here is my code:
if (isset($_GET['id']) && basename($_GET['id']) == $_GET['id']) {
$filename = $_GET['id'];
} else {
$filename =NULL ;
}
$err = 'Sorry, the file you are requesting is unavailable.';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo $err;
} else {
// define the path to your download folder plus assign the file name
$path = '/home/devestctrl/public_html/wp-content/uploads/'.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
// get the file size and send the http headers
$size = filesize($path);
header('Content-Type: application/octet-stream');
header('Content-Type: '.$mime);
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error messages if the file can´t be opened
$file = @ fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit();
} else {
echo $err;
}
} else {
echo $err;
}
}
I checked by using echo of $filename;
It shows the output in the file instead of printing in page.
error displays:
Sorry, the file you are requesting is unavailable.
After error also i can download, but echoed $filename displayed in file.
Insert into table:
echo "<tr><a href='?id=" . $row["FileupName"]. "'>".$row["FileupName"]."</td></tr>";