www.webdeveloper.com
Results 1 to 3 of 3

Thread: Validate if uploaded file is png or jpg with php

  1. #1
    Join Date
    Mar 2013
    Posts
    18

    Validate if uploaded file is png or jpg with php

    I am using the below script to validate if uploaded files are image for a multiple upload form. Everything works but I want to further validate that the uploaded files are png or jpg only. How do I accomplish this? I am new to php so any help will be appreciated. thanks.

    PHP Code:
            $images = array();
            
    $pic $_FILES['file']['tmp_name'];
            foreach(
    $pic as $image){
              if(!empty(
    $image)) {
                  if(!
    getimagesize($image)){
            
    $errors['image_error'] = "PNG and JPG Files ONLY.";
        }
        }} 

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,537
    Assign the results of getimagesize() to a (array) variable, then either look at index 2 for the applicable IMG_* constant value or else index 'mime' for the mime-type.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2011
    Posts
    101
    @carygh:

    The script I gave you in your previous thread has worked without error
    on over two-dozen websites for over two years. It validates according
    to what you populate the $allowtype() array with.

    C

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles