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 8598325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:07:33+00:00 2026-06-12T01:07:33+00:00

I want to upload images to my server from browser window. However, the upload

  • 0

I want to upload images to my server from browser window. However, the upload field will be visible for everyone, so I need to set up some restrictions. I’ve only found the w3schools file upload (and as of w3fools.com I don’t trust it). I want the restrictions to be:

Maximum size 2,5M

Image types jpg, jpeg, png, gif

So here’s the code that w3schools provides, but it won’t actually save the file anywhere? I’ve modified it a bit to meet my needs.

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/jpeg"))
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2500000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }
?>

And as I don’t want my site to be hacked, I want a secure solution, any help on this?

Edit

The code doesn’t even do anything. So how should I do it?

  • 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-06-12T01:07:34+00:00Added an answer on June 12, 2026 at 1:07 am

    You need to use php move_upload_file function and also I have made changes to your if statement here is the working and tested example:

    <?php
    
    if (isset($_REQUEST["submit"])) {
    
        $allowedExts = array("jpg", "jpeg", "gif", "png");
        $extension = end(explode(".", $_FILES["file"]["name"]));
    
        if ($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/png" && $_FILES["file"]["size"] < 2500000 && in_array($extension, $allowedExts)) {
    
          if ($_FILES["file"]["error"] > 0) {
    
            echo "Error: " . $_FILES["file"]["error"] . "<br />";
    
          }
          else {
    
            $fname = $_FILES["file"]["name"];
            move_uploaded_file($_FILES["file"]["tmp_name"], $fname);
    
            echo "Upload: " . $_FILES["file"]["name"] . "<br />";
            echo "Type: " . $_FILES["file"]["type"] . "<br />";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
            echo "Stored in: " . $fname;
    
          }
    
        }
        else {
    
          echo "Invalid file type";
    
        }
    
    }
    ?>
    <form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" name="submit" value="submit" />
    </form>
    

    You can also use getimagesize function as suggested by doing next thing:

    $size = getimagesize("http://www.simplestudio.rs/060620121945.jpg");
    
    $file_format = $size['mime'];
    

    $file_format will be represented as for example “image/jpeg” so you can easily check for image types like this:

    foreach($allowedExts as $allowed) {
    
    $chk_types = strpos($file_format, $allowed);
    
    if($chk_types > -1) {
    $type_is_good = true;
    break;
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to upload user images from client machine to server in php. The
I want to upload images from the client to the server. I already managed
I want to upload images from a phone device into a computer web-server using
In my application i want to upload the images to server. I am able
hai guys, I want to upload images through my web page to a folder
I'm trying to put together an image uploader. I want to upload images into
I want to let users upload Images. The file gets re-named (by adding the
I want to upload data, images, txt files, pdf documents , etc. Im using
I need to know it to correctly get filedata from POST dictionary. I want
I'm building a queue-based system to scale user-uploaded images. Users will upload images which

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.