File upload through FTP in php - php

I want to upload a file to multiple FTP.But it is showing error :
ftp_put() [function.ftp-put]: php_connect_nonb() failed: Operation now in progress (115).
Have a look to my code
foreach ($channel_details as $channel_list)
{
if(isset($connection))
unset($connection);
if(isset($login))
unset($login);
if(isset($upload))
unset($upload);
$server = $channel_list['channel'];
$ftp_user_name = $channel_list['username'];
$ftp_user_pass = $channel_list['password'];
$source='file_push.html';
$dest='/public_html/'.$path.$file;
$connection = ftp_connect($server) or die("Couldn't connect to ftp server");
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
// turn passive mode on
ftp_pasv($connection, true);
if (!$connection || !$login) { die('Connection attempt failed!'); }
$upload = ftp_put($connection, $dest, $source, FTP_ASCII);
if (!$upload) { echo 'FTP upload failed!'; }
ftp_close($connection);
}

Your code seems to be correct.
Maybe there is a firewall problem?
See here: https://bugs.php.net/bug.php?id=47110

Related

ftp_put(): Could not create file

I do not know what to do anymore I'm trying to overwrite a file from ftp with windows to a remote linux server using php.
I already tried with ftp_pasv in true, saying that I can not change the mode, I put this line in my file vsftpd.conf: pasv_promiscuous = YES and it does not work either.
Check the route in filezilla and it's the same.
Code php:
$ftp_server = 'ip';
$ftp_user_name = 'user';
$ftp_user_pass = 'pass';
$file = 'C:/archivos/sip_trunk.conf';
$remote_file = '/home/sk/sip_trunk.conf';
$conn_id = ftp_connect($ftp_server) or die("Unable to connect to host");
ftp_pasv($conn_id, true) or die("Unable switch to passive mode");
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("Authorization failed");
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
$text = "Upload $file\n";
} else {
$text = "Error Upload $file\n";
}
ftp_close($conn_id);
Error ftp_pasv in false:
ftp_put(): Could not create file.
Arguments:
FTP Buffer resource #315
"/home/sk/sip_trunk.conf"
"C:/archivos/sip_trunk.conf"
1
Error ftp_pasv in true:
Unable switch to passive mode
I do not think it's because I'm running the code on a larvel controller

how to get file from ftp server and copy on own server

I want to get file from client server and copy them on my server , I have successfully connected to client server, my code is below.
// connect and login to FTP server
$ftp_server = "xx.xxx.xxx.xxx";
$ftp_username = 'xxxxxxxxxxxxxxx';
$ftp_userpass = 'xxxxxxxxxxxxxxxx';
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
echo "<pre>";
print_r($login);
echo "</pre>";
// get the file list for /
$filelist = ftp_rawlist($ftp_conn, "/");
// close connection
ftp_close($ftp_conn);
echo "<pre>";
print_r($filelist);
echo "</pre>";
// output $filelist
var_dump($filelist);
May anyone please advise how can I achieve this?
You can use the ftp_fget function specified here: http://php.net/manual/en/function.ftp-fget.php
(ftp_fget() retrieves remote_file from the FTP server, and writes it to the given file pointer.)
Here an example provided by the documentation:
<?php
// path to remote file
$remote_file = 'somefile.txt';
$local_file = 'localfile.txt';
// open some file to write to
$handle = fopen($local_file, 'w');
// 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 $remote_file and save it to $handle
if (ftp_fget($conn_id, $handle, $remote_file, FTP_ASCII, 0)) {
echo "successfully written to $local_file\n";
} else {
echo "There was a problem while downloading $remote_file to $local_file\n";
}
// close the connection and the file handler
ftp_close($conn_id);
fclose($handle);
?>
This is how I resolved this now all files will copy on your server. use ftp_ssl_connect if its secure
$ftp_server = "xx.xxx.xxx.xxx";
$ftp_username = 'xxxxxxxxxxxxxx';
$ftp_userpass = 'xxxxxxxxxxxxxxxxxxx';
$ftp_conn = ftp_ssl_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
ftp_pasv($ftp_conn,pasv);
$output_directory="files1/ftpgetfiles/137/";
// get the file list for /
$filelist = ftp_nlist($ftp_conn, "/");
foreach ($filelist as $key => $value) {
$fp = fopen($output_directory.$value,"w");
if(ftp_fget($ftp_conn, $fp, $value, FTP_BINARY))
{
fclose($fp);
}
}
ftp_close($ftp_conn);

