Upload 0kb again

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
weblearner
Forum Newbie
Posts: 20
Joined: Wed Dec 29, 2010 9:01 am

Upload 0kb again

Post 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";

?>
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Upload 0kb again

Post 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.
weblearner
Forum Newbie
Posts: 20
Joined: Wed Dec 29, 2010 9:01 am

Re: Upload 0kb again

Post by weblearner »

Hey cpetercarter,
thanks for helping...had checked and resolved it...need to set permission on the host side.
Post Reply