Jump to content

Basic upload code error


graham23s

Recommended Posts

Hi Guys,

 

i can't understand this basic code i have written here:

 

<?php
  // some vars for the image //
    $image_file_name = $_FILES['image']['name'];
    $image_file_size = $_FILES['image']['size'];
    $image_file_temp = $_FILES['image']['tmp_name'];
    $image_file_type = $_FILES['image']['type'];  
    
    // do an extension check before it goes to the function for resizing //
    // first check what the files extension is and lower case it //
    // this takes away 4 characters from the end of the file name //
    $file_extention = substr("$image_file_name", - 4);
    $file_extention = strtolower($file_extention);
    
    // if the file doesn't have the correct extension then reject //
    if($file_extention != ".jpg" || $file_extention != ".gif" || $file_extention != ".png")
    {
     standard_error("Error","Your image file is not one of the allowed types only <b>.jpg</b> <b>.gif</b> and <b>.png</b> are allowed to be uploaded.");  
    }
?>

 

when i test upload an image with .jpg,.gif.png it hits the error, even though the extension is valid, it looks straight forward enough but keep giving me the error, is it  asyntax issue at all?

 

thanks guys

 

Graham

Link to comment
Share on other sites

Rather than using the following to get the file extension:

    $file_extention = substr("$image_file_name", - 4);
    $file_extention = strtolower($file_extention);

Use the pathinfo function:

    $file_extention = pathinfo($image_file_name, PATHINFO_EXTENSION);

Link to comment
Share on other sites

or you can explode the file name at the "."

 

so your code would look like:

 

<?php
   // some vars for the image //
    $image_file_name = $_FILES['image']['name'];
    $image_file_size = $_FILES['image']['size'];
    $image_file_temp = $_FILES['image']['tmp_name'];
    $image_file_type = $_FILES['image']['type'];  
    
    // do an extension check before it goes to the function for resizing //
    // first check what the files extension is and lower case it //
    // This explodes the string at "."'s, then countsd how many "."s there are //
    // Then gives the file extension
    $explode = explode(".", $image_file_name);
    $count = count($explode) -1;

    // Remember and minus 1 from the count, as the var count will show one more than there should be.
    $file_extention = $explode[$count];
    
    // if the file doesn't have the correct extension then reject //
    if($file_extention != "jpg" || $file_extention != "gif" || $file_extention != "png")
    {
     standard_error("Error","Your image file is not one of the allowed types only <b>.jpg</b> <b>.gif</b> and <b>.png</b> are allowed to be uploaded.");  
    }
?>

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.