SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File extension in_array()

    Hi,

    I have 5 upload file fields.. I need it to check through the 5 files and make sure that they are all .jpg files.. If any of the 5 files are different extensions, then populate $error_msg.

    Been trying with code bellow, but it seems to let the form submit the files are mixed say jpg and png..

    Code:
    	$allowedExtensions = array("jpg","jpeg","pjpeg"); 
      		foreach ($_FILES['photoname'] as $file) { 
        		if ($file['tmp_name'] > '') { 
          		if (!in_array(end(explode(".", 
                		strtolower($file['name']))), 
                		$allowedExtensions)) { 
           		$error_msg = 'File type not allowed'; 
          		} 
        	} 
      	}
    What am I doing wrong?

    Thanks

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Should something like

    Code:
    	$types_array = array("image/jpeg","image/pjpeg");
    	if (!in_array($_FILES['photoname']['type'], $types_array)) {
    	$error_msg = 'Invalid File type';
    	}
    ?

    I tried a few ways using exif_imagetype() But its not working out I guess because its not a single file being sent?

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You should not be using "type" from $_FILES either...
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Member blogaddition's Avatar
    Join Date
    Sep 2012
    Location
    India
    Posts
    23
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Use following code to validate exact

    <!-- your file fields are-->
    <input type="file" name="photoname[]" id="file1"/>
    <input type="file" name="photoname[]" id="file2"/>
    <input type="file" name="photoname[]" id="file3"/>
    <input type="file" name="photoname[]" id="file4"/>
    <?php

    //php code to validate
    $allowedExtensions = array("jpg","jpeg","pjpeg");
    $error_msg = array();
    foreach ($_FILES['photoname'] as $file) {
    $file_name = $file['name'];
    $file_ext = substr($file_name, strrpos($file_name, '.')+1);
    if(!in_array($file_ext,$allowedExtensions)){
    $error_msg[] = 'File '.$file_name.' is not allowed';
    }
    }

    if(count($error_msg)>0){
    foreach($error_msg as $errors){
    echo $errors."<br/>";
    }
    }
    ?>


Bookmarks

Posting Permissions

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