this script works fine, uploads multiple images and writes them to the db. but how can I restrict it to just images. I have been googleing since last night and seen hundreds of examples and still cant seem to grasp the concept. someone please help. the code I have is:

function findexts ($filename) { $filename = strtolower('$filename') ; 
$exts = preg_split("[/\\.]", $filename) ;
 $n = count($exts)-1; 
 $exts = $exts[$n];
  return $exts;
   }

$ext = findexts ($_FILES['images']['name']) ; 
$ran = rand ();
$ran2 = $ran.".";

while(list($key,$value) = each($_FILES['images']['name']))
        {
            if(!empty($value))
            {
                $filename = $ran.$value;
                    $filename=str_replace(" "," _ ",$filename);// Add _ inplace of blank space in file name, you can remove this line

				$the_dir = 'media/'.$ad_id;
				if(!file_exists($the_dir)) 
					{mkdir($the_dir, 0700);}

               $add = $the_dir."/".$ran."$filename";
            $insert_query = "MYSQL INSERT STATEMENT";
                   //echo $_FILES['images']['type'][$key];
             // echo "<br>";
                copy($_FILES['images']['tmp_name'][$key], $add);
                chmod("$add",0777);

    mysql_query($insert_query);

        }

    }

file fields are

<input type='file' name='images[]'>
<input type='file' name='images[]'>
<input type='file' name='images[]'>
<input type='file' name='images[]'>
<input type='file' name='images[]'>

    The first thing you need to do is figure what you mean by "just images." What's an image? Is it any file that has a file extension that matches some common image formats? Is it a file that contains binary data you can actually parse as an image file?

    For the former, you'd simply use something like [man]pathinfo/man to grab the file extension and perhaps something like [man]in_array/man to compare it with an array of known "good" values.

    For the latter, you might try using something like [man]getimagesize/man to see if PHP can parse the binary contents of the file as an image.

      [man]getimagesize[/man] is often able to determine if a file is an image file (and if it can, what sort of image it is).

        file extentions. I want to make sure users are only uploading png, gif and jpg images

          also, I seen tons of ways on how to do it, I just cant figure out how to incorporate any of them into my script. im not exactly a php expert I'm just tired of people uploading crap

            4 days later
            Write a Reply...