I am trying to upload image from one server to another server through FTP by using PHP.But the uploaded data shows.pureftpdupload.5809ed2f.15.7b36.24316ca6 error.
I am using this code.
$connection = 'servername';
$username = 'xxxx';
$password = 'yyyy';
$local_file = 'http://servername/test.jpg';
$remote_file = 'admin/files/company/test.jpg';
$connection = ftp_connect($server);
if (#ftp_login($connection, $username, $password)) {
// successfully connected
//echo 'connected';exit;
} else {
echo 'not connected';
return false;
}
if (ftp_put($connection, $remote_file, $local_file, FTP_BINARY)) {
echo "successfully uploaded\n";
} else {
echo "There was a problem while uploading \n";
}
My guess is that you want to upload a local file from a web server. But by mistake or misunderstanding, you are using file's HTTP URL (http://servername/test.jpg) instead of file's local path.
Try to use a real local path, like /home/user/test.jpg.
This may work as a generic solution: $_SERVER['DOCUMENT_ROOT']."/test.jpg"
Related
I am able to connect normally to filezilla (ftp). But the return from the ftp_fput () method returns false. This means that you were unable to upload the file.
Here is my code:
$server = "host.host.ws";
$user = "username";
$pass = "password";
// call function
$conn = uploadFTP($server, $user, $pass, $local_file, $remote_file);
function uploadFTP($server, $username, $password, $local_file, $remote_file) {
$connection = ftp_connect($server);
if (#ftp_login($connection, $username, $password)){
ftp_pasv($connection, true) or die("Unable switch to passive mode");
$fp = fopen('php://temp', 'r+');
fwrite($fp, 'content within the file');
rewind($fp);
// the file is not generated in the remote directory: /App/data/Json/File.json
if (ftp_fput($connection, '/App/data/Json/File.json', $fp, FTP_ASCII))
echo 'generated file!';
fclose($fp);
ftp_close($connection);
return true;
} else {
return false;
}
The file will only be generated when it displays the message: "generated file!", But it is not what is happening.
The code doesn't show any explicit errors, my question is, why doesn't this code generate the file in the directory? Could you share a solution to this problem?
Perhaps FTP gives access to the root directory and you need to provide the full path /var/www/App/data/Json/File.json
ftp_fput($connection, '/var/www/App/data/Json/File.json', $fp, FTP_ASCII)
The error is not visible because suppression is on.
Remove symbolically here "#" and use error_get_last() To see error information
if (ftp_login($connection, $username, $password)) {
echo 'Connected!';
if (ftp_fput($connection, '/App/data/Json/File.json', $fp, FTP_ASCII)) {
echo 'generated file!';
} else {
print_r(error_get_last());
}
} else {
return false;
}
You need to make sure the directory structure is created on the server /App/data/Json/ If not, then create it
I think it will be useful
php ftp make multiple directories
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 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);
Can someone figure out what this is not working? I kept trying different things, double checking syntax etc but I get the error message I have setup for when a file does not transfer. This is my first PHP script.
I appreciate any help I will do research in the meantime.
Using NetBeans
<?php
$user = "username";
$password = "password";
$server_file = 'mag_hounddog.mp3';
$local_file = 'C:\users\kazuiman\desktop\mag_hounddog.mp3';
$ftp_server = "ftp.mysite.com";
// setup a basic connection to the ftp server
$connectionID = ftp_connect($ftp_server) or die("Connection was Not Successful");
// login with username and password for your account
$loginResult = ftp_login($connectionID, $user, $password);
echo ftp_pwd($connectionID);
// changes the directory to the tvstation you can hard code one in here.
if (ftp_chdir($connectionID, "test1nbiiwcdp_apps/tv2beta/streams/_definst_/")) {
echo "Current Directory is Now..." . ftp_pwd($connectionID) . "\n";
} else {
echo "Error Changing Directory .. perhaps it doesn't exist?";
}
// Now to download the files and put them onto your local machine
if (ftp_get($connectionID, $local_file, $remote_file, FTP_BINARY)) {
echo "Succesfully written $server_file to $local_file\n";
} else {
echo "There was a problem with the FTP transfer please check script\n";
}
// close the connection
ftp_close($connectionID)
?>
You have never defined the variable $remote_file.
In your preamble it is called $server_file.
Is this a copy-paste-error?