Jump to content


Photo

upload image script, slight problem


  • Please log in to reply
3 replies to this topic

#1 MoFish

MoFish
  • Members
  • PipPipPip
  • Advanced Member
  • 210 posts

Posted 30 May 2006 - 12:59 AM

hello, can anyone identify why my upload code wont work? im catching the the error saying the filetype is not allowed however have tryed both jpeg and gif with no success.

PS: is this the correct way to validate the file type?

<?php
    } else {
      if ($_FILES['file']['type'] != "image/gif" AND $_FILES['file']['type'] != "image/jpg") {
        $error = "This file type is not allowed";
        unlink($_FILES['file']['tmp_name']);
        // assign error message, remove uploaded file, redisplay form.
      } else {


#2 ldsmike88

ldsmike88
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts
  • LocationFlorida

Posted 30 May 2006 - 03:33 AM

For jpeg it says image/pjpeg. It should probably say image/jpg. If that doesn't work I would say take out the image/ and just leave the jpg to see if that would work.

Most people don't like reading through someone's whole script to find their error. Next time I would recomend you just post the part where you think the error is.

Hope this helps!

#3 MoFish

MoFish
  • Members
  • PipPipPip
  • Advanced Member
  • 210 posts

Posted 30 May 2006 - 10:49 AM

thanks for the reply, I tryed the following but am still getting the error saying this file type is not allowed. :(

#4 MoFish

MoFish
  • Members
  • PipPipPip
  • Advanced Member
  • 210 posts

Posted 30 May 2006 - 11:32 AM

I also tryed the following, using an array. But this doesnt work either.

$types_array = array('image/gif','image/jpg','image/x-png');

    if(!in_array($_FILES['file']['type'], $types_array)){
        $error = "This file type is not allowed";
        unlink($_FILES['file']['tmp_name']);
    } else {

whole script below
<?php
###################################
# assign variables
####################################
$error = "";
$maxfilesize=100240;
$types_array = array('image/gif','image/pjpeg','image/x-png');

if (isset($_POST['submit'])){

###################################
# if there is a file name > proceed
####################################

  if (!is_uploaded_file($_FILES['file']['tmp_name'])) {
    $error = "You did not upload a file!";
    unlink($_FILES['file']['tmp_name']);
  } else {

####################################
# validate max file size    
####################################

    if ($_FILES['file']['size'] > $maxfilesize) {
      $error = "file is too large";
      unlink($_FILES['file']['tmp_name']);
    } else {
    
####################################
# validate file type    
####################################

    if(!in_array($_FILES['file']['type'], $types_array)){
        $error = "This file type is not allowed";
        unlink($_FILES['file']['tmp_name']);
    } else {

####################################
# finally, copy file to destination
####################################

       copy($_FILES['file']['tmp_name'],"/uploads/".$_FILES['file']['name']);
       unlink($_FILES['file']['tmp_name']);
       print "File has been successfully uploaded!";
       exit;
     }
    }
  }
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users