PHP script using ssh2_scp_send Fails to create remote file - php

I'm trying to transfer files to a shared hosting account (godaddy) and it's failing:
Warning: ssh2_scp_send(): Failure creating remote file: failed to send file in 'local/file/path'
I have SSH2 installed and the connection is working. I even ran a ssh2_exec command to see if I can affect the remote server and I was able to create a directory. I also successfully used scp directly on the command line going both ways... As a further troubleshooting attempt, I reversed the file paths and created a remote server file for it to attempt downloading, I get this error:
failed to open stream: No such file or directory
I called the host to make sure I'm using the correct absolute file path, I've tried many variations, and I still get this error. I think my issue lies somewhere with the remote file path.
The problem code is below:
$localpath=$_SERVER['DOCUMENT_ROOT'].'/local_file_path/' ;
$remotepath = 'website.com/public_html/folder/' ;
$connection = ssh2_connect('website.com', 22);
ssh2_auth_password($connection,'username','password' );
ssh2_scp_send($connection,"$localpath"."SUCCESS.txt", "$remotepath"."SUCCESS.txt", 0644);
I am also having issues trying to use sftp via PHP. I read elsewhere on the web that you have to use double quotes on the file paths, whenever they fail, the printed path appears correct, but perhaps I'm missing something?
UPDATE
I logged into the shared hosting account via sftp with filezilla and discovered that, when copied, the full url path is different then what I was being given when logged in threw regular ftp. I can now upload a file, but, I can't seem to download just yet, ironically... I still get the error about not being able to locate the file path on the remote server.

ssh2_scp_send need absolute path, I solved my issue by using pwd where I need to send my files. ~/ dont works either, it's not an absolute path.
The bottom comment on this page http://www.php.net/manual/en/function.ssh2-scp-send.php refers to a problem you might be having.
I suggest that, use phpseclib or ssh2_sftp
Interesting topics:
Uploading files with SFTP & phpseclib interesting debate with good informations.
How to SFTP with PHP?

Related

Can't write to a remote server location with PHP but I can browse and write to it in File Explorer?

Sorry if this a repeat of a question that has been asked before but I have not been able to find my exact situation. We are trying to migrate our website server from a Windows 10 VM (yes I know) to a Windows Server 2019 VM. We have some PHP on our site that writes files to some of our other servers on the same domain and have been able to do so without issue using file_put_contents like so:
file_put_contents("\\\\server\\folder\\folder\\folder\\".$filename, $file);
Now all of a sudden, to run the same code on our new server I get a Warning on this line, "Failed to open stream: Permission denied". I have permission to access this folder, I can browse to \server\folder\folder\folder and create a file there. I even tried mapping this server to a letter drive on my new web server, and still same error. I can put the file on the local C drive just fine but that's it.
Running fileperms on the folder path gives Warning: fileperms(): stat failed. Running is_writable on the folder path returns false, I just can't see how. Running it on the old Windows 10 web "server" returns true. I've read some things about needing to enable certain settings on the server you're trying to access, but I just can't think of what would allow one VM to access it and not another. Both VM's are logged in with the same user with admin rights. I can bring up the same folder in file explorer and write to it, just not via PHP. What obvious thing am I missing?
Thanks!
After weeks of banging our heads against the wall we finally figured this out. On our old web server, when right-clicking the website from the Sites file tree in IIS, under Manage Website -> Advanced Settings, the Physical Path Credentials field was set to the credentials needed to access these folders. On the new server it was blank. I'm not sure how this got missed but in any case, after entering the correct credentials here everything immediately worked.

PHP can't download files from another server locally when accessed with browser

I'm trying to download a file locally from a remote server but every time i access the file that contains the PHP code using the browser file_get_contents() fails because it doesn't have the permission to write to /var/www/html (Apache2). I tried using cURL but that didn't work either, checked to see if allow_url_fopen is on (it is) and added the php file to sudoers. I can't seem to find any solution online.
i think this is permission issue, see this maybe will solve your problem, pick that suit to your enviroment.

Using laravel SSH to upload files to another server

