Jump to content

file field validation $_FILES??


DasHaas

Recommended Posts

I have the following code that validates my form fields. I have a file field called file that I cant figure out how to validate. Any help will be greatly appreciated

 

my code

<?php
// validate text input fields an check password
    if (trim($_POST['txtname']) == '') 
    {$errorList[] = 'Please enter your name';}

    if (trim($_POST['txtemail']) == '') 
    {$errorList[] = "Please enter your email address";}
    
    if (trim($_POST['txtphone']) == '') 
    {$errorList[] = "Please enter your phone number";}
    
    if (trim($_POST['txtpassword']) == '') 
    {$errorList[] = "Please enter a password";}
    
    if (trim($_POST['txtpassword1']) == '') 
    {$errorList[] = "Please re-enter your password";}
    
    if (trim($_POST['sslimpressions']) == '') 
    {$errorList[] = "Please select a number of impressions";}    
    
    if (trim($_POST['ssladtype']) == '') 
    {$errorList[] = "Please select ad type";}		

    if (trim($_POST['txturl']) == '') 
    {$errorList[] = "Please enter your website url";}	

    if (trim($_POST['txtalt']) == '') 
    {$errorList[] = "Please enter your business name";}

if ($_POST['txtpassword'] != $_POST['txtpassword1'])
{$errorList[] = "Passwords did not match";}

if($_FILES['file']['size'] > 3000000)
    {$errorList[] = "Selected file is too large";}	
?>

 

i tried using:

 

if($_FILES['file']['size'] = 0)
    {$errorList[] = "Please select a file";}	

 

but it did not work, it let me submit the form

Link to comment
https://forums.phpfreaks.com/topic/63131-file-field-validation-_files/
Share on other sites

To elaborate on the previous post, if the user doesn't submit a file, the files array will be empty, so all you have to do to see if the user submitted a file is if (isset($_FILES['file'])) { doSomething; }. Of course then you'd proceed to validating it by checking type and size and all, but it looks like you've got that going.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.