How to set up phpseclib with composer - php

I have downloaded phpseclib using composer and it created the path /var/www/html/dom/vendor/phpseclib/phpseclib/phpseclib
Now I am not sure where I include my test.php file to run it. Continuously getting the error " Fatal error: Class 'phpseclib\Net\SSH2' not found in /var/www/html/dom/vendor/phpseclib/phpseclib/phpseclib/Net/SFTP.php on line 50"
My code of test.php is
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '/phpseclib');
include('Net/SFTP.php');
$sftp = new Net_SFTP('domain');
if (!$sftp->login('user', 'pass')) {
exit('Login Failed');
}
// outputs the contents of filename.remote to the screen
echo $sftp->get('filename.remote');
// copies filename.remote to filename.local from the SFTP server
//$sftp->get('filename.remote', 'filename.local');
?>

Composer generates an autoloader. You don't have to care about including things manually. Just do:
include '/var/www/html/dom/vendor/autoload.php';

Besides the include/require the namespace is also different. Thats the solution for me:
require_once __DIR__ . '/vendor/autoload.php';
$handler = new \phpseclib\Net\SFTP('ftp_host');
if (!$handler->login('ftp_user', 'ftp_password')) {
exit('FTP login failed');
}
print_r($handler->nlist());

Related

How to Tesseract OCR for php without Composer

Hi can anyone help me to install Tesseract OCR for php without Composer. How can I use it on xampp server so that the code can be used to develop a web application. Thanks in advance
I know this is old, but still would have helped me out... This is the code that I created to use the latest Tesseract without composer. To those that are asking why, instead of creating a solution, why waste people's reading time when you don't understand?
<?php
require_once "./tesseract-ocr/src/TesseractOCR.php";
require_once "./tesseract-ocr/src/Command.php";
require_once "./tesseract-ocr/src/FriendlyErrors.php";
require_once "./tesseract-ocr/src/Process.php";
require_once "./tesseract-ocr/src/TesseractOcrException.php";
require_once "./tesseract-ocr/src/TesseractNotFoundException.php";
require_once "./tesseract-ocr/src/ImageNotFoundException.php";
require_once "./tesseract-ocr/src/UnsuccessfulCommandException.php";
$filepath = "./images/processing";
$dir = new DirectoryIterator("$filepath");
use thiagoalessio\TesseractOCR\TesseractOCR;
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
$filename = $fileinfo->getFilename();
$fullfilepath = $filepath . "/" . $filename;
$ocr = new TesseractOCR("$filename");
try {
$content = $ocr->run();
}
catch (exception $e) {
echo "$filename - No text, good image\n";
continue;
}
echo "$filename - There's Text!\n";
die();
}
}
?>
In this case, I'm using Tesseract just to detect if there's any text in an image at all (which for me is bad).

phpseclib shows no error and login fails in HEROKU

