Jump to content

[SOLVED] Checking for a valid filetype? Code provided


Mr Chris

Recommended Posts

Hello,

 

I have a script i;m using to insert data into a database and at the same time check for required fields.  Now the below checks for required fields and the filesize of the uploaded image:

 

<?php 
if (isset($_POST['Submit'])) 
{ 
   $errors = array(); 
   $byte = "2097152"; 
   $your_name = trim($_POST['your_name']); 
   $your_email = trim($_POST['your_email']); 
      
   // Check for normal errors in name and email
   if ($your_name === '') 
   { 
      $errors[] = "You forgot to enter your name."; 
   } 
   
   if ($your_email === '') 
   { 
      $errors[] = "You forgot to enter your email address."; 
   } 
   
   // Check  to see if the uploaded file is empty and greater than the $byte variable set above, if it is bigger or there is nothing do the below
   if(empty($_FILES['ufile'])) 
   { 
      $errors[] = "No file was uploaded."; 
   } 
   
   else if($_FILES['ufile']['size'][0] > $byte) 
   { 
      $errors[] = "Your file is too big."; 
   }

  
   // If errors is equal to zero then set the image details and insert into the database!
   if(count($errors) == 0) 
   { 
   
      // set the image details and upload the file
      $picunique = md5(uniqid());
      $picture = "/home/************/*********/images/$picunique".$_FILES['ufile']['name'][0];
      move_uploaded_file ($_FILES['ufile']['tmp_name'][0], $picture);

      // and insert into the database  
      $result = mysql_query("Insert into image_test (your_name,your_email,picture) values('$your_name','$your_email','". basename($picture) ."')") or die(mysql_error());
      $picture_id=mysql_insert_id();
      $error = "Sucess - everything uploaded";
   } 
   else 
   { 
      $error = "<span style='color:#c00'>" . implode(' ', $errors) . "</span>"; 
   } 
} 
?>
<form action="" method="post" enctype="multipart/form-data" name="myform" id="myform">
<h3>Submit your Photo</h3>
<?php echo $error;?>
  <fieldset>
    <legend>Please fill in the details below: </legend> 
  <p>  
    <label for="Story">Your Name:</label> 
    <input type="text" name="your_name" id="your_name" value="" tabindex="1" />
  </p>  
  <p>  
    <label for="Story">Your Email:</label> 
    <input type="text" name="your_email" id="your_email" value="" tabindex="2" />
  </p>
  <div style="clear:left;"></div>
  <p> 
    <label for="Main Picture">Upload Image:</label> 
<input name="ufile[]" type="file" id="ufile[]" />
  </p>
  <input name="Submit" type="submit" value="Submit" />
  <input name="Reset" type="reset" value="Reset" />  
  </fieldset>
</form>
</body>
</html>

 

However, i'd also like the system to check the file is a .jpg and i've been trying to adapt the following in the above before the if(count($errors) == 0) , but i'm not too sure how to do it.

 

  $FileType = array (".jpg", ".jpeg", ".JPG", ".JPEG");    
   if(!in_array($FileType)){ 
      $errors[] = "Sorry, but this extension type is not supported."; 
   }  
  

 

Can anyone please help?

Link to comment
Share on other sites

in_array() expects two parameters, a needle and a haystack. Try this:

<?php
if(!in_array(substr($filename,strrpos($filename,".")),$FileType)) {
    $errors[] = "Sorry, but this extension type is not supported.";
}
?>

Link to comment
Share on other sites

Sorry, might be being thick but where does $filename derive from.  Where is that variable created?

Sorry, replace $filename with $_FILES['ufile']['name'], I didn't write my example specifically for your code - It was just that, an example.

Link to comment
Share on other sites

@.Mr Chris:

this is where the filename is set.... SemiApocalyptic just used $filename instead of $picture.. $filename is the most normal to use on fileupload ;)

$picture = "/home/************/*********/images/$picunique".$_FILES['ufile']['name'][0];

 

 

@SemiApocalyptic:

Sorry for going off topic here... but i got one question that i've been wondering...

is there actually a difference between this

<?php
if(!in_array(substr($filename,strrpos($filename,".")),$FileType)) {
    $errors[] = "Sorry, but this extension type is not supported.";
}
?>

 

and this?

<?php
if(!in_array(end(explode(".",$filename)),$FileType)) {
    $errors[] = "Sorry, but this extension type is not supported.";
}
?>

Link to comment
Share on other sites

Thanks, sorry to be a pain, but have the snippett like this now:

 

// Check the extension
$FileType = array (".jpg", ".jpeg", ".JPG", ".JPEG");  
if(!in_array(substr($_FILES['ufile']['name'],strrpos($_FILES['ufile']['name'],".")),$FileType)) {
    $errors[] = "Sorry, but this extension type is not supported.";
}

 

and get the message Warning: strrpos() expects parameter 1 to be string, array given?

Link to comment
Share on other sites

@SemiApocalyptic:

Sorry for going off topic here... but i got one question that i've been wondering...

is there actually a difference between this

<?php
if(!in_array(substr($filename,strrpos($filename,".")),$FileType)) {
    $errors[] = "Sorry, but this extension type is not supported.";
}
?>

 

and this?

<?php
if(!in_array(end(explode(".",$filename)),$FileType)) {
    $errors[] = "Sorry, but this extension type is not supported.";
}
?>

 

Apart from it requiring less typing? Probably not - There's more than one way to skin a cat ;)

Link to comment
Share on other sites

Thanks, sorry to be a pain, but have the snippett like this now:

 

// Check the extension
$FileType = array (".jpg", ".jpeg", ".JPG", ".JPEG");  
if(!in_array(substr($_FILES['ufile']['name'],strrpos($_FILES['ufile']['name'],".")),$FileType)) {
    $errors[] = "Sorry, but this extension type is not supported.";
}

 

and get the message Warning: strrpos() expects parameter 1 to be string, array given

 

Replace both instances of $_FILES['ufile']['name'] with $_FILES['ufile']['name'][0]. I didn't realise you were passing it as an array - Why have you chosen to do that?

Link to comment
Share on other sites

@SemiApocalyptic:

Sorry for going off topic here... but i got one question that i've been wondering...

is there actually a difference between this

<?php
if(!in_array(substr($filename,strrpos($filename,".")),$FileType)) {
    $errors[] = "Sorry, but this extension type is not supported.";
}
?>

 

and this?

<?php
if(!in_array(end(explode(".",$filename)),$FileType)) {
    $errors[] = "Sorry, but this extension type is not supported.";
}
?>

 

Apart from it requiring less typing? Probably not - There's more than one way to skin a cat ;)

 

hehe that's what i thought :P i love less typing :D

 

-----

Back on topic... I belive he's editing a tutorial or premade script or something... am I right Chris?

Link to comment
Share on other sites

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.