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);
Related
I have a web form that I will be hosting on a centOS web server. The code that I have written, will take the data that has been entered, and save it to an excel spreadsheet to a specific location. i also have a file server, this is where I would like to have the file saved to. this is the first time that i have done a server to server file save/copy/transfer. on the web server, would i have to install an ftp client to accomplish this? When testing the program locally on my computer, this is how i currently have the destination path set up.
file_put_contents('C:\Users\username\Box Sync\Driver Check In\Shortage'.
$month. '\Shortage'.date('m-d-y').".csv", $result, FILE_APPEND);
with the code being hosted on the web server, how can i change the destination path to point to my file server?
Here is a sample script of how to upload file from your localhost to any FTP server.
Source: PHP Manual
<?php
$host = '*****';
$usr = '*****';
$pwd = '**********';
$local_file = './orderXML/order200.xml';
$ftp_path = 'order200.xml';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_pasv($resource, true);
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// perform file upload
ftp_chdir($conn_id, '/public_html/abc/');
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
if($upload) { $ftpsucc=1; } else { $ftpsucc=0; }
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
// close the FTP stream
ftp_close($conn_id);
?>
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 I create/write on a file on another host & domain with file_put_contents() OR fwrite()?
If I can, what permissions and other property should set on that host?
Thanks ..
check this out from http://www.php.net/manual/en/function.file-put-contents.php#101408
To upload file from your localhost to any FTP server.
pease note 'ftp_chdir' has been used instead of putting direct remote file path....in ftp_put ...remoth file should be only file name
<?php
$host = '*****';
$usr = '*****';
$pwd = '**********';
$local_file = './orderXML/order200.xml';
$ftp_path = 'order200.xml';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_pasv($conn_id, true);
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// perform file upload
ftp_chdir($conn_id, '/public_html/abc/');
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
if($upload) { $ftpsucc=1; } else { $ftpsucc=0; }
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
// close the FTP stream
ftp_close($conn_id);
?>
I wrote a function similar to PHP file_put_contents() which is writing to a FTP server:
function ftp_file_put_contents($remote_file, $file_string)
{
// FTP login
$ftp_server="my-ftp-server.com";
$ftp_user_name="my-ftp-username";
$ftp_user_pass="my-ftp-password";
// Create temporary file
$local_file=fopen('php://temp', 'r+');
fwrite($local_file, $file_string);
rewind($local_file);
// Create FTP connection
$ftp_conn=ftp_connect($ftp_server);
// FTP login
#$login_result=ftp_login($ftp_conn, $ftp_user_name, $ftp_user_pass);
// FTP upload
if($login_result) $upload_result=ftp_fput($ftp_conn, $remote_file, $local_file, FTP_ASCII);
// Error handling
if(!$login_result or !$upload_result)
{
echo('FTP error: The file could not be written on the remote server.');
}
// Close FTP connection
ftp_close($ftp_conn);
// Close file handle
fclose($local_file);
}
// Usage
ftp_file_put_contents('my-file.txt', 'This string will be written to the remote file.');
If you want to use file_put_contents specifically, you have to use a stream context for a protocol the remote server accepts for uploading. For instance, if the server is configured to allow PUT requests, you could create an HTTP context and send the appropriate method and content to the server. Another option would be to setup an FTPs context.
There is an example in the comments for file_put_contents on how to use it with a stream context for FTP. Note that the used ftp://user:pass#host URI scheme is transmitting the user credentials in cleartext.
Additional examples
<?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.