Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.