I want to be able to upload a csv file once a day (locally from my pc) to ftp.
I then am going to insert this csv file into a mysql table.
I've created the cron job to pick up a csv and insert it into the database, but I'm struggling how to figure out how to pick up a file that is on my loacl pc and upload it to FTP.
Has anyone got any ideas?
Thanks
Adi
You can do using the ftp extension in PHP, something like:
$conn = ftp_connect("destination.host", 21) or die("failed to connect");
ftp_login($conn, $user, $pass) or die("failed to login");
ftp_put($conn, "/path/on/ftp/server", "/path/on/your/local", FTP_BINARY) or die("failed to upload);
More details: http://us2.php.net/manual/en/book.ftp.php
// open some file for reading
$file = 'somefile.txt';
$fp = fopen($file, 'r');
$ftp_user_name="xxxxxx";
$ftp_user_pass="xxxxx";
// set up basic connection
//$conn_id = ftp_connect($ftp_server);
$conn_id = ftp_connect("xxxxxxx.com", 21) or die("failed to connect");
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to upload $file
if (ftp_fput($conn_id, $file, $fp, FTP_ASCII)) {
echo "Successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection and the file handler
ftp_close($conn_id);
fclose($fp);
Related
i need a list of all the files on an FTP server. I can get a list of files using php , but no idea how to get the file size. How do I get the file sizes? Thanks.
You can call
ftp_size
As in
int ftp_size ( resource $ftp_stream , string $remote_file )
To get the size of files over FTP.
For example:
$file = 'somefile.txt';
// 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);
// get the size of $file
$res = ftp_size($conn_id, $file);
if ($res != -1) {
echo "size of $file is $res bytes";
} else {
echo "couldn't get the size";
}
// close the connection
ftp_close($conn_id);
However only some FTP servers support this.
Source: http://php.net/manual/en/function.ftp-size.php
Im using php code to upload a file.
The problem is i can only code it to upload file on my local host.
How do I code the target directory that can upload file in server using info such as localhost, name, password etc.
You are looking for a remote file transfer such as ftp.
Taken from the PHP manual:
<?php
$ftp_server="";
$ftp_user_name="";
$ftp_user_pass="";
$file = "";//tobe uploaded
$remote_file = "";
// 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);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
?>
I want to upload the directory content which includes only text files to a ftp server and it should be automated through windows task schedular. Right now I'm using this but its only uploading one file, My requirement is basically a user can copy its files to a specific folder and it should be uploaded on ftp server asap.
this is the code I'm using right now:
$host = 'ftp.xyz.com';
$usr = 'abc123';
$pwd = 'l2345';
date_default_timezone_set('Asia/Kuala_Lumpur');
$date = date('d-m-Y_hi');
$name = $date.".txt";
$local_file = '/Applications/XAMPP/xamppfiles/htdocs/Conversion/Upload/xyz/abc.txt';
$ftp_path = '/public_html/xyz/abc.txt';
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_BINARY);
echo (!$upload) ? 'Cannot upload' : 'Upload complete';
echo "\n";
if (!function_exists('ftp_chmod')) {
function ftp_chmod($ftp_stream, $mode, $filename){
return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
}
}
if (ftp_chmod($conn_id, 0666, $ftp_path) !== false) {
echo $ftp_path . " chmoded successfully to 666\n";
} else {
echo "could not chmod $file\n";
}
ftp_close($conn_id);
To upload multiple files, you'll want to either issue a series of FTP (connections and) transfers with a loop in your code...or use a wildcard in FTP's interactive mode.
i understand that the ftp_put method uploads a file from the local server computer to the ftp server but i have problems using it where when i try to execute a simple script like this:
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file = "localfile.txt";
// upload file
if (ftp_put($ftp_conn, "serverfile.txt", $file, FTP_ASCII))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
?>
the operation is successfully done except for that the file uploaded to the my ftp server is always with zero byte size!
also i tried to enable passive mode but it still uploads an empty file.
Try enabling track_errors and access $php_errormsg
ini_set('track_errors', 1);
// put operation
// if error
var_dump($php_errormsg);
I had the same issue. When I change "FTP_ASCII" to "FTP_BINARY" it solved my problem and files uploaded as expected.
I had ran into this as well. Bit late but thought I'd post my solution:
$file_name = "localfile.txt";
Get content from your existing file
$content = file_get_contents('http://www.somewhere.com/'.$file_name);
...or make temp file content
$content = "This is content";
upload file
// connect
$conn_id = ftp_connect($host);
$login = ftp_login($conn_id, $username, $password);
ftp_pasv($conn_id, true);
// create
$tmp = fopen(tempnam(sys_get_temp_dir(), $file), "w+");
fwrite($tmp, $content);
rewind($tmp);
// upload
$upload = ftp_fput($conn_id, "serverfile.txt", $tmp, FTP_ASCII);
// close
ftp_close($conn_id);
I tried to open a file in another ftp and write but I coulnd how can I do that?
$ftpstream = #ftp_connect('****');
//Login to the FTP server
$login = #ftp_login($ftpstream, '****', '***');
if($login) {
echo "Conectado";
$fp = fopen('file.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
}
The code you have posted is wrong. It opens up an FTP stream (ftp_connect), but then writes a file to the local file system (fopen). Your ftp stream won't allow you to write to it with fwrite- you need to use the commands to transfer entire files.
You can do what you want with fopen if you use an ftp:// scheme.
For example:
$fp = fopen("ftp://user:password#example.com/file.txt","w");
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
Alternativley, you can write the file to the local file system and use an ftp stream to transfer it:
$file = 'somefile.txt';
// create your file in the local file system here.
$remote_file = 'readme.txt';
// 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);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}
// close the connection
ftp_close($conn_id);
As always, be clear on what you want to do.
PHP FTP reference is here
PHP scheme & wrapper reference (for use with fopen) is here