I am unable to connect to a FTP server when i am connecting it through our PHP code. Most embarrassing thing is that i get only false when trying to connect server with ftp_connect() method. No errors are reported. This is my first time when i am working with FTP in PHP. if anyone who have previously worked on it can help me out here. Here is my code :
/* Source File Name and Path */
$remote_file = $_SERVER['DOCUMENT_ROOT'] ."/some-folder/file.txt";
/* New file name and path for this file */
$ftp_host = 'ftp://targetserver.com/some folder/';
$ftp_user_name = 'user';
$ftp_user_pass = 'XXXXX';
/* New file name and path for this file */
$local_file = 'ftp://targetserver.com/some-folder/file.txt';
/* Connect using basic FTP */
$connect_it = ftp_connect( $ftp_host, 21);
var_dump($connect_it);
/* Login to FTP */
$login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );
/* Download $remote_file and save to $local_file */
if ( ftp_get( $connect_it, $local_file, $remote_file, FTP_BINARY ) ) {
echo "Successfully written to $local_file\n";
}
else {
echo "There was a problem\n";
}
/* Close the connection */
ftp_close( $connect_it );
Actually getting false means that your ftp client couldn't connect to the server for some reason
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
The best to do before you dive into php debugging is to try out another ftp client such as https://filezilla-project.org/ and see if all works fine, if not you will have saved your time because the issue not coming from your php script.
UPDATE 1
Then try this:
$ftp_host = 'targetserver.com';
http://php.net/manual/en/function.ftp-connect.php#refsect1-function.ftp-connect-parameters
The FTP server address. This parameter shouldn't have any trailing slashes and shouldn't be prefixed with ftp://.
And as for local_file and remote_file, they must be paths and not urls
http://php.net/manual/en/function.ftp-get.php#refsect1-function.ftp-get-examples
As ftp_connect function doesn't produce an error, I assume the extension is installed.
It is very possible this could be a firewall issue with port 21 blocked. (This is a very common reason)
<html>
<body>
<form enctype="multipart/form-data" action="upload_file.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
<?php
$ftp_server = "94.23.x.xxx";
$ftp_username = "anxxxxx";
$ftp_password = "6xxxxxxxx";
// setup of connection
$conn_id = ftp_connect($ftp_server) or die("could not connect to $ftp_server");
// login
if (#ftp_login($conn_id, $ftp_username, $ftp_password))
{
echo "conectd as $ftp_username#$ftp_server\n";
}
else
{
echo "could not connect as $ftp_username\n";
}
$file = $_FILES["uploadedfile"]["name"];
$remote_file_path = "/JustForTest".$file;
ftp_put($conn_id, $remote_file_path, $_FILES["uploadedfile"]["tmp_name"],
FTP_ASCII);
ftp_close($conn_id);
echo "\n\nconnection closed";
?>
Then what's the problem with the above code?
Related
I'm trying to download file via php script, but unfortunately with this script i get error:
PHP Warning: ftp_connect(): php_network_getaddresses: getaddrinfo
failed: nodename nor servname provided, or not known in
/Users/apple/projects/asystem/download_dump.php on line 15
i try to put different FTP address test it and it worked fine
so the problem is 100% in the server address...
// define some variables
$folder_path = "/Users/apple/projects/asystem";
$local_file = "auct_lots_full.xml.zip";
$server_file = "auct_lots_full.xml.zip";
//-- Connection Settings
$ftp_server = "ftp://xxxx_user:Eecohshxxxxxx#auctionsdata.xxxxx.com"; // Address of FTP server.
$ftp_user_name = "xxxxx_user"; // Username
$ftp_user_pass = "xxxx"; // Password
#$destination_file = "FILEPATH";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);
?>
so yes! the problem was that i use wrong path to the FTP. This code works:
//-- Connection Settings
$ftp_server = "auctionsdata.xxxxx.com"; // Address of FTP server.
$ftp_user_name = "xxxxx_user"; // Username
$ftp_user_pass = "xxxx"; // Password
I have tried in many ways to upload files using FTP functions in PHP. But it is not working for me. I don't know what mistake I made in this code. I have given the local file path or another server file path also, but in both cases it is not working. I have given my code below. Can anyone help to find my problem?
/* Source File Name and Path */
$backup_file = '/var/www/html/artbak/assets/uploads/edi/test.txt';
//$backup_file = '/workspace/all-projects/artbak/assets/uploads/edi/test.txt';
$remote_file = $backup_file;
$ftp_host = 'hostname'; /* host */
$ftp_user_name = 'username'; /* username */
$ftp_user_pass = 'password'; /* password */
/* New file name and path for this file */
$local_file = '/public_html/example.txt';
/* Connect using basic FTP */
$connect_it = ftp_connect( $ftp_host );
/* Login to FTP */
$login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );
/* Download $remote_file and save to $local_file */
if ( ftp_put( $connect_it, $local_file, $remote_file, FTP_BINARY ) ) {
echo "WOOT! Successfully written to $local_file\n";
}
else {
echo "Doh! There was a problem\n";
}
/* Close the connection */
ftp_close( $connect_it );
The below code is working for local to server upload, but not working for server to server upload. It shows the server not connecting. Please give some ideas guys. I am struggling in more in this concept.
$ftp_host = 'hostname'; /* host */
$ftp_user_name = 'username'; /* username */
$ftp_user_pass = 'password'; /* password */
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
$file = "dummy.txt";
$remote_file = "receiveDummy.txt";
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
Thanks for your support guys. I am happy to post this answer couple of days work. Only the thing is need to avoid the destination server path /public_html/test.txt instead of test.txt. If you have any sub folders give like sample/test.txt . Kindly check the below code, it is use full for some one like me.
/* Source File Name and Path */
$remote_file = '/var/www/html/artbak/assets/uploads/edi/test.txt';
$ftp_host = 'hostname'; /* host */
$ftp_user_name = 'username'; /* username */
$ftp_user_pass = 'password'; /* password */
/* New file name and path for this file */
$local_file = 'test.txt';
/* Connect using basic FTP */
$connect_it = ftp_connect( $ftp_host );
/* Login to FTP */
$login_result = ftp_login( $connect_it, $ftp_user_name, $ftp_user_pass );
/* Download $remote_file and save to $local_file */
if ( ftp_put( $connect_it, $local_file, $remote_file, FTP_BINARY ) ) {
echo "WOOT! Successfully written to $local_file\n";
}
else {
echo "Doh! There was a problem\n";
}
/* Close the connection */
ftp_close( $connect_it );
And one thing is in my server had some firewall block, for that reason only first ftp not connecting via php code. Now i solve that issue also. Because my code working in local to server already know. For that i referred the below link to solve the firewall block in server, Can't connect to FTP with PHP ftp_connect from localhost . So it is working well.
I'm connecting to FTP server and trying to download a file from it, but I'm getting an error of
Warning: ftp_get() [function.ftp-get]: Can't open /home/a*******/public_html/files/test.txt: No such file or directory in /home/a*******/public_html/MainHomescreen.php on line 213
if(isset($_REQUEST['download']))
{
// connect and login to FTP server
$ftp_server = "********.*****.***";
$ftp_username = "a*******";
$ftp_userpass = "******";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
if(isset($_POST['checkbox']))
{
foreach($_POST['checkbox'] as $selected)
{
//echo $selected;
$local_file = "local.zip";
$server_file = "/home/a*******/public_html/files/$selected";
// download server file
if (ftp_get($ftp_conn, $local_file, $server_file, FTP_BINARY))
{
echo "Successfully written to $local_file.";
}
else
{
echo "Error downloading $server_file.";
}
}
}
// close connection
ftp_close($ftp_conn);
}
it's connecting to the server ok as I've checked that but when I try and download a file it comes up with the error above, but does echo the error line too. I'm not sure whether I've connected to it wrong in $local_file and $server_file, which is my best bet as to where I've gone wrong. Hoping someone might be able to help. Thanks.
I have tried files upload to server using ftp connection in php and its not working, its connecting but getting Error like "Connected to XXXXXXXXXXX, for user XXXXXXXXXXXXX FTP upload has failed!" I have tried following code please help by correcting it,..
image.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Welcome</title>
</head>
<body>
<form action="upload.php" enctype="multipart/form-data" method="post">
<input name="file" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>
</body>
</html>
upload.php
<?php
$ftp_server = "XXXXXX";
$ftp_user_name = "XXXXXXX";
$ftp_user_pass = "XXXXXXXX";
$destination_file = "imagetest/123/".$_FILES['file']['name'];
$source_file = $_FILES['file']['tmp_name'];
// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
I've tested your code and had a bit of a hard time to get it working, but what worked for me was to use something like /http_docs, or /public_html as the base/root folder.
Root folder names will vary from hosting services, so modify accordingly.
I.e. and with a few modifications:
<?php
$ftp_server = "XXXXXX";
$ftp_user_name = "XXXXXXX";
$ftp_user_pass = "XXXXXXXX";
$folder = "/http_docs/imagetest/123/";
$destination_file = $folder . $_FILES['file']['name'];
$source_file = $_FILES['file']['tmp_name'];
// rest of code
Sidenote:
Do not use a full path name.
I.e.: /var/user/you/public_html/ it won't work.
I have a form with html browse type and a submit button. I chose a file using browse button and submit the form. On form submission following code is called.
$conn_id="myid";
$conn_id = ftp_connect ( 'server' );
$ftp_user_name="username";
$ftp_user_pass="password";
// login with username and password
$login_result = ftp_login ( $conn_id , $ftp_user_name , $ftp_user_pass );
// check connection
if ((! $conn_id ) || (! $login_result )) {
echo "FTP connection has failed!" ;
exit;
} else {
echo "Connected to for user $ftp_user_name" ;
}
// upload the file
$upload = ftp_put( $conn_id, "images/signatures/" . $fileName , $_FILES['tmp_name'] , FTP_BINARY );
// check upload status
if(!$upload){
echo "FTP upload has failed!" ;
} else {
echo "Successfully Uploaded." ;
}
But it produce the following warning:
Warning: ftp_put(): Filename cannot be empty in /var/www/echdp/_ProviderSignature.php on line 70 FTP upload has failed!
But when I hard code the source path in above code then it upload the file on server:
$upload = ftp_put( $conn_id, "images/signatures/myfile.txt" , "/var/www/images/hello.txt" , FTP_BINARY );
Try this:
$file = 'somefile.txt';
$remote_file = '/public_html/dir/dir2/dir3/somefile.txt';
$ftp_server = "yourdomain.in";
$ftp_user_name = "ftpusername";
$ftp_user_pass = "ftppassword";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
Your file will be uploaded in the /public_html/dir/dir2/dir3/ directory of your domain.
Caution: Never upload an index file when you are testing this feature.
Source: PHP.net
Example of html code with enctype:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="myfile" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
Do you have enctype in Form tag? Without the attribute your file won't be send.
For debugging of $_FILES array. Try
var_dump($_FILES);
to see if the data are correct.
Your variable:
$_FILES['tmp_name']
is used wrongly because $_FILES contain files not a one file. Therefore you should write:
$_FILES['your-name-in-html-form']['tmp_name'] // your-name-in-html-form = myfile in the example above
Have a look on the example: http://www.zymic.com/tutorials/php/creating-a-file-upload-form-with-php/
$_FILES['tmp_name']
isn't name of your file. It is stored in:
$_FILES['inputname']['tmp_name']
where inputname is name of your field. Also check your form for enctype ="multipart/form-data".
Be aware of simply putting on your server everything that user can send - if there is access to uploaded files by http (like http://yoursite/images/signatures/uploadedfile.xxx) someone can put .php files on your server and execute it !
Obviously $_FILES['tmp_name'] does not contain the filename. Try print_r($_FILES) to see what it contains. Also, use multipart/form-data as your form enctype.