Revised:
I have an uploaded file that I would like to move to my other server for processing with another application. I thought the best way would be to use SSH built into Laravel however, it's proven to be a headache.
In my configuration file (remote.php), I've successfully set up my connection to my other server using SSH keys. I tested this by running
SSH::run(['cd /var/www/html', 'ls'], function($line) {
echo $line . PHP_EOL;
});
and it gave me a list of files in /var/www/html/ on my remote server. All seems good, right?
Now I want to use SSH::put() to upload my uploaded file to the remote server. I used:
SSH::put($file->getRealPath(), '/var/www/html/uploads');
Guess what? It ran without an error so I assumed it was uploaded successfully. When I checked that directory on the remote server, it was blank. I've tried to set file permissions to 777 on /var/www/html/uploads to see if it was a permission issue, same thing--nothing.
I also tried using SSH::get() to download a file from my remote server and it ran successfully.
Here's a dump on $file->getRealPath():
string(14) "/tmp/phpikt2mg"
Both machines are running Centos and have their public keys registered on both ends.
I'm a little bit late, but just faced the same problem and finally found the reason of failure! So it could save some minutes to others.
You should use full path with filename as a second parameter instead of just a path to folder. For this question it should be
SSH::put($file->getRealPath(), '/var/www/html/uploads/' . $file->getFilename());

FTP opendir() — "FTP server reports 550 [can't find file]"

I am attempting to read a open a directory using opendir(), and then loop through the files in that directory. The ultimate goal for this script is to transfer certain files from the directory that is being read to a different FTP server.
The "FTP" part is what's screwing me up—and I have no idea why!
On some servers (this script is expected to be run across several) I am receiving the following error:
Warning: opendir(ftp://...#jslsolutions2.flashsvr.com/streaming)
[function.opendir]: failed to open dir: FTP server reports 550
Can't find file in [PATH] on line 88
[PATH] would be the actual path to the PHP script, of course.
Line 88 is as follows:
if ($handle = opendir($from))
The variable $from represents a complete FTP path.
The complete FTP path is as follows:
ftp://[USERNAME]:[PASSWORD]#[SERVER]/streaming
I have confirmed that directory is correct. When I copy and paste the directory into my web browser, for example, Firefox opens a directory tree containing all of the files, exactly as expected. In other words, though PHP is complaining that it "can't find file", the "file" (that is the directory, I believe) actually does exist.
What on earth is going on? As I mentioned before, this script does work on some servers. The thing holding it up appears to be whether or not the server is Windows-based, as it works fine on the Linux servers I have tested. Unfortunately, I do not have access to the servers, though our server administrator can make changes if we can isolate the problem.
Why am I receiving this error, and is there a workaround?
Add a trailing slash / see if that resolves the issue there was a bug in PHP which has been fixed.

Permission Denied on move_uploaded_file to another server in IIS

I have a PHP web application running on IIS, which allows a user to upload a file from a simple html form. The uploaded file is then moved to a folder on another server. This is working correctly on Apache, but fails on IIS with the error:
function['move_uploaded_file']failed to open stream: Permission denied
in...
I've checked all the permissions on the directories. If I run a simple PHP script through the command line to copy a file from the server into the folder on the other server it works correctly, so I suspect the problem is with IIS.
if (move_uploaded_file($_FILES ["file"] ["tmp_name"], "\\\\000.00.0.00\\tia\\web\\upload\\" .$_FILES["file"]["name"])) {
This has been covered already here. Quoting the reason here:
You have mapped target directory to a share such as \server2\files. These mappings are only available to the current users (eg, the admin account), not to the IUSR account that your php is probably running as (assuming IIS). Solution, don't use mappings, instead use the full 'unc' path name, ie '\server\share\folder\file.ext', also remember that the IUSR account will need access to these shares/folders/files
From what I can see in your comment, you are using a \\ prefixed network share as the target for your move_uploaded_file() operation.
I'll bet a beer that PHP isn't allowed to access that share. Remember, PHP runs with IIS's permissions!
Try saving to a local, globally writable path first. If that works, you know you have to fix the share's permissions.

Categories