Recently, for a project, i have been trying to get a few files from a sftp webserver using ssh2 extension and php. So far i've managed to connect to the server and list all the files on a specific directory, but when i try to get them with ssh2_scp_recv i allways get
"Warning: ssh2_scp_recv(): Unable to receive remote file in ..." . I've used another implementation, that worked, with fread, but the transfer speed are too slow, and i need some more speed.
In the implementation i've been working on i tried using the refered function inside a loop passing the file names / paths as:
ssh2_scp_recv($connection, "/download/file.json", "C:\Users\User\file.json");
but no luck. Does anyone know of a better way to achieve the goal or to surpass this problem?
Edit:
<?php
$url = 'server.url.com';
$port = 22;
$username = "username";
$password = "password";
// Make our connection
$connection = ssh2_connect($url);
// Authenticate
if (!ssh2_auth_password($connection, $username, $password)) throw new Exception('Unable to connect.');
// Create our SFTP resource
if (!$sftp = ssh2_sftp($connection)) throw new Exception('Unable to create SFTP connection.');
$localDir = 'C:\Users\User';
$remoteDir = '/download';
// download all the files
$files = scandir('ssh2.sftp://' . $sftp . $remoteDir);
if (!empty($files)) {
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
ssh2_scp_recv($connection, "$remoteDir/$file", "$localDir\\$file");
}
}
}
?>
This is the code i'am using, and i get this output:
Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26
Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26
Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26
Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26
Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26
Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26
Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26
Warning: ssh2_scp_recv(): Unable to receive remote file in C:\xampp\htdocs\teste1.php on line 26
As i was saying i suspect the problem may be the localdir path as i am using windows, but not sure as i tryed many alternatives and none worked.
Related
I want to get informations about a file in a local filesystem.
This gets an error
$file = 'file:///.../test.txt';
$stat = stat($file);
Warning: stat(): stat failed for file:///.../test.txt
But this works
$file = __DIR__.'/test.txt';
$stat = stat($file);
Is there a chance to get the local file? Here is a hint that it should work https://www.php.net/manual/de/wrappers.file.php
PHP - move_uploaded_file() unable to move /temp/ to actual folder in windows server. Where as it is working fine in linux without issue. I have even given read write permission for the folders.
if(empty($_FILES['verifyfile']['name']))
{
$path = 'No';
}
else
{
$name = $_FILES['verifyfile']['name'];
$tmp = $_FILES['verifyfile']['tmp_name'];
$path = "files/".basename($currentdatetime.$name);
move_uploaded_file($tmp,$path) ; // Moving Uploaded file
Error Log Given below
[14-Nov-2017 07:55:04 Asia/Kolkata] PHP Warning: move_uploaded_file(files/2017-11-14 07:55:04Sakthi APIL (Paid).pdf): failed to open stream: Invalid argument in C:\inetpub\wwwroot\verifydata.php on line 44 [14-Nov-2017 07:55:04 Asia/Kolkata] PHP Warning: move_uploaded_file(): Unable to move 'C:\Windows\Temp\php9779.tmp' to 'files/2017-11-14 07:55:04Sakthi APIL (Paid).pdf' in C:\inetpub\wwwroot\verifydata.php on line 44
I am trying to upload a local file from my Mac to SFTP via PHP. My code:
$connection = ssh2_connect($server, $port);
if (ssh2_auth_password($connection, $username, $passwd)) {
$sftp = ssh2_sftp($connection);
echo "Connection successful, uploading file now..."."\n";
$file = '/Users/petenaylor/Desktop/diamondexclusive.mp4';
$contents = file_get_contents($file);
file_put_contents("ssh2.sftp://{$sftp}/{$file}", $contents);
}
else {
echo "Unable to authenticate with server"."n";
}
It connects as it should and I have checked that the local file location is correct, but the error messages I get are:
Warning: file_get_contents(/Users/petenaylor/Desktop/diamondexclusive.mp4): failed to open stream: No such file or directory in /home/test.php on line 39
Warning: file_put_contents(): Unable to open ssh2.sftp://Resource id #3//Users/petenaylor/Desktop/diamondexclusive.mp4 on remote host in /home/test.php on line 40
Warning: file_put_contents(ssh2.sftp://Resource id #3//Users/petenaylor/Desktop/diamondexclusive.mp4): failed to open stream: operation failed in /home/test.php on line 40
My log file from Filezilla:
Command: put "/Users/petenaylor/Desktop/diamondexclusive.mp4" "diamondexclusive.mp4"
Status: local:/Users/petenaylor/Desktop/diamondexclusive.mp4 => remote:/home/myfarewellnote/web/diamondexclusive.mp4
Trace: FileTransferParseResponse(0)
Trace: CSftpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Status: File transfer successful, transferred 12,661,295 bytes in 111 seconds
Status: Retrieving directory listing of "/home/myfarewellnote/web"...
Trace: CSftpControlSocket::ParseSubcommandResult(0)
Trace: CSftpControlSocket::ListSubcommandResult()
Trace: CSftpControlSocket::SendNextCommand()
Trace: CSftpControlSocket::ListSend()
Command: ls
Status: Listing directory /home/myfarewellnote/web
Trace: CSftpControlSocket::ListParseResponse()
Trace: CSftpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Status: Directory listing of "/home/myfarewellnote/web" successful
In FileZilla, you upload a file under a name diamondexclusive.mp4 to the current remote working directory, which is /home/myfarewellnote/web.
Hence the full target path is /home/myfarewellnote/web/diamondexclusive.mp4.
While in PHP you upload the file to /Users/petenaylor/Desktop/diamondexclusive.mp4 (what is actually a local source path, that has nothing to do with the server).
Use the same path that you upload the file to in FileZilla:
file_put_contents("ssh2.sftp://{$sftp}/home/myfarewellnote/web/diamondexclusive.mp4", $contents);
I am trying to write a file to a local share using PHP and a webserver. The origin of the file is the webserver ubuntu01 and a share on my local machine 7PD01-2012 is where the file is to be written. The path "ftp://ryan:pass#7PD01-2012//file.txt" resolves when I put it in a web browser and displays the file contents. There does not seem to be a permissions issue. I am able to ping the webserver from the client and the client from the webserver. FTP is installed on my ubuntu webserver and Filezille ftp Server is installed on the client. Using filezilla or the browser I am able to ftp to both locations. I have setup firewall rules on the webserver and updated the /etc/hosts file. Any ideas what could be causing these errors?
$fileName = "ftp://ryan:pass#7PD01-2012//file.txt";
is_dir($fileName);
if (($myfile = fopen($fileName, "w")) === false) { //open the file
//if unable to open throw exception
throw new RuntimeException("Could Not Open File Location on Server.");
}
( ! ) Warning: is_dir(): connect() failed: Connection timed out in /var/www/html/CDprinter/processor.php on line 143
( ! ) Warning: fopen(): connect() failed: Connection timed out in /var/www/html/CDprinter/processor.php on line 144
( ! ) Warning: fopen(ftp://...#10.162.12.130//file.txt): failed to open stream: operation failed in /var/www/html/CDprinter/processor.php on line 144
I have to retrieve a file by using FTPS :
$ftp = ftp_ssl_connect($ftp_server, 9921, 10);
$bool = ftp_login($ftp, $login, $pass);
ftp_pasv($ftp, true);
$files = ftp_nlist($ftp, "/");
var_dump($files);
die();
But I have this warnings, and $files is false
Warning: ftp_login() [function.ftp-login]: SSL/TLS handshake failed in xxx\view.importfromadmin.php on line 81
Warning: ftp_login() [function.ftp-login]: Using authentication type TLS in xxx\view.importfromadmin.php on line 81
How can I fixe it ?
I think your issue is going to lay with OpenSSL not being compiled with PHP. You can find more information about that here: http://www.deciacco.com/blog/php/php-openssl-and-ftp_ssl_connect-on-win32
With information on how to fix it as well. You can check if your PHP has OpenSSL compiled through the phpinfo. Given that you are running a Windows server check the link above. Since you did not provide that information this is just a random guess.
I can copy my file using this :
$ftp_path = "ftps://$ftp_login:$ftp_password#$ftp_server:9921/".$import_file;
copy($ftp_path, $uploadFileName);