I am trying to connect to a server via php and write a PDF file to a folder that I dynamically create. This is the code I am using:
<?php
$ftp_server = "[server ip]";
$ftp_user_name = "[username]";
$ftp_user_pass = "[password]";
// set up basic ssl connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
if ($login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass)){
mkdir('/home/luke/public_html/pdfs/kmhllc/', 0777, true);
}else{
echo "Connection Failed";
}
// close the ssl connection
ftp_close($conn_id);
?>`
The code connects to the server just fine but it can't write to the folder even though it has 0777 permissions on the server.
use ftp_mkdir not mkdir to create a directory on the ftp server.
Related
I need to transfer an file created but I keep getting the following error:
Warning: ftp_put(): Can't open data connection for transfer of "/server.txt"
I can access the ftp via an ftp application and I can create and transfer files via the application but when I try to do the same via PHP it doesn't work.
This is my code.
$serverFile = "server.txt";
$localfile = "/var/sites/store/publish/store-201908-1323-rev9286/www/text.txt";
$localfile = "text.txt";
//Connect to ftp
$ftp_server = "111.111.111.11";
$ftp_user_name = "username";
$ftp_user_pass = "password";
/*set up basic ssl connection*/
$ftp = ftp_ssl_connect($ftp_server);
// login with username and password
$login_result = ftp_login($ftp, $ftp_user_name, $ftp_user_pass);
ftp_pasv($ftp, true);
if($login_result){
if (ftp_put($ftp, $serverFile, $localfile, FTP_ASCII))
{
Mage::log("successfully uploaded \n",null,"ftp.log");
}
}
ftp_close($ftp);
Am I missing something here, I do not have access to change anything on the ftp-server but I am aware that we have remotely placed files in the server previously.
Here is also a screenshot of the Ftp-connection I am using in WinSCP. I've tried adding the port. I also tried creating a file on the server and did a ftp_get but got the same result.
I checked your code, and server.txt file will be root of path.
In general, ftp user do not have write permission on root.
Please create new directory with 777 permission and try to upload.
I have wrote a script to connect to FTP, it runs correctly when I do it manually but when I run it as cron, it gives me error while connecting to ftp.
The connection is unsuccessful.
//--------------------------- FTP Credentials --------------------------------------------//
$ftp_server='154.0.174.248';
$ftp_user_name="*******";
$ftp_user_pass="********";
//--------------------------- set up basic connection -------------------------------------//
$conn_id = ftp_connect($ftp_server) or die('Connection Unsuccessful');
//------------------------- login with username and password ------------------------------//
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv($conn_id, true);
When using crontab to execute a file you should add the type off the file at the very top off your script. In your case you should add #!/usr/bin/php at the very top off the file.
#!/usr/bin/php
<?php
//code
?>
Why you should add it? When the cron is going to execute the file it needs to know what the file type is, and in this method you can declare it.
I have upload script for file uploading from php which is configured to upload file on hosting server from godaddy. Same script I am using on different server domain it is not able to upload on that server which is also godaddy server. I have full access to the folder and server ftp but still didnt find out why this script is not working on that server. Anyone can find out why it is throwing 500 error.
<?php
$ftp_server = "ftp.xxxxx.com";
$ftp_username ="xxxx#xxxx.com";
$ftp_password = "xxxxxxxxxxx";
$destination_folder = "/upload/";
$file_name = $_FILES["userfile"]["name"];
$destination_file = $destination_folder.time().'_'.$file_name;
$file = $_FILES["userfile"]["tmp_name"];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
// upload a file
if (ftp_put($conn_id, $destination_file, $file, FTP_BINARY)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
?>
Do you have shell access? If so, reconfig apache. It may sound not related to your problem, but this will solve it. I had this problem some time ago and rebuilding apache fixed it.
I am attempting to use ftp_login() but continue to get the error:
Warning: ftp_login(): Login authentication failed in /path/to/file.php
I am 100% positive that the username and password are correct. Login succeeds when using an FTP client like WinSCP or FileZilla. There are no symbols in either the password or username.
Here's the code:
$ftp_host = 'host.com';
$ftp_user_name = 'user#host.com';
$ftp_user_pass = 'password';
$connect = ftp_connect( $ftp_host ) or die( "Couldn't connect to server!!!" );
$login_result = ftp_login( $connect, $ftp_user_name, $ftp_user_pass );
I am using HostGator and I am trying to transfer files this domain to a sub-domain also with HostGator.
Please help. Thanks!
Try dropping #host.com from your $ftp_user_name string.
Use this to see if your credentials are correct:
if (ftp_login($connect, $ftp_user_name, $ftp_user_pass)) {
echo "Connected as $ftp_user_name#$ftp_host\n";
} else {
echo "Couldn't connect as $ftp_user_name\n";
}
Note, this code will show errors.
Try changing your ftp password, click View FTP Details and change your password.
You're better off with a proper ftp client than a web based file manager though and also they're notorious for corrupting files.
I have written a script to upload files from my computer to a external server. However I get the following error message:
Warning: ftp_put(): Filename invalid
I have tried changing the filename and etc but have had no luck.
Here is my code:
$ftp_server = "ipaddress";
$username = "username";
$password = "password";
$destination_file = 'C:\ftp_root\borna\ttt.txt';
$source_file = 'C:\inetpub\ftp_test.txt';
// setup basic connection
$conn_id = ftp_connect($ftp_server);
// check connection
if(!$conn_id){
print "FTP connection has failed.<br>";
}else{
print "FTP connection successfull.<br>";
}
// login with username and password
$login_result = ftp_login($conn_id, $username, $password);
// check login
if(!login_result){
print "FTP login credentials not recognised.<br>";
}else{
print "FTP login credentials recognised.<br>";
}
// upload our file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// read upload status
if(!$upload){
print "FTP upload has failed.<br>";
}else{
print "FTP upload was successfull on file $source_file to $destination_file via $ftp_server.<br>";
}
// close the ftp stream
ftp_close($conn_id);
I'd say the error is correct:
$destination_file = "C:\ftp_root\borna\ttt.txt";
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
You're feeding it with a local path as target path (see the C: bit).
If unsure about what server paths will look like, just open the site with a regular FTP client.
If you don't have an FTP client you can download a free one called FileZilla. Install it, launch it, and type your FTP credentials (the piece of data you have in $ftp_server, $username and $password variables) in the top bar. Once you've established a connection you'll see remote paths in the Remote Site box.