I'm trying to upload a file to sftp server using phpseclib library. Here's the code I've written
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib0.3.5');
$fileName = "sImg3.jpeg";
$fileData = file_get_contents($fileName);
$b64Data = base64_encode($fileData);
// upload the file to ftp server
include('Net/SFTP.php');
include_once('include/config.php'); // contains sftp connection details
$sftp = new Net_SFTP($ftpHost);
if (!$sftp->login($ftpUser, $ftpPasswd))
{
die("Failed to connect to SFTP server");
}
else
{
$sftp->put($filename,$b64Data);
echo "Uploaded the file (".$filename.")\n";
}
When I try to run this, I'm getting:
PHP Fatal error: Call to undefined function crypt_random_string() in
/var/www/phpseclib0.3.5/Net/SSH2.php on line 1014
I've installed php5-mcrypt and phpseclib. Do I need to install any other dependencies for phpseclib? Please let me know.
Thanks much in advance,
Regards,
Sudheer
crypt_string_random is defined in Crypt/Random.php:
https://github.com/phpseclib/phpseclib/blob/0.3.5/phpseclib/Crypt/Random.php
But then again Net_SSH2 auto-includes Crypt/Random.php:
https://github.com/phpseclib/phpseclib/blob/0.3.5/phpseclib/Net/SSH2.php#L749
And Net_SFTP extends Net_SSH2:
https://github.com/phpseclib/phpseclib/blob/0.3.5/phpseclib/Net/SFTP.php#L253
And Net_SSH2 does a require_once so if anything you should be getting some sort of "file not found" error, if you're getting an error at all, not a "function not defined" error..
The problem is caused by how PHP handles nested include statements and relative paths. If you look at the current phpseclib programming, at least 'some' of the includes still use include() instead of require_once() which silently fail - which is why you don't see the errors about failing includes.
It all comes down to how PHP resolves relative pathing of nested includes - using the starting location of the parent script or the starting location of the current/child script. More info on the conundrum of PHP nested includes with relative pathing here:
php nested include behavior
When I ran into this problem myself, I worked around it by promoting /phpseclib out of it's own subfolder and into my existing /includes folder. Then I only ever include phpseclib files from within php scripts in my /includes folder so that the relative paths in phpseclib's nested include statements will work.
Related
I've created a phar of a Symfony2 web application, but I'm having some troubles with the cache-folders.
I found out that I could mount an external file/folder into a phar. That would fix my issue, but I can't get the example on the PHP site working.
I have a phar which contains an index.php:
<?php
$configuration = simplexml_load_string(file_get_contents(
Phar::running(false) . '/config.xml'));
?>
Then I include the .phar with the following code:
<?php
// first set up the association between the abstract config.xml
// and the actual one on disk
Phar::mount('phar://config.xml', '/var/www/$projectname/config.xml');
// now run the application
include 'phar-archive.phar';
?>
All files exists, but I get the following error:
PHP Fatal error: Uncaught exception 'PharException' with message 'config.xml is not a phar archive, cannot mount' in /var/www/$projectname/index.php:3
I already tried relative/absolute paths, changing permissions but can't get this to work. Additionally a working example of how I could mount a folder into a phar would be great !
First check that variable $projectname exist in this place (just run echo $projectname; before Phar::mount). If all ok, then change
Phar::mount('phar://config.xml', '/var/www/$projectname/config.xml');
to
Phar::mount('phar://config.xml', "/var/www/{$projectname}/config.xml");
It seems that variable $projectname not converted to its value because you used single quotes.
I've spent a lot of time troubleshooting this myself but none of what I've read solves my issue so I'm hoping I get some help here.
So anyway, I have written a PHP script that provides various functions to connect with Google Calendar. When I run this script directly using some inline test code to call my functions, everything runs fine. However, when I call the function from other scripts using 'require_once' to include it I get the following errors:
Warning: include_once(Zend\Gdata\Calendar\Extension\EventQuery.php) [function.include-once]: failed to open stream: No such file or directory in C:\Users\Luke Franklin\Clients\Tiers For Tea\Development\TiersForTea.com v1.1\lib\Zend\Loader.php on line 134
Warning: include_once() [function.include]: Failed opening 'Zend\Gdata\Calendar\Extension\EventQuery.php' for inclusion (include_path='.;C:\xampp\php\PEAR;C:\Users\Luke Franklin\Clients\Tiers For Tea\Development\TiersForTea.com v1.1\lib;C:\xampp\htdocs\TiersForTea.com\lib') in C:\Users\Luke Franklin\Clients\Tiers For Tea\Development\TiersForTea.com v1.1\lib\Zend\Loader.php on line 134
Your first thoughts might be that I'm not using the correct include path, but I have checked and rechecked this many times. I even tried hard coding the path. I'm quite sure that I'm using the correct path.
Now for the weird bit. If you look at the error you will notice the file Zend is trying to include: Zend\Gdata\Calendar\Extension\EventQuery.php. This file does not actually exist in the 'Extension' folder. It does exist in the parent folder though. If I just copy 'EventQuery.php' into the 'Extension' folder my script runs as expected. Weird, right?
So that does sorta solve my problem, but I would like to know what's going on here in-case it creates further issues. I should also note that I'm calling this script into an OpenCart module.
You might want to see some of my code so here's a snippet of the important bits, if you want more details just let me know:
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . realpath('\\lib') . PATH_SEPARATOR . "C:\\xampp\\htdocs\\TiersForTea.com\\lib");
require_once('Zend/Loader.php');
function connect() {
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
I doubt the error exists in any of the Zend files as you have indicated it works correctly in your isolation tests.
The error is thrown from Zend_Loader which means something in your code is making reference to Zend_Gdata_Calendar_Extension_EventQuery.
Check the stack trace for the error if available to pinpoint the location. If that's not available, do a global find for that string. If you find any matches, you will need to change them to Zend_Gdata_Calendar_EventQuery.
Update
Seems this is a known bug(s)
http://framework.zend.com/issues/browse/ZF-7013
http://framework.zend.com/issues/browse/ZF-11959
It's an issue when your code registers an error handler using ErrorException. Apparently it's fixed in the 1.12 branch but hasn't made it to a release yet.
There's a patch in the 11959 bug report that fixes the issue
I am trying to install xml diff ; https://github.com/mmacia/XMLdiff and i have not managed yet to make it work.Whenever i run any test example,i get
Fatal error: Interface 'PHPUnit_Framework_Test' not found in
C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php on line 85
Has anyone managed to install and use the library.I am using xampp on windows.
I believe your problem has to do with PHPUnit's Autoloader.php not being included. This file sets the php spl_autoloadspl_register function which is responsible for loading in interfaces and classes like PHPUnit_Framework_Test.
According to this SO question, you have to include the autoloader file manually. Without knowing more about your set-up and how that particular library works, I would say do something like this in the appropriate file(s):
// define phpunit path
if ( ! defined('PHPUNIT_PATH')) {
// define an absolute path to your PHPUnit dir
// CHECK THIS, i'm not good with php on windows:
define('PHPUNIT_PATH','C:\xampp\php\PEAR\PHPUnit');
}
// Then include the autoloader like this:
include PHPUNIT_PATH.'Autoloader.php';
I hope this helps you or anyone else out.
Check execution flags for C:\xampp\php\PEAR\PHPUnit\Framework\Framework\Test.php
The file needs to be executable by the user who is launching tests (probably you).
I was taught that it's good practice to specify the directory of a file you're linking to, even when the file is in the current directory. So, for example, use "./constants.inc.php" as opposed to "constants.inc.php". This has generally worked fine for me, except in one particular spot in my code I get the following PHP error when I use the ./ notation:
Warning: require(./constants.inc.php) [function.require]: failed to open stream: No such file or directory in C:\xampp\htdocs\widget_corp\includes\functions.inc.php on line 4
Fatal error: require() [function.require]: Failed opening required './constants.inc.php' (include_path='.;C:\xampp\php\PEAR;C:\xampp\htdocs\websites\mathverse\includes') in C:\xampp\htdocs\widget_corp\includes\functions.inc.php on line 4
Writing "./constants.inc.php" gives this error, but "constants.inc.php" doesn't (PHP seems to find the file just fine). Can someone tell me why the ./ notation doesn't work in this particular case? Perhaps it's something very simple I'm missing?
To give a bit more context (if necessary), the error is in the function below (line 2), which is in my PHP functions file (called functions.inc.php) which is inside an includes directory. The constant.inc.php file is also inside the includes directory.
Thank you.
function connect_to_db() {
require("./constants.inc.php");
$connection_id = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if (!$connection_id) {
die("Database connection failed: " . mysql_error());
}
$db_selection_was_successful = mysql_select_db(DB_NAME, $connection_id);
if (!$db_selection_was_successful) {
die("Database selection failed: " . mysql_error());
}
}
Writing "./constants.inc.php" gives this error, but "constants.inc.php" doesn't (PHP seems to find the file just fine). Can someone tell me why the ./ notation doesn't work in this particular case? Perhaps it's something very simple I'm missing?
The only explanation I have is that PHP seems to be finding the constants.inc.php file in one of the directories in the include path - most likely C:\xampp\htdocs\websites\mathverse\includes.
even with or without ./ PHP will be searching for same file (in case of ./, it won't search in include_path) , and this means that constats.inc.php DOES NOT exist.
Try this one
require(dirname(__FILE__)."/../constants.inc.php");
Create the following heirachy:
index.php // echo 'I am in the top directory';
folder1/
index.php // echo 'I am in folder 1';
folder2/
index.php // 'echo I am in foler' 2
on your top level index.php require folder1/index.php. From that require 'folder2/index.php';
This works no problem. However add a ./ before the folder2/index.php and you will get an error. It seems as once you specify which folder you want by using the ., it has to be relative to the current working directory. But if left open, the path will be checked in the calling scripts own directory too.
From the manual:
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal error.
This is the error I am receiving:
Fatal error: Class 'Validate' not found in C:\xampp\htdocs\final_project\validate.php on line 5
And here is my PHP code:
<?php
require_once 'Validate.php';
foreach($_POST as $name => $value)
{
$valid = Validate::string($value);
}
?>
I do not understand what I am missing. I installed --alldeps for the validate package, and the PEAR include path is also correct. Validate_CA is not giving me any errors, but it is not properly validating either.
PHP parses the include_path in order of precedence. This means that when a relative path is passed to require(), include(), fopen(), file(), readfile() or file_get_contents(), PHP will start looking in the first directory. If the file is found, it includes it. If not, it will continue to the next and repeats the process.
Consider the following include path:
include_path = ".:/php/includes:/php/pear"
and the following PHP script:
<?php
require('MyFile.php');
PHP will look for MyFile.php in the following order:
./MyFile.php (Current Directory)
/php/includes/MyFile.php
/php/pear/MyFile.php
The reason why you cannot load Validate.php is you already have a file called validate.php (remember, paths are not case-sensitive on Windows, but are on UNIX) in your current directory. Therefore, PHP includes your file instead of the file corresponding to PEAR::Validate since yours is found before PEAR's in the include_path order of precedence.
Simply renaming your file to something else than validate.php should fix your problem. If it still doesn't work, try echoing the return value of get_include_path() to make sure it really is set right.