Jump to content

Recommended Posts

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?

[code]
<?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 {
[/code]
Link to comment
https://forums.phpfreaks.com/topic/10742-upload-image-script-slight-problem/
Share on other sites

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!
I also tryed the following, using an array. But this doesnt work either.

[code]
$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 {
[/code]

whole script below
[code]
<?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;
     }
    }
  }
}
?>
[/code]
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.