PHP download file from SFTP - php

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;

Related

Extra data in downloaded files (PHP)

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>';
}
}
?>

compress csv before export in php

Here is my code. Its working properly and now i want to compress the csv file before generate. Anybody please suggest me what to do!
$filename = 'Product_Export_' . date('Y-m-d') . '.csv';
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=' . $filename);
echo "\xEF\xBB\xBF"; // UTF-8 BOM
// clean the output buffer
ob_clean();
echo trim($_SESSION['cProductCSVdata']);
$_SESSION['cProductCSVdata'] = '';
exit;
Sending it to a zip file with ZipArchive is your solution.
Here is an example with .txt files.
<?php
$zip = new ZipArchive();
$filename = "./test112.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n");
$zip->addFile($thisdir . "/too.php","/testfromfile.php");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
?>

Why I'm unable to download a pdf file which is already present onto server?

Following is the code:
define(ADMIN_ROOT,'/var/www/abc/pqr/web/control/');
$filename = $test_data['test_name'];
$flname = ADMIN_ROOT.'modules/tests/test_pdfs/'.$filename;
header("Content-Type: application/pdf");
header("Cache-Control: no-cache");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"" .$flname. ".pdf\"");
The pdf file is already present over there at the directory
/var/www/abc/pqr/web/control/modules/tests/test_pdfs/
Also it has all the permissions assigned.But when I try to download a file, it's downloading some different file with same name but the file is not in a format which could be opened. In short the desired file is not getting downloaded. Can anyone please help me in correcting my issue?
You must flush the file contents to the browser, headers won't do that.
So: readfile( $flname ) http://php.net/manual/pt_BR/function.readfile.php
Please try this
<?php
$filename = $test_data['test_name'];
$file = dirname(__FILE__) . "/modules/tests/test_pdfs/" . $filename;
//$file = "/var/www/htdocs/audio/" . $filename;
if (file_exists($file)) {
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=" . str_replace(" ", "_", basename($file)) . ";");
header ("Content-Length: " . filesize($file));
readfile($file);
die();
}
else {
//ERROR MESSAGE HERE..........
}
?>
Please change this line
header("Content-Type: application/pdf");
to
header ("Content-type: octet/stream");
You can also use the following code for download any type of file extensions:
<?php
$filename = $test_data['test_name'];
$contenttype = "application/force-download";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
readfile(ADMIN_ROOT.'modules/tests/test_pdfs/'.$filename);
exit();
?>

Cant pass value as variable in this php script

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

WKHTMLTOPDF with PHP not accepting args for page size?

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.

Categories