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);
Related
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.
Warning: include(timesb.php): failed to open stream: No such file or directory in /var/www/html/tn_poly_lateral/candidate-portal/application/fpdf.php on line 1140
Warning: include(): Failed opening 'timesb.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/tn_poly_lateral/candidate-portal/application/fpdf.php on line 1140
Fatal error: Uncaught Exception: FPDF error: Could not include font definition file in /var/www/html/tn_poly_lateral/candidate-portal/application/fpdf.php:271 Stack trace: #0 /var/www/html/tn_poly_lateral/candidate-portal/application/fpdf.php(1142): FPDF->Error('Could not inclu...') #1 /var/www/html/tn_poly_lateral/candidate-portal/application/fpdf.php(459): FPDF->_loadfont('timesb.php') #2 /var/www/html/tn_poly_lateral/candidate-portal/application/fpdf.php(507): FPDF->AddFont('times', 'B') #3 /var/www/html/tn_poly_lateral/candidate-portal/application/application.php(259): FPDF->SetFont('times', 'B', 12) #4 {main} thrown in /var/www/html/tn_poly_lateral/candidate-portal/application/fpdf.php on line 271
This fpdf error shows on production server But when I execute file on locally using xampp everything works fine and pdf is downloading. Anyone help to resolve the problem
depending on your error it appears that there is missing files
timesb.php doesn't exist on your hosting you can check on your hosting search that file if it doesn't exist add it
I am on Windows 10. I'm using the laragon dev environment, which wraps apache.
I am trying to run the following guzzle call to download a file:
$downloadClient = new Client(['base_uri' => 'https://vendor/region']);
$downloadResponse = $downloadClient->request('GET','endpoint/file_url.txt',['sink' => __DIR__]);
I keep getting the following in return:
Fatal error: Uncaught RuntimeException: Unable to open C:\laragon\www\act using mode w+: fopen(C:\laragon\www\act): failed to open stream: Permission denied in C:\laragon\www\act\vendor\guzzlehttp\psr7\src\functions.php:303 Stack trace: #0 [internal function]: GuzzleHttp\Psr7{closure}(2, 'fopen(C:\larago...', 'C:\laragon\www\...', 311, Array) #1 C:\laragon\www\act\vendor\guzzlehttp\psr7\src\functions.php(311): fopen('C:\laragon\www\...', 'w+') #2 C:\laragon\www\act\vendor\guzzlehttp\psr7\src\LazyOpenStream.php(37): GuzzleHttp\Psr7\try_fopen('C:\laragon\www\...', 'w+') #3 C:\laragon\www\act\vendor\guzzlehttp\psr7\src\StreamDecoratorTrait.php(31): GuzzleHttp\Psr7\LazyOpenStream->createStream() #4 C:\laragon\www\act\vendor\guzzlehttp\psr7\src\StreamDecoratorTrait.php(136): GuzzleHttp\Psr7\LazyOpenStream->__get('stream') #5 C:\laragon\www\act\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(395): GuzzleHttp\Psr7\LazyOpenStream->write(' in C:\laragon\www\act\vendor\guzzlehttp\psr7\src\functions.php on line 303
I have tried granting "Full Control" to my user account, which is what Laragon is running as. I've also tried granting all to IUSR and "all users" on the directory with now results.
Any help is appreciated.
You're passing __DIR__ as the "sink" where to write the file. That's a directory. You can't write data to a directory like it was a file. Try using the name of a file.
$downloadClient = new Client(['base_uri' => 'https://vendor/region']);
$downloadResponse = $downloadClient->request('GET','endpoint/file_url.txt',
['sink' => "file_url.txt"]);
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
so my script is running in my localhost(pc1) and i am trying to save an excel file using phpexcel in a network folder (\\pc2\c$\myfolder) but it gives me an error saying that i dont have permission in that particular folder eventhough i have a full access. I can manually create, edit or deleta a file in that folder.
error msg:
Warning: fopen(\\pc2\c$\myfolder\12122014.xls) [function.fopen]: failed to open stream: Permission denied in C:\wamp\www\receivables\PHPExcel\Shared\OLE\PPS\Root.php on line 90
Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Can't open \\pc2\c$\myfolder\12122014.xls. It may be in use or protected.' in C:\wamp\www\receivables\PHPExcel\Shared\OLE\PPS\Root.php:93 Stack trace: #0 C:\wamp\www\receivables\PHPExcel\Writer\Excel5.php(226): PHPExcel_Shared_OLE_PPS_Root->save('\\ipcsvs001\c...') #1 C:\wamp\www\receivables\index.php(241): PHPExcel_Writer_Excel5->save('\\ipcsvs001\c...') #2 {main} thrown in C:\wamp\www\receivables\PHPExcel\Shared\OLE\PPS\Root.php on line 93
note: i am using wamp server(not sure if this matters)