Page 1 of 1

Upload 0kb again

Posted: Wed Jan 26, 2011 5:31 pm
by weblearner
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";

?>

Re: Upload 0kb again

Posted: Thu Jan 27, 2011 1:48 am
by cpetercarter
Where do you think the problem is - in the file upload, or in moving the file from the temporary upload location to $ftp_path? If you are not sure, check that full error reporting is on, and use a function such as is_uploaded_file() to check whether the file has uploaded successfully. If it has not uploaded, check the html of the form which is supposed to upload it - the <form> tag should have the attribute 'enctype="multipart/form-data"'. If that is not the problem, it may be that php on your server is set up with a low value for upload_max_filesize.

If the file is uploading ok, but is somehow getting lost on its way to the place you want to put it, try using move_uploaded_file() instead of the ftp functions - this is the normal, and much simpler, way of moving an uploaded file.

Re: Upload 0kb again

Posted: Fri Jan 28, 2011 4:27 am
by weblearner
Hey cpetercarter,
thanks for helping...had checked and resolved it...need to set permission on the host side.