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);
Related
I would like to copy a file (eg. MYFILE.csv) from FTP 'SOURCE' to FTP 'TARGET'. What type of command would you recommend for a script in php?
I have tried this but it didn't work.
<?php
$server = 'ftp.TARGET.com' ;//address of ftp server
$user_name = 'USER_TARGET'; // Username
$password = 'PASSWORD_TARGET'; // Password
$source = 'MYFILE.csv';
$dest = '/in/MYFILE.csv';
$mode='FTP_ASCII';
// set up basic connection
$connection = ftp_connect($server) ;
// login with username and password
$login_result = ftp_login($server, $user_name, $password);
// upload a file
if (ftp_put($connection, $dest, $source, $mode)) {
echo "successfully uploaded $source\n";
} else {
echo "There was a problem while uploading $source\n";
}
// close the connection
ftp_close($connection);
?>
=> The php script will be hosted in a folder on FTP A.
Thank you for your guidance.
You can use file_put_contents
http://php.net/manual/en/function.file-put-contents.php
file_put_contents('ftp://user:pass#server/path/to/file.txt', $data);
It will return false on a failure:
if(file_put_contents('ftp://user:pass#server/path/to/file.txt', $data)) {
// ftp upload successful
} else {
// ftp upload failed
}
Why does your current method fail though?
I'm trying to download file from ftp, but whatever i tried i couldn't download any file from ftp and it is not giving any error.
My code fetching file names from database and compares these file names with existing ftp file(names). If file name exist on ftp i want to download this file to my local-destination folder.
I can read ftp file names but i can't download them. Hope you can help me.
Here is my code
error_reporting(E_ALL);
$ftp_server = "xx.xx.y12.34"; //server
$bt = ftp_connect($ftp_server); // connect to ftp
$username = "myusername"; $pass = "mypass"; //username and password
$connection = ftp_login($bt, $username, $pass); // login ftp with username and password
ftp_pasv($bt, true);
if ((!$bt) || (!$connection)) {
echo "failed";
exit;
}else {
$path = "/1_Swatch Files/LG Swatches/";
$destination_folder = "/home/faruk/lasenza/media/color_samples/";
// mysql connection codes..
$results = $readConnection->fetchAll($query); // fetch file names from database
$contents_on_server = ftp_nlist($bt, $path);
foreach($results as $result){
$check_file_exist = $result["CLR_CODE"].".gif";
if(in_array($check_file_exist, $contents_on_server)) {
$server_file = $result["CLR_CODE"].".gif";
if (ftp_get($bt, $destination_folder , $server_file, FTP_BINARY)) {
echo 'Succeeded';
}else{
echo "There was a problem";
}
break;
}
}
}
I am trying ftp connections for file read.
All I want to do is
read particular ftp dir
list all its text files
and read them one by one
I am successful in listing files in given directory.
For eg: below code returns $emailfiles array with TEST.txt as one value.
But when I try to open/read the same file content using fopen/file_get_contents , it is giving the error: " Failed to open Stream "
I am able to access a file from another ftp : .38, which is public ftp.
The ftp I want to actually use is .94, for which I have full privileges, as I am able to access files from browser with same link. I'm also able to write/paste files from windows explorer.
But I am unable to read the same file from php code.
Below is my code for ftp connection and testing.
$ftp_server = "10.96.5.94";
$ftp_serverpath = "ftp://".$ftp_server;
$ftp_user_name = "myusername";
$ftp_user_pass = "mypassword";
$email_dir = "ebill";
try {
$con = ftp_connect($ftp_server);
if (false === $con) {
throw new Exception('Unable to connect');
}
$loggedIn = ftp_login($con, $ftp_user_name, $ftp_user_pass);
if (true === $loggedIn) {
echo 'Success!';
} else {
throw new Exception('Unable to log in');
}
ftp_pasv($con, true);
$emailfiles = ftp_nlist($con, $email_dir);
print_r($emailfiles);
//$filename = "ftp://10.16.0.38/ebill/TEST.txt";
$filename = "ftp://10.96.5.94/ebill/TEST.txt";
$pr = file_exists($filename);
// OR
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
Any help is greatly appreciated, Thank you.
Ok, This solved my error.
$filename = "ftp://username:password#hostname/path/to/file";
Thanks.
I have a problem when I use do_upload its always returns an error saying 'The upload path does not appear to be valid'.
function save_blog(){
$config['upload_path'] = 'ftp://username:password#hostname:port/public_html/img/blog/';
//$config['upload_path'] = 'img/blog/';
$config['allowed_types'] = 'gif|jpg|png';
get_instance()->load->library('upload', $this->config);
echo $config['upload_path'];
if($this->upload->do_upload('myDoc'))
{
echo "file upload success";
}
else
{
echo $this->upload->display_errors();
}
}
On my server there are path /public_html/img/blog/
PS* i can use do_upload when i used a simple code like http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
but on my project I have to upload 2 files in one click so I try to use $this->upload->do_upload('myDoc') for one of my input first but it still error.
Updated:
If you need to upload files, do do_upload first by your first method to local folder, and then FTP upload the local file to FTP server.
I think if you need to upload via FTP. for establishing a valid connection to FTP.
http://ellislab.com/codeigniter/user-guide/libraries/ftp.html
Example for Codeigniter.
$this->load->library('ftp');
$config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE;
$this->ftp->connect($config);
$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);
$this->ftp->close();
Exmaple for PHP only. (Credits to http://www.jonasjohn.de/snippets/php/ftp-example.htm)
// FTP access parameters
$host = 'ftp.example.org';
$usr = 'example_user';
$pwd = 'example_password';
// file to move:
$local_file = './example.txt';
$ftp_path = '/data/example.txt';
// connect to FTP server (port 21)
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
// send access parameters
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// turn on passive mode transfers (some servers need this)
// ftp_pasv ($conn_id, true);
// perform file upload
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
/*
** Chmod the file (just as example)
*/
// If you are using PHP4 then you need to use this code:
// (because the "ftp_chmod" command is just available in PHP5+)
if (!function_exists('ftp_chmod')) {
function ftp_chmod($ftp_stream, $mode, $filename){
return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
}
}
// try to chmod the new file to 666 (writeable)
if (ftp_chmod($conn_id, 0666, $ftp_path) !== false) {
print $ftp_path . " chmoded successfully to 666\n";
} else {
print "could not chmod $file\n";
}
// close the FTP stream
ftp_close($conn_id);
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