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);
Related
File is created on the FTP server, but its always 0 bytes large. Please give me a solution so that the file upload will working success.
I keep getting this warning:
Warning: ftp_put (): PORT command successful in C: \ xampp \ htdocs \ mailing \ teskirim-file-simpan2.php on line 30
FTP upload has failed!
My script is:
<?php
$ftp_server = "********";
$ftp_serverpath = "ftp.".$ftp_server;
$ftp_user_name = "********";
$ftp_user_pass = "***********";
$email_dir = "*******#*********";
$nyambungkeftp = ftp_connect($ftp_server);
if (false === $nyambungkeftp) {
throw new Exception('Unable to connect');
}
$loggedInnyambungkeftp =
ftp_login($nyambungkeftp, $ftp_user_name, $ftp_user_pass);
if (true === $loggedInnyambungkeftp) {
echo 'Success!';
} else {
throw new Exception('Unable to log in');
}
if ((!$nyambungkeftp) || (!$loggedInnyambungkeftp)) {
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
$dest = 'detectip.txt';
$source = 'C:\xampp\htdocs\persuratan\file2\detectip.txt';
echo $dest;
echo $source;
$upload = ftp_put($nyambungkeftp, $dest, $source, FTP_ASCII);
// 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($nyambungkeftp);
?>
PHP defaults to the active FTP mode. The active mode hardly ever works these days due to ubiquitous firewalls/NATs/proxies.
You almost always need to use the passive mode.
For that call the ftp_pasv after the ftp_login:
ftp_pasv($nyambungkeftp, true);
See my article on FTP connection modes, to understand, why you typically need to use the passive mode.
Try two things:
Try FTP_BINARY instead of FTP_ASCII
Try to use passive mode doc here
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 am trying to uplaod a zip file using php in ftp connection. only the zip is uploading but there is no folders or file in the zip folder.
Here is the code which i m trying:
$host = 'Your Ftp host';
$usr = 'Your Ftp User Name';
$pwd = 'Your Ftp password';
// file to move:
$local_file = 'C:\wamp\www\demo.zip';
$ftp_path = '/demo.zip';
// 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, 0644, $ftp_path) !== false) {
print $ftp_path . " chmoded successfully to 644\n";
} else {
print "could not chmod $file\n";
}
// close the FTP stream
ftp_close($conn_id);
Please provide me a solution, m trying to do this from past 3 days..
If the file appears on the server but the size/content of the file is not correct, it may be a problem with your transfer mode (FTP_ASCII or FTP_BINARY).
Try replacing your put line with:
// perform file upload
$upload = ftp_put($conn_id,$ftp_path,$local_file, FTP_BINARY);
<?php
ini_set('max_execution_time', '0');
$host = '234.546.155.485';
$usr = 'fgfgfgdf';
$pwd = 'fghghh';
// file to move:
$file = 'http://vsomesite.com/file.flv';
$ftp_path = '/public_html/video57242/test.flv';
// 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, $file, FTP_ASCII);
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
?>
upload fails when the file is remote not local. what problem?
Your variables don't add up. You use $local_file in your ftp_put() call, but you only declare $file earlier in the script
And are you sure you should be using FTP_ASCII for a FLV file? I'd have thought that was binary.
Edit
Scratch my first point. Seems you edited it away :)
I have installed XAMPP vrsion 1.7.2 on my Mac OS 10.5.7 ?
I am using the following code to upload a file , but i am getting few errors
<?
$host = 'localhost';
$usr = 'nobody';
$pwd = 'xampp';
// 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
// file to upload:
$local_file = './del.php';
$ftp_path = '/del.php';
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
// check upload status:
if(!$upload) {
print 'Cannot upload' ;
} else {
print '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 am getting this warning .
Warning: ftp_put() [function.ftp-put]: /del.php: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/test/ftp1.php on line 31
Cannot upload
Warning: ftp_chmod() [function.ftp-chmod]: /del.php: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/test/ftp1.php on line 84
could not chmod
What is the problem ? Is there any setting that I need to enable ?
It looks like PHP does not have read/write permissions to that directory. This is something that you can resolve on your Mac while developing locally but be aware that the problem may likely repeat itself when you migrate to your production server.
$local_file = './del.php';
Check your del.php file permissions.