I had a problem with my code . when I click the button to download the picture , it appears the code that I do not know, how to solve this ? what is wrong with my code ?
--> Download.php
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
$filename = $_GET['file'];
} else {
$filename = NULL;
}
// define error message
$err = '<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>';
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 = '../images/upload/lowongan_pekerjaan/'.$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-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error message if 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;
}
}
why show like this?
My Picture
whats wrong with my code?
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
should be
header('Content-Type: image/jpg');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
or png or gif or whatever your file type is.. the browser does not know how to handle what you are sending it because application/octet-stream != an image type.
Related
The code below displays this:
/home/my_site/www/wp-content/uploads/2017/03/2017-03-17-my_file.mp3
As far as I can tell, this path is correct. Yet when I comment away the echo and download the file, it's an empty file. Why?
Code:
if (isset($_GET['file'])) {
clearstatcache();
$file_path = str_replace('http://www.example.com/', '', $_GET['file']);
$file_path = $_SERVER['DOCUMENT_ROOT'] . '/' . $file_path . '.mp3';
echo $file_path;
if(file_exists($file_path)) {
$file_name = basename($file_path);
$file_size = filesize($file_path);
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".$file_size);
header("Content-Disposition: attachment; filename=".$file_name);
exit();
}
else {
die('The provided file path is not valid.');
}
}
EDIT: this is what I have after KIKO Software's suggestion. Still downloads empty file.
<?php
if (isset($_GET['file'])) {
$file_path = $_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['file'] . '.mp3';
//echo $file_path;
//$file_path = $_SERVER['SCRIPT_FILENAME'];
if (file_exists($file_path)) {
$file_name = basename($file_path);
$file_size = filesize($file_path);
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$file_size);
header('Content-Disposition: attachment; filename='.$file_name);
//readfile($file_path);
exit();
}
else {
die('The provided file path is not valid.');
}
}
?>
Simple example to download a file:
$content = 'This is the content.';
$size = strlen($content);
$name = 'test.txt';
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$name);
echo $content;
Make sure this works first. If it doesn't there could be a problem with the headers. Switch on error reporting and see: How to fix "Headers already sent" error in PHP
Then build up on that. Start by downloading the script itself:
$file_path = $_SERVER['SCRIPT_FILENAME'];
if (file_exists($file_path)) {
$file_name = basename($file_path);
$file_size = filesize($file_path);
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$file_size);
header('Content-Disposition: attachment; filename='.$file_name);
readfile($file_path);
exit();
}
else {
die('The provided file path is not valid.');
}
And only after that try to download something else.
By approaching the problem step by step it is easier to see where it goes wrong.
I have tried all sorts of codes I've found but nothing has worked for me...
When I use readfile() or fopen() or something like this:
function makeDownload($file, $dir, $type) {
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($dir.$file);
}
... A download is started but the file is always empty...
here's the last code I've tried:
$filename = "gandalf.jpg";
// define error message
$err = '<p style="color:#990000">Sorry, the file you are requesting is unavailable.</p>';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo " filename NULL";
echo $err;
} else {
// define the path to your download folder plus assign the file name
$path = 'upload/'.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
echo "file exists";
// get the file size and send the http headers
$size = filesize($path);
header('Content-Type: image/png');
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 message if file can't be opened
//readfile($path.$filename);
$file = # fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
} else {
echo $err;
}
}
This is the source of my code: Source code
I hope you can help me :)
I use google chrome
I need your help for this little project: Link to Project
When you click on a file in the drop down appears "herunterladen" (download in german) here is where I want to start the php download code
I just need the most basic download function for PHP... why is there an upload example on w3schools but NOT a download example? oO
$fileSource= $_GET['fileSource']; //If you are passing the filename as a URL parameter
$fileSource= "sample.pdf" //If the filename is fixed in the current folder
if($fileSource) {
$filesize = filesize($fileSource);
$path_parts = pathinfo($fileSource);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-Disposition: attachment;
filename=\"".$path_parts["basename"]."\""); // use 'attachment' to
force a download
header("Content-type: application/pdf"); // add here more headers
for diff. extensions
break;
default:
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
if($filesize) {
header("Content-length: $filesize");
}
readfile($fileSource);
exit;
}
Here is my code. I wrote this to serve mp3, images and videos. Mp3 files are received and working well. Images and videos are received but corrupted. I'm new to php.
<?php
if( !empty( $_GET['type']|| $_GET['name']|| $_GET['ext'] ) ) {
// check if user is logged
if(true) {
$type = preg_replace( '#[^-\w]#', '', $_GET['type'] );
$name = $_GET['name'] ;
$file = "{$_SERVER['DOCUMENT_ROOT']}/cont/{$type}/{$name}";
echo $file;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
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;
}
}
}
die( "ERROR: invalid file or you don't have permissions to download it." );
Can you help me?
There is an echo $file; before the actual response that doesn't belong there.
Besides the text itself, PHP also produces a Warning on the first call to header(). Both these arrive in the final content before the file header and makes the data received by the browser unrecognizable by most file readers.
I have problem in downloading file(php)from a particular folder.
when i download and open the file it says your file is corrupted.
when i check the size of the uploaded file and downloaded file it is same , but for zip file size it differs.
No Files are opening.
can any one say where i am wrong???
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
$filename = $_GET['file'];
}
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 = '/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 ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment;filename="'.basename($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;
}
exit;
}
inserting into table:
echo "<tr><td><a href='?file=" . $row["FileupName"]. "'>".$row["FileupName"]."</td></tr>";
I am happy that the file is getting downloaded but it not getting opened.
.txt file is getting opened.
Had checked with header also.
i have tried putting:
ob_clean();
flush();
readfile($file);
if (file_exists($path)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);
exit;
}
the code I am using:
function DownLoading($Peter)
{
// if(ini_get('zlib.output_compression'))
// ini_set('zlib.output_compression', 'Off')
$File = "";
$Filename = "";
If ($Peter == "Farm") {
$File = "TestFile.txt";
$FileName = $File; //"TestFile.txt";
$len = filesize($File); // Calculate File Size
if (file_exists("TestFile.txt")) {
header('Content-Description: File Transfer');
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="TestFile.txt"');
header('Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . $len);
ob_clean();
$wasdownloaded = readfile("TestFile.txt");
if ($wasdownloaded === false)
echo "error";
else {
echo "no error";
Flush();
exit;
}
}
}
}
The strange thing is that if I run this code on wamp server then it works fine (no echoeing, but downloading)
The following is echoed to the sdcreen: This is a test file!!! Nowno error
whereby "This is a test file!!! NOW" is the files's content
Can somebody help me?
I did something very stupid that was causing the same thing. Basically, as already said, you must ensure nothing has been sent already.
I had put a space before the <?php opening declaration - and that was the problem!