Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3334188
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:52:39+00:00 2026-05-17T23:52:39+00:00

I have been writing a file uploader, and I want to validate the types

  • 0

I have been writing a file uploader, and I want to validate the types and size, and I currently have this:

    <?php 

//SETTING UP LOCAL VARIABLES
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);

//STRIPPED OUT ALL NON ASCII CHARACTERS
$username = preg_replace('/[^(\x20-\x7F)]*/','', $username);
$password = preg_replace('/[^(\x20-\x7F)]*/','', $password);
$name = preg_replace('/[^(\x20-\x7F)]*/','', $name);
//$email = preg_replace('/[^(\x20-\x7F)]*/','', $email);

//SETTING UPLOAD DIR
$upload_dir = $_SERVER['DOCUMENT_ROOT'] . "/beta_images/";

//CREATING A RANDOM HASH TO PROTECT FROM DUPLICATE FILES
$random = rand(1, 100);

$user_hash = $random . $name;

$hash = sha1($user_hash);
$hash = substr($hash, 32, $random);

foreach ($_FILES['file']['error'] as $key => $error) {

$counter++;

if (
(
    ($_FILES['file']['type'][$key] == "image/jpeg")
 || ($_FILES['file']['type'][$key] == "image/pjpeg")
 || ($file['file']['type'][$key] == "image/jpg")
)

&& ($error == UPLOAD_ERR_OK)
&& ($_FILES['file']['size'][$key] < 20971520)
) {

        $temp_name = $_FILES['file']['tmp_name'][$key];
        $image_name = $hash . '_' . $name . $counter . '.jpg';
        move_uploaded_file($temp_name, $upload_dir . $image_name);

    } else {

    $body_fail = "Someone forgot to do something and wound up on the Sorry page. You might want to contact them and try and convince them to join still. Here are the details \n\n Username: $username \n Password: $password \n Email: $email \n Name: $name \n Message: $message";

    mail("bla", "Failed beta sign up", $body_fail);

    header("Location: ../sorry.html");

    }

}

        //EMAIL INTERNAL

            $body_internal = "Success! $name has submitted a beta entry!\n\n Their username is \n $username \n Their password is \n $password \n Their email address is $email.\n\n The images are located in \n /beta_images/{$hash}_{$name}1/2/3.jpg \n\n They also wrote a little message for us: \n$message";

            mail("bla", "New Beta Sign Up", $body_internal);

        //EMAIL INTERNAL

        //EMAIL EXTERNAL

            $body_external = "Thank you for applying to join the Stylistic Gallery, we just wanted to let you know that we have received everything and will be in touch shortly.\n\n Best Wishes \n Stylistic Gallery \n\n The Stylistic Gallery, a portal for creative individuals and businesses to showcase and sell their work online";

            mail($email, "Thank you for your application", $body_external);

        //EMAIL EXTERNAL

        header("Location: ../thanks.html");

However it is still going through even if the files are not jpg or the size is bigger.

Sorry for being vague, so confused from looking at it too long at the moment 🙂

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:52:40+00:00Added an answer on May 17, 2026 at 11:52 pm

    You’re missing some parantheses and you want to replace one || by &&.

    if (
    (
        ($_FILES['file']['type'] == "image/jpeg")
     || ($_FILES['file']['type'] == "image/pjpeg")
     || ($_FILES['file']['type'] == "image/jpg")
    )
    
    && ($error == UPLOAD_ERR_OK)
    && ($_FILES['file']['size'] < 20971520)
    ) {
    

    If you’ve got long if statements like this it’s better to break it down. Here’s how I would code your loop:

    /* ideally you would move those messages to some text files, to make it
     * easy to change any content and eventually localize it.
     */
    
    // mail if upload fails
    $failure_mail_body_to_admin = "Someone ...";
    
    // mail to admin after successful upload
    $sucess_mail_body_to_admin = "Success! ...";
    
    // mail to user after successful upload
    $sucess_mail_body_to_user = "Thank you ...";
    
    // mime types of allowed images, I know that those can be spoofed
    $allowed_mime_types = array("image/jpeg", "image/pjpeg", "image/jpg");
    $fileCount = 0;
    
    foreach ($_FILES as $filename => $file) {
        $file_is_ok = true;
        // test general errors
        if ($file['error'] != UPLOAD_ERR_OK) {
             $file_is_ok = false;
        }
    
        // test size (< 20MB)
        if ($file['size'] >= 20971520) {
            $file_is_ok = false;
        }
    
        if (!in_array($file['type'], $allowed_mime_types) {
            $file_is_ok = false;
        }
    
        if ($file_is_ok) {
            $fileCount++;
    
            // store image
            $temp_name = $file['tmp_name'];
            $image_name = $hash . '_' . $name . $counter . '.jpg';
            move_uploaded_file($temp_name, $upload_dir . $image_name);
        }
    }
    
    if ($fileCount > 0) {
        // send confirmation mails
        mail("bla", "New Beta Sign Up", $sucess_mail_body_to_user);
        mail($email, "Thank you for your application", $sucess_mail_body_to_admin);
    
        // redirect user
        header("Location: ../thanks.html");
        die();
    } else {
        mail("bla", "Failed beta sign up", $failure_mail_body_to_admin);
    
        // redirect user
        header("Location: ../sorry.html");
        die;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have been writing to Environment.SpecialFolder.ApplicationData this data file, that upon uninstall needs
I have been writing web applications for quite sometime in PHP with MySQL. I
I have been writing in PHP for just over half a year now, and
I have been writing an implementation for reading a .csv data file into C#
I'm relatively new to PHP and have been writing a project using what I
I have been learning some Haskell and writing very simple programs. I want to
I have been writing this encryption algorithm in my free time for a few
I have been writing an c s-function block for simulink.I want my output as
I have been writing php for quite some time and never really thought about
I have been working on a XSLT file here. I had been writing CSS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.