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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:04:05+00:00 2026-05-25T23:04:05+00:00

Im creating a image uploader, and i dont know how to do so only

  • 0

Im creating a image uploader, and i dont know how to do so only the filetype can be .jpg so im asking you, do you know it?

heres what i got so far:

    <?php
session_start();
if($_SESSION['username']) {
    $target_path = "users/$username/";

    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        "has been uploaded";
        header("Location: user.php");
    } else{
        echo "There was an error uploading the file, please try again!";
    }
}
    else {
            echo "Only members can be here. Register <a href='index.php'>here</a>";
  }
  ?>

And one more thing, how may i rename the uploaded file to : “profile.jpg” ?

  • 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-25T23:04:06+00:00Added an answer on May 25, 2026 at 11:04 pm

    There are some important things to consider here:

    First of all, never rely on the file extension that was provided. I could upload a php file with the extension .jpg if I wanted. Granted, I’d probably have to do some more to actually get it to execute as a php file on your server, but it certainly was not a valid image.

    If the upload was successful, $_FILES[ 'uploadedfile' ][ 'type' ] will hold the mime-type that was provided by the request. However, this should also not be trusted, as this can be tampered with as well.

    A more reliable way to determine whether the uploaded file is actually an image of type jpeg is to use the function getimagesize. getimagesize() will return false if it’s not a valid image and will return an array with information about the image, if it recognized the file as an image. Index 2 of the array will hold a value that corresponds with one of these constants that begin with IMAGETYPE_.

    $info = getimagesize( $_FILES[ 'uploadedfile' ]['tmp_name'] );
    if( $info !== false )
    {
        // file is an image to begin with
    
        if( $info[ 2 ] == IMAGETYPE_JPEG )
        {
            // file appears to be a valid jpeg image
        }
    }
    

    This is somewhat of an old school method which, as far as I know, is reliable though.

    I believe, depending on platform (Windows/*nix) and version (< 5.3, >= 5.3) there are more reliable ways to determine the actual mime-type of a file though. I’ll see what I can find for you about that later on.

    edit:
    I forgot about the renaming part.

    Simply replace this:

    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    

    … with this:

    $target_path = $target_path . 'profile.jpg';
    

    In other words, when you move the file with move_uploaded_file() the second argument will be the new path (including the new file name).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Uploadify and Kohana and I'm creating file uploader. User can upload only
I am creating an image dynamically using php. The image is being created if
I've got the following in my .css file creating a little image next to
I'm creating some image uploader in Flex with client-side image processing. Processing consists of
I am creating several image dynamically using the following code: function refresh_gallery(galleryidentifier, albumid) {
I have been creating an image gallery with jQuery, all is done. The images
I'm currently having trouble creating an image from a binary string of data in
I am creating a buffered image that is going to be a snapshot of
I am creating a basic image browsing application using Silverlight. Depending on the user's
I'm creating an dynamic image, which creates headers on my page using PHPs GD-library.

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.