-1

I try to do so that I can download files but I have a problem and don't know how to fix it can you help me find the error?

Nothing happens. And help me to get this to work. I don't know how to fix this so need all the help I can get.

<?php

require("configuration.php");
require("include.php");

require_once('./libs/phpseclib/SFTP.php');
require_once("./libs/phpseclib/Crypt/AES.php");


if(!isset($_SESSION['clientid']))
{
    //DON'T KNOW HOW THE REQEUSTOR IS!!
    die();
}

$clientid = $_SESSION['clientid'];

$serverid = '';
$extendedPath = '';
$action = '';

if(!isset($_GET['serverid']) or !isset($_GET['path']) or !isset($_GET['action']))
{
    die();
}


$serverid = $_GET['serverid'];
$extendedPath = $_GET['path'];
$action = $_GET['action'];

$boxDetailsSQL = sprintf("SELECT box.boxid, box.ip, box.login, box.password, box.sshport, srv.path
                            FROM %sbox box
                            JOIN %sserver srv ON box.boxid = srv.boxid
                            JOIN %sgroupMember grpm ON (grpm.groupids LIKE CONCAT(srv.groupid, ';%%')
                                                      OR grpm.groupids LIKE CONCAT('%%;', srv.groupid, ';%%'))

                            WHERE srv.serverid = %d
                            AND grpm.clientid = %d;", DBPREFIX, DBPREFIX, DBPREFIX, $serverid, $clientid);

$boxDetails = mysql_query($boxDetailsSQL);
$rowsBoxes = mysql_fetch_assoc($boxDetails);

$aes = new Crypt_AES();
$aes->setKeyLength(256);
$aes->setKey(CRYPT_KEY);

$sftp= new Net_SFTP($rowsBoxes['ip'], $rowsBoxes['sshport']);

if(!$sftp->login($rowsBoxes['login'], $aes->decrypt($rowsBoxes['password'])))
{
    echo 'Failed to connect';
    die();
}


//ACTION SELECTOR
if($action == 'list')
{
    getlist($rowsBoxes, $extendedPath, $sftp);
}   

if($action == 'fileUpload')
{
    fileUpload($rowsBoxes, $extendedPath, $sftp);

}   

if($action == 'download')
{
    delete($rowsBoxes, $extendedPath, $sftp);   
}   

//ACTION FUNCTIONS

function download($rowsBoxes, $extendedPath, $sftp)
{
    $remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
    $downloadfile $sftp->put($remoteFile);

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($downloadfile));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($downloadfile));
    readfile($downloadfile);

}

This code I need help to fix.

function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile $sftp->put($remoteFile);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($downloadfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($downloadfile));
readfile($downloadfile);

}
neubert
  • 15,947
  • 24
  • 120
  • 212
Big Loser
  • 1
  • 4
  • 1
    [Here is an example of what happens when you continue to use `mysql_*` functions.](http://stackoverflow.com/questions/26299564/php-version-upgraded-cannot-use-few-functions) Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 16 '14 at 17:44
  • Try to add some messages to your `die()` commands, so you'll know *where* it stopped. Also, try adding `ini_set('display_errors', 1); error_reporting(-1);` to the top of your file to have PHP display all errors. – gen_Eric Oct 16 '14 at 17:44
  • Need help to download files files. What do you mean with mysql is not safe? – Big Loser Oct 16 '14 at 17:50
  • The unctions that start with `mysql_` are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). – Jay Blanchard Oct 16 '14 at 17:55
  • It is this code that I need help with a. coming after this. "This code I need help to fix." – Big Loser Oct 16 '14 at 18:01

1 Answers1

0

You're calling getList, fileUpload or delete, depending on the value of $_GET['action'] but you're not calling download at all. You've defined that function but you aren't calling it in your code snippet. And in your download function...

$downloadfile $sftp->put($remoteFile);

You should probably be doing this instead:

$downloadfile = $sftp->get($remoteFile);

Also, instead of doing filesize($downloadfile) do strlen($downloadfile) and instead of readfile($downloadfile) do echo $downloadfile;. If you want to save to a local file do $sftp->get($remoteFile, $downloadfile);

Updated code:

function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile = $sftp->get($remoteFile);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($downloadfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($downloadfile));
echo $downloadfile;

}

If you want to do something like display an image do this:

function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile = $sftp->get($remoteFile);

$file_extension = strtolower(substr(strrchr($filename,"."),1));

switch( $file_extension ) {
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpeg":
    case "jpg": $ctype="image/jpg"; break;
    default:
}

header('Content-type: ' . $ctype);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
echo $downloadfile;

}
neubert
  • 15,947
  • 24
  • 120
  • 212
  • Instead of doing `filesize($downloadfile)` do strlen($downloadfile)` and instead of `readfile($downloadfile)` do `echo $downloadfile;`. If you want to save to a local file do `$sftp->get($remoteFile, $downloadfile);` – neubert Oct 17 '14 at 19:44
  • Want it's hot on the web site to download the file. How do I fix it? – Big Loser Oct 17 '14 at 19:58
  • You would have to write code so that I better understand it? – Big Loser Oct 17 '14 at 20:03
  • I've updated my orig post to include a rewritten version of your function. – neubert Oct 17 '14 at 20:07
  • I am trying to do so that it comes up like this but it does not work the way you work. But when I enter it here then download the file and save it to the web server. echo $sftp->get($remoteFile,'test.jpg'); http://php.net/manual/en/images/e88cefb5c3fca5060e2490b9763c4433-readfile.png – Big Loser Oct 17 '14 at 22:05
  • I've updated my post to do something like http://stackoverflow.com/a/2634072/569976 . Is that what you're wanting? – neubert Oct 18 '14 at 02:05
  • I want them to be able to download all kind of files. – Big Loser Oct 18 '14 at 17:55
  • I've posted two different re-implementations of your `download()` function. The first one let's people "download all kinds of files". So what exactly do you want? Something that'll download files that aren't images and that'll display images in-line? Something else? This is beginning to feel more like a "hey can you do this program for me?"-type question instead of a "hey I don't know how to do this"-type question... – neubert Oct 18 '14 at 20:25