I am using phpseclib to transfer file via SFTP to a server.
In my local computer the file gets transfered perfectly fine to the server, but on Heroku it does not work at all and login fails each time. I am not sure why this is happening.
Here is the piece of code.
$sftp = new \phpseclib\Net\SFTP(getenv('INSTRUM_SERVER'),22222);
if (!$sftp->login(getenv('INSTRUM_USERNAME'), getenv('INSTRUM_PASSWORD'))) {
var_dump($sftp->getSFTPErrors());
exit('Login Failed');
}
"var_dump" returns an empty array so I am not sure why login fails.
I am using composer for the phpseclib to be loaded from vendor.
I'm using phpseclib like this, and it's working fine:
private $sftp = NULL;
private function create_sftp_object( $host, $port = 22, $timeout = 10 )
{
$path = APPPATH . 'libraries/phpseclib';
set_include_path( get_include_path() . PATH_SEPARATOR . $path );
include_once( APPPATH . 'Net/SFTP.php');
return new Net_SFTP( $host, $port, $timeout );
}
public function index()
{
$this->sftp = create_sftp_object(
getenv('INSTRUM_SERVER'),
22222,
6
);
if( $this->sftp->login( getenv('INSTRUM_USERNAME'), getenv('INSTRUM_PASSWORD') ){
echo 'Login successful';
}else{
echo 'Login not successful';
}
}
It's a little different than your code. As you can see, I have phpseclib in my libraries directory. Try it and see if it works for you.

Downloading file from remote to local using SCP in phpseclib - PHP

I am trying to download a zip file from remote server to local in PHP using phpseclib, I have found a code that lets me upload but I couldn't find anything to download. Here is the code that I found on stackoverflow to upload :
<?php
$cwd = getcwd();// . "/updater/";
set_include_path(get_include_path() . PATH_SEPARATOR . $cwd . '/phpseclib/');
require_once('Net/SSH2.php');
require_once('Net/SCP.php');
$ssh = new Net_SSH2('remote_ip');
if (!$ssh->login('remote user', 'remote_pwd'))
{
throw new Exception("Failed to login");
}
$scp = new Net_SCP($ssh);
if (!$scp->put('remote_path',
'local_path',
NET_SCP_LOCAL_FILE))
{
throw new Exception("Failed to send file");
}
?>
I also tried to switch between the remote_file and the local_file but that will not work.
Thanks in advance.

How do I include a path of google client library in php

$r = set_include_path(get_include_path() . '\google-api-php-client-master\src');
echo $r;
require_once 'Google/Client.php';
require_once 'Google/Service/AdExchangeSeller.php';
function __autoload($class_name) {
include 'examples/' . $class_name . '.php';
}
when I am running it on browser its showing following error..
You are not using the path seperator when calling set_include_path, try this:
$r = set_include_path(get_include_path() . PATH_SEPARATOR . '\google-api-php-client-master\src');
Here is the documentation on set_include_path
Edit
Did you restart Apache after making the change to your php.ini?

connecting to SFTP is not working form the command line

I am trying to read a file from SFTP. I am able to connect to the server read information when I run the URL via browser.
But when I run this command from the command line I get many errors
This is the error that I get
C:\Users\test>php -f C:\phpsites\dp_in\IN_sales.php
PHP Warning: require(../classes/connection.php): failed to open stream: No such
file or directory in C:\phpsites\dp_in\IN_sales.php on line 17
Warning: require(../classes/connection.php): failed to open stream: No such file
or directory in C:\phpsites\dp_in\IN_sales.php on line 17
PHP Fatal error: require(): Failed opening required '../classes/connection.php'
(include_path='.;C:\php\pear') in C:\phpsites\dp_in\IN_sales.php on li
ne 17
Fatal error: require(): Failed opening required '../classes/connection.php' (inc
lude_path='.;C:\php\pear') in C:\phpsites\dp_in\IN_sales.php on line 17
This is how I connect to the server using SFTP.
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
Another weird thing is that another file using similar code works with no issues!
I am not sure what could be causing this issue
EDITED
Here is my entire code after the edit
<?php
ini_set('max_execution_time', 900);
ini_set('user_ini.cache_ttl', 900);
date_default_timezone_set('America/Los_Angeles');
error_reporting(E_ALL);
ini_set('display_errors', 1);
$today = strtotime( "-25 day" );
$remote_file = '/per/Data_'. date('Ymd', $today ) . '.csv';
//set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include(__DIR__ . '/Net/SFTP.php');
//include "phpseclib.php";
$sftp = new Net_SFTP('IPADDRESS');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
if(empty($sftp->size($remote_file))){
exit('File Not Found - ' . $remote_file);
}
$csv = $sftp->get($remote_file);
$handle = fopen('php://temp', 'r+');
fputs($handle, $csv);
rewind($handle);
$data = array();
while ($csv_row = fgetcsv($handle) ) {
$data[] = $csv_row;
}
fclose($handle);
require realpath(__DIR__ . '/..') . '/classes/connection.php';
use __DIR__ in your require in order to require your files properly:
require(__DIR__ . "../classes/connection.php")
Multiple issues...
1) You need to define your includes path so that the require works
2) Your connection is probably failing because of #1 (missing credentials from included file)
I figured it out!!
I added this code to the top one my file
chdir(dirname(__FILE__));
This changes the working directory to the running file path.

Categories