Using laravel SSH to upload files to another server - php

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

Related

PHP - fopen("filename", "w") creates file in local environment but doesn't create document in the hosting server

I have a website with a hosting server running PHP.
I have written an experiment that retrieves data through a survey model, and I use the fopen("filename", "w") function to store retrieved data.
I tried this in my local environment while building up the webpage. Everything worked fine, and a document was created in the subfolder TestResponses/ with the data requested.
I just uploaded the file in my server, and tried it out. Everything works fine except that my file is not created or stored.
I use the following code to submit my data:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "storedata.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("test_variables=some_variables");
and I use the following code to write it down in a file to be stored in my subfolder:
$test_variables = $_POST["test_variables"];
$myfile = fopen( "TestResponses/" . substr($test_variables, 0, 18) . ".txt", "w") or die("Unable to open file!");
fwrite($myfile, $test_variables);
fclose($myfile);
Does anybody know if I missed to do something required to create and store the file in the server?
P.S. the subfolder TestResponses/ already exists in the server, so it does not to be created.
P.P.S. I don't get any error message by pressing F12.
update: I discovered that the fopen() function works correctly and files are updated to the folder in the server side. It was a delay problem, and files were uploaded (visible) only after about 5 minutes. So, I will mark this question solved.
The local environment and the remote server are different (duuh :) ).
Your first step is to figure out what is the user (on remote server) that runs your php file. What OS is the remote server? If it is linux, probably your user that runs php is www-data (apache) if you are running a version of ubuntu/debian.
If you have console access to the remote server you can check the permission of the folder TestResponses using ls -alh command in the parent folder of TestResponses.
HOW TO TEST
The first and the simplest thing is to chmod 777 TestResponses folder - but this is a security risk, only use the 777 permission to test that your data is created inside the folder.
If you use a software like filezilla or winscp, you can also check what permissions the TestResponses folder has.
The correct permissions for a folder should be 775 or 755 (depending on the group).
You might need to check how permissions RWX are on linux (if linux is your remote server OS).
P.S. make sure you also add encoding UTF-8 to your website.
On outside servers, always check write access using is_writable() on the folder. If there's no write access, check the rights of the folder with some FTP client or console (see #Mike 's answer), and if it's 775, make sure your FTP user is in the same group as www-data (or whatever the web server is running with). If you're not sure how, talk to the Linux guy operating the server.
If it's 755, you can only write with the same user (not group) that created the folder - so in this case, if it's an option to delete the folder (from FTP or console) and re-create it with your php script, it can be a good solution. (Obviously, if there's something in the folder or if it's something used by others, not just you, well, then please don't delete it)

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.

PHP script using ssh2_scp_send Fails to create remote file

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?

PHP can't open uploaded files

I have Ubuntu Server with lamp.
When i uploading files like xls or images it works fine, but when I trying to open it says me that the Excel file was broken. Same thing happens with images
Is it possible that php changes the file?
Permissions for /var/www/publick_html/uploads is 777
well, I know,I use windows and maybe this is extremely OFF TOPIC, but I have a simililar problem with uploaded files and my experience can be usefull for other people.
In my case, the uploaded file can not be open because it was written first in
C:\Windows\Temp
and then moved to the upload directory, but my temp folder doesn t have permission to access to it. Infact, if I click on temp folder I see:
After I clicked "Continue", the problem has been solved!
The problem was in versions of apache and php.
Version of apache was 2.2 and php was 5.4
I upgraded my apache to 2.4 version and now it works fine.
This should solve your problem with being unable to open uploaded files with PHP on SQL SERVER. It solved mine.
You may come across the following problem using PHP on Microsoft IIS: getting permission denied errors from the move_uploaded_file function even when all the folder permissions seem correct. I had to set the following to get it to work:
Write permissions on the the folder through the IIS management console.
Write permissions to IUSR_'server' in the folder's security settings.
Write permissions to "Domain Users" in the folder's security settings.
The third setting was required because my application itself lives in a secure folder - using authentication (either Basic or Windows Integrated) to identify the users. When the uploads happen IIS seems to be checking that these users have write access to the folder, not just whether the web server (IUSR_'server') has access.
Also, remember to set "Execute Permissions" to "None" in the IIS management console, so that people can't upload a script file and then run it. (Other checks of the uploaded file are recommended as well but 'Execute None' is a good start.)
reference https://www.php.net/manual/en/features.file-upload.php

file_exists() not working on AWS ec2 server, even though works on dev server and file is where it is

I have a buddypress installation on an Amazon ec2 server running default Debian linux, wordpress and buddypress installed. Users are trying to upload avatars, but they cannot be resized. This is because in the buddypress code for resizing avatars, a call to file_exists($original_file) returns false, even though I can see that the file is in exactly the directory where it's supposed to be.
This is not a problem on our development server, where uploading avatars works flawlessly. The next line of code is what causes the resizing to fail.
$original_file = '/var/www/html/wp-content/uploads/avatars/1/test-picture.png';
if(!file_exists($original_file)
return false;
Now I am ssh'd in that directory, and can see that that path is absolutely correct. I am guessing this is an issue with php permissions? To be able to access that directory and see that the file doe sin fact exist.
All files are now owned by apache:apache. I've experimented with chmod 775 and 777, but still php cannot recognize the file. Does anybody know how php can be configured on this Amazon ec2 server so that it can recognize that the file does in fact exist?
If you pasted your code above correctly, you will get a syntax error that will show up in your syslog that you are missing a closing ) in your if statement.
Your code should look like this:
$original_file = '/var/www/html/wp-content/uploads/avatars/1/test-picture.png';
if(!file_exists($original_file))
return false;
One possible cause could be case sensitivity. You state that your production server runs linux, which IS case sensitive. If your development server is a mac, you are most likely working with a non-case sensitive filesystem.
Possible solution: safe all images in all-lower-case and user lowercase directories. When calling file_exists, call strtolower on the file name.
If you are developing on a windows machine, you might be using \ as directory separators.
Possible solution:
Always use / as directory separators, since that will work on all systems.
Do you have these issues when saving to a folder that is above your document root?

Categories