connecting to SFTP is not working form the command line - php

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.

Related

ckeditor:Fatal error: require() [function.require]: Failed opening required '__DIR__/lang/en.php')

I am getting the following error from server:
Fatal error: require() [function.require]: Failed opening required '__DIR__/lang/en.php' (include_path='.:/usr/local/php5/lib/php') in /home/content/38/8264638/html/console/assets/ckeditor/plugins/imageuploader/imgbrowser.php on line 16
In my ckeditor i integrate the imageuploader and its working in my local server but its not working in hosted server.its saying error in imagebrowser.php file following are the code in imagebrowser.php..please help me..
<?php
session_start();
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$root = "http://$_SERVER[HTTP_HOST]";
// checking lang value
if(isset($_COOKIE['sy_lang'])) {
$load_lang_code = $_COOKIE['sy_lang'];
} else {
$load_lang_code = "en";
}
// including lang files
switch ($load_lang_code) {
case "en":
require(__DIR__ . '/lang/en.php');
break;
case "pl":
require(__DIR__ . '/lang/pl.php');
break;
}
// Including the plugin config file, don't delete the following row!
require(__DIR__ . '/pluginconfig.php');
// Including the functions file, don't delete the following row!
require(__DIR__ . '/function.php');
// Including the check_permission file, don't delete the following row!
require(__DIR__ . '/check_permission.php');
$_SESSION["username"] = "disabled_pw";
?>
Your PHP version doesn't support __DIR__ so it's being treated as a literal string.
You can fix it, but you should update your PHP version.
dirname(__FILE__)

How to set up phpseclib with composer

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());

realpath($_SERVER["DOCUMENT_ROOT"]) not working on Windows

I get this error:
Warning: require_once(C:\xampp\htdocs/models/config.php): failed to open stream: No such file or directory in C:\xampp\htdocs\venture\html\admin\index.php on line 3
Here is the code where the error appears:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require_once($root . "/models/config.php");
you need change slash
$root = realpath(str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']) );
require_once($root . "/models/config.php");

Error On Using spl autoload register

This is working fine on init.php
include 'models/m_app.php';
include 'views/v_app.php';
include 'controllers/c_app.php';
but the spl_autoload_register() is not working
spl_autoload_register(function ($class) {
include 'models/' . $class . '.class.php';
include 'views/' . $class . '.class.php';
include 'controllers/' . $class . '.class.php';
});
and I am getting error like
Warning: include(models/Model.class.php): failed to open stream: No such file or directory in C:\wamp\www\App\core\init.php on line 3
Warning: include(): Failed opening 'models/Model.class.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\App\core\init.php on line 3
Warning: include(views/Model.class.php): failed to open stream: No such file or directory in C:\wamp\www\App\core\init.php on line 4
Warning: include(): Failed opening 'views/Model.class.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\App\core\init.php on line 4
Warning: include(controllers/Model.class.php): failed to open stream: No such file or directory in C:\wamp\www\App\core\init.php on line 5
Warning: include(): Failed opening 'controllers/Model.class.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\App\core\init.php on line 5
Can you please let me know why this is happening?
Well, you are using the spl_autoload_register function incorrectly.
An autoload function is getting called, when you are trying to create a class that does not exist.
The correct way would be to name your files just as the class inside them and add an ending like .class.php:
View.class.php
Model.class.php
Controller.class.php
The autoloader:
spl_autoload_register(function ($class) {
set_include_path('views/' . PATH_SEPARATOR . 'controllers/' . PATH_SEPARATOR . 'models/');
include $class . '.class.php';
});
And to test it:
$test1 = new Model();
$test2 = new Controller();
$test3 = new View();

PHP Script fopen failed to open stream: > Invalid argument Error

I am getting a warning.
Warning: fopen(76561197992146126 .txt): failed to open stream:
Invalid argument in C:\wamp\www\Download\t3.php on line 6
For anyone wondering, this is the contents of main.txt below (http://pastebin.com/53chSRRz)
<?php
$APIkey = 'APIKeyHere';
$file = file('C:\wamp\www\Download\main.txt');
foreach ($file as $link) {
$link2 = "http://api.steampowered.com/ITFItems_440/GetPlayerItems/v0001/?key=" . $APIkey . "&SteamID=" . $link . "&format=json";
$downloaded = file_get_contents($link2);
$fh = fopen($link . ".txt", "a"); //or die("can't open file");
fwrite($fh, $downloaded);
}
echo "Finished";
?>
If I replace "fopen($link . ".txt", "a")" with a static file-name it works. But I need $link to be the filename. It is imperative to my setup.
I am running Windows 7 x64 using WAMP 2.2 with PHP 5.4.3
Your link IDs have whitespace in them. Try adding $link = trim($link); before creating $link2.

Categories