Upload 0kb again
Posted: Wed Jan 26, 2011 5:31 pm
Hi all, been pulling my hair trying to upload a csv file but always get 0kb results...would appreciate if anyone could help me look at my code and give some pointers as to where is the problem, much appreciated, thanks.
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";
?>