upload 0kb file

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 file

Post by weblearner »

hi all, still struggling with this,
problem: am trying to upload a csv file into CSV folder on the hosting but it's always 0kb
had created a new ftp user account specifically upload folder with full permission
would really appreciate if you guys could help me a hand...thanks :)

file structure on the host:
/root
/httpdocs
/CSV
--filename.csv --- 0kb
--home.php
--uploadcsvform.php


here's the code:

<?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.********.com';
$usr = '**********';
$pwd = '**********';
$ftp_path = './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 completed';
print "\n";

// close the connection
ftp_close($conn_id);

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

Re: upload 0kb file

Post by cpetercarter »

Your code includes $_FILE, which suggests that you have already uploaded a file using your form. The normal way of moving an uploaded file to the place you want it to be is to use move_uploaded_file(), not the ftp functions.

Incidentally, the form which uploads the file has to have an attribute enctype="multipart/form-data", otherwise the file wont upload
weblearner
Forum Newbie
Posts: 20
Joined: Wed Dec 29, 2010 9:01 am

Re: upload 0kb file

Post by weblearner »

tks cpetercarter

the form code is:

<form action="csvuploadscript.php" method="post" enctype="multipart/form-data" id="upload">
<p>
<label for="file">Select a file:</label>
<input type="file" name="userfile" id="file" />
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<br />
<input type="submit" name="button" id="button" value="Submit" />
</p>
<p>&nbsp;</p>
</form></td>

do you mind give me some pointers how to tweak my code to include the move_uploaded_file function?
thanks
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: upload 0kb file

Post by cpetercarter »

Code: Select all

<?php

//have we uploaded a file successfully
if (!isset($_FILES['userfile']))
{
	die ('Sorry, the file upload has not succeeded');
}

//possible error code?
if ($_FILES['userfile']['error'] != 0)
{
	die ('There was a problem with the file upload. The returned error code is ' . $_FILES['userfile']['error'] .
		'Further information about error codes <a href="http://www.php.net/manual/en/features.file-upload.errors.php">here</a>');
}
// Validate file types
$allowed_filetypes = array('.csv');

// Get filename
$filename = $_FILES['userfile']['name'];

// Get extension
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

// 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.');
}

$success = move_uploaded_file($_FILES['userfile']['tmp_name'], './filename.csv');

echo ($success) ? 'Moved uploaded file to ./filename.csv' : 'I have not been able to move the uploaded file.';
?>
You do not need to specify a maximum file size for the upload. The maximum size of an uploaded file is set in the php.ini file for your site. The default value is often 2MB. If you try to upload a larger file than this, $_FILES['userfile']['error'] will contain an error code 1.

The directory into which you want to move the uploaded file needs to be writable by php. This often means that it needs 0777 permissions.
Post Reply