File upload limitation by file type and size.

I have this piece of PHP code here and I need it to limit the uploadable file type to .csv and size to 2 mb.
It does have file size limit but it does it in the weird way and it doesn’t limit the file extension.
What to do?

$connect = mysql_connect(“localhost”,“root”,“asdasd”);
mysql_select_db(“nooruse”,$connect) or die (‘Viga andmebaasi valimisel’);

if ($_FILES[csv][size] > 0) {

$file = $_FILES[csv][tmp_name]; 
$handle = fopen($file,"r"); 
do { 
    if ($data[0]) { 
        mysql_query("INSERT INTO norse5_proov (osakond, soetusaasta, it_number, tooteruhm, mudeli_nimetus, sn, riigivara_nr, inventaari_nr, maja, ruum, vastutaja, markus, kasutajanimi) VALUES 
        ") or die(mysql_error()); 
} while ($data = fgetcsv($handle,1000,",","'")); 

header('Location:insert.php?success=1'); die; 


?> [/php]

I am by no means an expert on this matter and hopefully some of the other members can contribute, but this is something i have also been looking into to suit my needs.

With regards to file size, from what i can see you already have the code in place but you haven’t set a file size.
Something like this should do

if ($_FILES['csv']['size']>2097152) { die('file size is restricted to 2MB'); }

With regards to restricting file type, there are several MIME types for CSV depending on OS & browser. Here is one way of validating MIME types.

[code]$csv_mimetypes = array(

if (in_array($_FILES[‘upload’][‘type’], $csv_mimetypes)) {
// possibly a CSV file

I’m sure there are other, better ways to do it … but just thought i’d throw in my 2 cents worth and maybe i can learn from this also.

Thank you for this piece of code but I have no idea how to use it in my code. Could you please show me?

Sponsor our Newsletter | Privacy Policy | Terms of Service