ftp_get(): Transfer failed

This is the code that I am using
<?php
// connect and login to FTP server
$ftp_server = "ftp_server";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
ftp_pasv($ftp_conn, TRUE);
ftp_set_option($ftp_conn, FTP_TIMEOUT_SEC, 2800);
$ftp_username="ftp_username";
$ftp_userpass="ftp_userpass";
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$local_file = "local_file";
$server_file = "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);
I get the same error all the time-
"Warning: ftp_get(): Transfer failed in myfile.php on line 33
Error downloading server_file"
I try to see if the file that I am trying to get is the right one so I used
this code-
$res = ftp_size($ftp_conn, $server_file);
if ($res != -1) {
echo "size of $server_file is $res bytes";
} else {
echo "couldn't get the size";
}
I get the file size so the file is exist. The file is about 11MB so the size of the file not suppose to be an issue.
I added the lines :
ftp_pasv($ftp_conn, TRUE);
ftp_set_option($ftp_conn, FTP_TIMEOUT_SEC, 2800);
After searching solutions on the web but the results are the same with or without those lines...
Any ideas?
Cheerz
You need to call ftp_pasv() after ftp_login(), not before. Check the return value of the function call to see if it succeeds.

opendir() of an ftp url fails

I'm trying to open a folder from a distant server. I wrote :
if ($folderHandle = opendir($folder))
where $folder = "ftp://xxx:xxx#xxx.net:21"
I get the weird error Warning: opendir(ftp://...:21): failed to open dir: operation failed in ... on line 38
Any ideas as to where I should go from here ? Is it a problem with the FTP credentials ?
You could use PHP's FTP Capabilities to remotely connect to the server and get a directory listing:
// set up basic connection
$conn_id = ftp_connect('otherserver.example.com');
// login with username and password
$login_result = ftp_login($conn_id, 'username', 'password');
// check connection
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
exit;
}
// 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";
}
// Retrieve directory listing
$files = ftp_nlist($conn_id, '/remote_dir');
// close the FTP stream
ftp_close($conn_id);

Unable to upload file to ftp server using php

I am trying to upload file to FTP server using php but it is not getting uploaded.
Code:
$response =<<<RESPONSE
<cdm:Response>
<cdm:header exportTime="{$export_time}" baseVersion="{$baseline_snapshot_id}" version="{$this->snapshot_id}">
<cdm:countryCode>{$this->domain}</cdm:countryCode>
<cdm:description>{$description}</cdm:description>
<cdm:environment>{$destination}</cdm:environment>
<cdm:name>{$name}</cdm:name>
</cdm:header>
<cdm:Status>{$this->status}</cdm:Status>
</cdm:Response>
RESPONSE;
$handler = fopen($log_file_name, 'w');
fwrite($handler, $response);
fclose($handler);
$server = "adoshi.dev.com";
$ftp_user_name = "adoshi";
$ftp_user_pass = "*******";
#$source = $handler;
$mode = "FTP_ASCII";
$dest = "/home/adoshi/ftp_folder";
$connection = ftp_connect($server);
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
if (!$connection || !$login) { die('Connection attempt failed!'); }
$upload = ftp_nb_put($connection, $dest, $handler, $mode);
if (!$upload) { echo 'FTP upload failed!'; }
ftp_close($connection);
I have provided all login credentials proper and so still wondering that why it is not uploading to remote server using php.
Any guidance would be highly appreciated.
You dont need to create a file handler to upload them you just need the path to that file and the filename like this
$destFile= "test.htm";
$lokal_file = "test.htm";
$upload = ftp_put ($connection_id, $destFile, $lokal_file, FTP_ASCII);

Categories