I am trying to set up a PHP page to be able to download some log files.
I have tried searching (very hard) for a solution, but I don't really know PHP or HTML, so I have tried many snippets from many sources.
<?php
$dir = "/home/pi/fluidLogs";
$phpfiles = glob("$dir/log*.txt");
foreach ($phpfiles as $phpfile) {
echo '<a href=?file=' . $phpfile . '>' . basename($phpfile) . '</a>';
echo ' - Last mod: ' . date("Ymd-H:i:s", filemtime($phpfile));
echo ' - Size: ' . filesize($phpfile);
echo '</a><br>';
}
if(isset($_GET['file'])){
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($_GET['file']) . "\"");
readfile($_GET['file']);
}
?>
For example one file should contain,
This is a fake log file.
OK?
with a new line before OK?, but this (and all the downloaded files (via Chrome)) have the same extra data on the first line,
Sorry about the image. The contents were not being displayed correctly.
If user is requesting for a file, i.e. clicked on your anchor link, do not echo the list of files, which are then echoed on top of the file.
<?php
if(isset($_GET['file'])){
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($_GET['file']) . "\"");
readfile($_GET['file']);
}
else{
$dir = "/home/pi/fluidLogs";
$phpfiles = glob("$dir/log*.txt");
foreach ($phpfiles as $phpfile) {
echo '<a href=?file=' . $phpfile . '>' . basename($phpfile) . '</a>';
echo ' - Last mod: ' . date("Ymd-H:i:s", filemtime($phpfile));
echo ' - Size: ' . filesize($phpfile);
echo '<br>';
}
}
?>
Related
I'm trying to download files from another server using ssh2. I already can execute commands with ssh2, but i need to download a file located in /root/
I tried with:
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=backup.vps");
echo file_get_contents('ssh2.sftp://' . $row['login'] . ':' . $row['senha'] . 'pass#'. $row['ip'] . ':22/root/backup.vps');
But it send a blank file. What's the problem?
I resolved it by changing from this:
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=backup.vps");
echo file_get_contents('ssh2.sftp://' . $row['login'] . ':' . $row['senha'] . 'pass#'. $row['ip'] . ':22/root/backup.vps');
To this:
$result = file_get_contents('ssh2.sftp://' . $row['login'] . ':' . $row['senha'] . 'pass#'. $row['ip'] . ':22/root/backup.vps');
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=backup.vps");
echo $result;
Like the question says, I can't seem to pass a value in a variable in the following script. If I echo the variable, it exists as expected. If i copy and paste the echoed value into the code where $my_var is, it works. But it wont work with $my_var ?!?!?
Context- code requires another file to create a pdf, attaches it to an email and sends it, and then displays it in the browser. Have removed most of the code for brevity
$my_var = $_POST['quote_number'];
$filename = 'Quote_no' . $my_var . '.pdf';
$file = $_SERVER['DOCUMENT_ROOT'] . '/quotes/' . $filename ;
require('instant_quote.php');
function send_quote($my_var) {
//my_function code
};
send_quote($my_var);
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
The syntax highlighting in your example was helpful, you have incorrect matching quotes:
$filename = "Quote_no' . $my_var . '.pdf";
... should be:
$filename = 'Quote_no' . $my_var . '.pdf';
Don't know why this worked, but it did...
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $file . '"');
header('Content-Length: ' . filesize($filename));
#readfile($filename);
Just removed the transfer encoding and accept-ranges from the headers, and it started accepting my variable as a value... go figure
I'm using WKHTMLTOPDF for a client. My PHP is quite rough... The problem is, my arguments aren't working, and in the below example they could be wrong, but none are working... such as zoom, page-size, etc. So for example, in the switch statement, you can see the Building type. I want this to output as a standard US Letter type. --page-size A, or is it --page-size "A", or --page-size "Letter"? But regardless, I'm not passing the arguments properly, because no matter what I change, (i.e. --zoom .10) for testing, nothing changes. Any help is appreciated!
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
try {
$mydir = getcwd();
// Figure out the URL
$url = $_GET['url'];
$url = str_replace(';', ' ', $url);
// Figure out the mode
$mode = isset($_GET['mode']) ? $_GET['mode'] : 'standard';
// Figure out the mode
$name = isset($_GET['name']) ? '-' . $_GET['name'] . '-' : '';
// Generate GUID filename
$file = $mydir . '\\' . uniqid('', true) . '.pdf';
// Build arguments
$args = '
--load-error-handling ignore
';
switch($mode) {
case 'Site';
$args .= ' --title "Site Report"';
$filename = 'Site-Report' . $name . '.pdf';
break;
case 'Building';
$args .= ' --title "Building Report" --page-size "A"';
$filename = 'Building-Report' . $name . '.pdf';
break;
case 'standard';
default;
$args .= ' --title "Development Report" --zoom .55';
$filename = 'Development-Web-Report.pdf';
break;
}
// Build the command
putenv("path=" . getenv("path") . ';"C:\Program Files (x86)\wkhtmltopdf"');
$cmd = escapeshellcmd('CMD /c wkhtmltopdf ' . $url . ' ' . $file);
$com = new COM("WScript.Shell");
$shell = $com->Exec($cmd);
$shell->StdErr->ReadAll;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=' . $filename);
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($file));
ob_clean();
flush();
readfile($file);
unlink($file);
exit;
}
} catch (Exception $e) {
echo $e->getMessage();
}
?>
You are not even attaching your arguments to command string.
Try this:
$cmd = escapeshellcmd('CMD /c wkhtmltopdf ' . $args . ' ' . $url . ' ' . $file);
Also good to know that on Windows you cannot use absolute paths (like C:\) to point to a html file since it expects url's or relative paths, so you must use wkhtmltopdf file:///d:/in.html out.pdf, also note the / instead of \. This is true even for the latest version (0.11.0 rc1), although in future it may support absolute system paths for win.
i have a link pointing to a music file on my site, now the name of the file was hashed when it was uploaded so i want to use the original filename which i have stored in my database, I did some research and found this new 'download' attribute for the 'a' tag but that only works in later versions of firefox and chrome, it doesn't work in ie and also doesn't work with the download manager i use so i checked online and found out about headers which i then implemented. I now get the filename changed allright but the music file keeps on getting saved as a '11.35kb' filesize no matter the music file i try to download. This is my code:
if (isset($_REQUEST['download']))
{
$download_id = $_REQUEST['download'];
$db = new MysqliDatabase(ConnectionString);
$result = array();
$result = $db->query_one("SELECT TrackID, ma.ArtisteName, FeaturedArtistes,
mc.Category, TrackName
FROM `musictracks` mt
LEFT JOIN `musiccategories` mc
ON mt.CategoryID = mc.CategoryID
LEFT JOIN `musicartistes` ma
ON mt.ArtisteID = ma.ArtisteID
WHERE mt.TrackID = '$download_id';");
$filename = $result->TrackPath;
$outputfilename = $result->ArtisteName . ' ft. ' . $result->FeaturedArtistes . ' - ' . $result->TrackName . '.mp3';
header("Content-Type: audio/mpeg");
header("Content-Disposition: attachment; filename=\"" . basename($outputfilename) . "\";" );
header("Content-Transfer-Encoding: binary");
readfile("$filename");
}
And this is the download link:
<a href="<?php echo 'musicdownload.php?download='. $row->TrackID ?>" ><img src="images/download.png" alt="download" title="download" width="14" height="14" /></a>
My PHP is a bit rusty but I can think of one thing with your code, no content length header. Update your code to this and see if that works:
if (isset($_REQUEST['download'])) {
{
$download_id = $_REQUEST['download'];
// ...
$filename = $result->TrackPath;
$outputfilename = $result->ArtisteName . ' ft. ' . $result->FeaturedArtistes . ' - ' . $result->TrackName . '.mp3';
if (file_exists($filename)) {
header("Content-Type: audio/mpeg");
header("Content-Disposition: attachment; filename=\"" . basename($outputfilename) . "\";" );
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);
exit;
}
}
}
Note that we use flush(); to send the headers to the browser before we start downloading the actual file. And I also added an if (file_exists($filename)) to make sure we have a file to send. I'd recommend you put an else clause there to give you something that will show you if you don't have a file like you expect...
header("Content-Type: application/force-download");
header("Content-Type:audio/mpeg");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=".$file_name);
Please download your mp3 files using curl
Here is sample code for your reference
<?php
if(isset($_REQUEST['inputurl']) && $_REQUEST['inputurl']!="") {
$file = $_REQUEST['inputurl'];
header("Content-type: application/x-file-to-save");
header("Content-Disposition: attachment; filename=".basename($file));
readfile($file);
}
?>
<form name="from" method="post" action="">
<input name="inputurl" type="text" id="inputurl" value="" />
<input type="submit" name="Button1" value="Get File" id="Button1" />
</form>
may be it will help you.
I have this page that is supposed to be a download for a song. The download works in firefox for me but in chrome and safari nothing happens..here is my code
public function download() {
if (isset($this->request->get['order_download_id'])) {
$order_download_id = $this->request->get['order_download_id'];
} else {
$order_download_id = 0;
}
$download_info = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_download od LEFT JOIN `" . DB_PREFIX . "order` o ON (od.order_id = o.order_id) WHERE o.customer_id = '" . (int)$this->customer->getId(). "' AND o.order_status_id > '0' AND o.order_status_id = '" . (int)$this->config->get('config_download_status') . "' AND od.order_download_id = '" . (int)$order_download_id . "'");
if ($download_info->row) {
$file = DIR_DOWNLOAD . $download_info->row['filename'];
$mask = basename($download_info->row['mask']);
$mime = 'application/octet-stream';
$encoding = 'binary';
if (!headers_sent()) {
if (file_exists($file)) {
header('Pragma: public');
header('Expires: 0');
header('Content-Description: File Transfer');
header('Content-Type: ' . $mime);
header('Content-Transfer-Encoding: ' . $encoding);
header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
header('Content-Length: ' . filesize($file));
$file = readfile($file, 'rb');
print($file);
} else {
exit('Error: Could not find file ' . $file . '!');
}
} else {
exit('Error: Headers already sent out!');
}
}
}
I have tried all kinds of different things to get this to work but nothing is happening in the two browsers...any ideas or help will be appreciated...
readfile returns the number of bytes sent, and needs not to be printed out. You should remove the line print($file);. Otherwise, you'll send more bytes than the Content-Length header specifies, and that will lead some HTTP clients to discard your answer.
Also, consider strange file names such as
"\r\nLocation: http://evil.com\r\n\r\n<script>alert('XSS');</script>
Are you handling that correctly?
See your syntax near
header('Content-Disposition: attachment; filename="'.$file_name_with_space. '"');
OR it can be
header("Content-Disposition: attachment; filename='".$file_name_with_space."'" );
Here the game is in Quotes only it will be treated as part of the string if it is written properly else will crash.
It works in all browser. IE, FF, Chrome, SAFARI I checked it personally so goahead.