Code: Select all
<?php
// Validate file types
$allowed_filetypes = array('.csv');
// Maximum filesize at 2MB
$max_filesize = 2097152;
// Upload directory
$upload_path = './';
// Get filename
$filename = $_FILES['userfile']['name'];
// Get extension
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
// Settings
$host = 'ftp.domain.com';
$usr = 'username';
$pwd = 'password';
$ftp_path = '/directory/filename.csv';
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('Please select a valid csv file.');
// Check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
// Connection
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
ftp_pasv($conn_id, true);
$upload = ftp_put($conn_id, $ftp_path, $filename, FTP_ASCII);
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
?>