Asked  1 Year ago    Answers:  5   Viewed   7 times

So what it does is successfully connects then uploads the file logo.png but the contents of the file isn't what was on web host or uploaded with html if i use a html upload code. What it puts in the file is the ACTUAL text between the second ' 's so for that very example the contents of logo.png is literally logo.png and not the picture.

require_once("ftp/vendor/autoload.php");

use phpseclibNetSFTP;

$sftp = new SFTP('SERVER');

if (!$sftp->login('USER', 'PW')) {
    throw new Exception('Login failed');
}

$sftp->put("/some/path/logo.png", "logo.png", NET_SFTP_LOCAL_FILE);

 Answers

3

If you would read through the documentation, you would find out that the second argument of the put() function is $data, therefore not the file path, but the actual data to write:

function put($remote_file, $data, $mode = NET_SFTP_STRING, $start = -1, $local_start = -1)

By default, NetSFTP::put() does not read from the local filesystem. $data is dumped directly into $remotefile. [...]

To upload a local file, the easiest way is to read the content into one variable that will be passed to the put() function:

$data = file_get_contents("logo.png");
$sftp->put("/some/path/logo.png", $data);

Edit: You are probably using a new version of phpseclib, which renamed these constants to make them more object-like. With a new version, you should use

$sftp->put("/some/path/logo.png", "logo.png", SFTP::SOURCE_LOCAL_FILE);
Thursday, April 1, 2021
 
tpow
 
1

http://phpseclib.sourceforge.net/documentation/intro.html#intro_usage_correct

Per that, phpseclib's root directory needs to be in your include_path. From that page:

<?php
  set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

  include('Net/SFTP.php');
?>

You should really familiarize yourself with this kind of include technique - it's pretty standard for PEAR libraries and Zend ones.

Thursday, April 1, 2021
2

I don't think your put is doing what you think it is doing. According to the docs, you need to do a Net_SFTP::chdir('/some-dir/') to switch to the directory you want to send file to, then put($remote_file, $data), where remote_file is the name of file, and $data is the actual file data.

Example Code:

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

echo $sftp->pwd() . "rn";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>
Saturday, May 29, 2021
 
Trott
 
5

First of all, the first rule of multipart Content-Type is to define a boundary that will be used as a delimiter between each part (because as the name says, it can have multiple parts). The boundary can be any string that is not contained in the content body. I will usually use a timestamp:

define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));

Once your boundary is defined, you must send it with the Content-Type header to tell the webserver what delimiter to expect:

$header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;

Once that is done, you must build a proper content body that matches the HTTP specification and the header you sent. As you know, when POSTing a file from a form, you will usually have a form field name. We'll define it:

// equivalent to <input type="file" name="uploaded_file"/>
define('FORM_FIELD', 'uploaded_file'); 

Then we build the content body:

$filename = "/path/to/uploaded/file.zip";
$file_contents = file_get_contents($filename);    

$content =  "--".MULTIPART_BOUNDARY."rn".
            "Content-Disposition: form-data; name="".FORM_FIELD.""; filename="".basename($filename).""rn".
            "Content-Type: application/ziprnrn".
            $file_contents."rn";

// add some POST fields to the request too: $_POST['foo'] = 'bar'
$content .= "--".MULTIPART_BOUNDARY."rn".
            "Content-Disposition: form-data; name="foo"rnrn".
            "barrn";

// signal end of request (note the trailing "--")
$content .= "--".MULTIPART_BOUNDARY."--rn";

As you can see, we're sending the Content-Disposition header with the form-data disposition, along with the name parameter (the form field name) and the filename parameter (the original filename). It is also important to send the Content-Type header with the proper MIME type, if you want to correctly populate the $_FILES[]['type'] thingy.

If you had multiple files to upload, you just repeat the process with the $content bit, with of course, a different FORM_FIELD for each file.

Now, build the context:

$context = stream_context_create(array(
    'http' => array(
          'method' => 'POST',
          'header' => $header,
          'content' => $content,
    )
));

And execute:

file_get_contents('http://url/to/upload/handler', false, $context);

NOTE: There is no need to encode your binary file before sending it. HTTP can handle binary just fine.

Wednesday, June 2, 2021
 
Ticksy
 
4

Try to use following style

insert into File (name, type, data) values ("good.bin", "binary", 0x01020304)
Saturday, October 23, 2021
 
Sasha
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :