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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:53:01+00:00 2026-06-12T03:53:01+00:00

So i want the user to only be able to upload docs or docx’s.

  • 0

So i want the user to only be able to upload docs or docx’s. So first here’s my html:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
    Select a file: <input type="file" name="img">
    <input type="submit">
</form>

And here’s my php:

$allowedExts = array("doc", "docx");
    $extension = end(explode(".", $_FILES["file"]["name"]));

    if ($extension!=".doc" || $extension!=".doc"
    && ($_FILES["file"]["size"] < 200000)
    && in_array($extension, $allowedExts)) {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

            if (file_exists("Proposals/" . $_FILES["file"]["name"]))
            {
                echo $_FILES["file"]["name"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["file"]["tmp_name"],
                "Proposals/" . $_FILES["file"]["name"]);
                echo "Stored in: " . "Proposals/" . $_FILES["file"]["name"];
            }
        }
    } else {
        echo "Invalid file";
    }

Everytime I try to upload a file, whether it’s a doc or a png it outputs this:

Upload: 
Type: 
Size: 0 Kb
Temp file: 
already exists.

And nothing ends up getting uploaded to the Proposals folder. So I have 2 questions:
1) what is the problem with my code?
2) it redirects to upload_file.php and displays a message. Is there a way to actually go back the main page and display text that it was successful?

  • 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-12T03:53:02+00:00Added an answer on June 12, 2026 at 3:53 am

    The first question has already been answered so I won’t answer it again.

    2) it redirects to upload_file.php and displays a message. Is there a way to actually go back the main page and display text that it was successful?

    It is normally better to show the success message on the redirected page but you can solve this two ways:

    • Store the previous URL (or know it) and redirect to it with header("Location: old_page.php");
    • Make the target of the form an iframe. This means the main page itself will not redirect and you can just bubble a response from upload_file.php (which will load in the iframe) making seamless uploading for your app.

    Also this is not the best way to get a file extension:

    $extension = end(explode(".", $_FILES["file"]["name"]));
    

    Try using pathinfo instead: http://php.net/manual/en/function.pathinfo.php

    $extension = pathinfo( $_FILES["file"]["name"], PATHINFO_EXTENSION);
    

    And your if statement:

    if ($extension!=".doc" || $extension!=".doc"
        && ($_FILES["file"]["size"] < 200000)
        && in_array($extension, $allowedExts)) {
    

    Even though this is a tiny usage of in_array I still would recommend taking that out and using a more practical method of searching arrays: http://php.net/manual/en/function.array-search.php

    So your if would become something like:

    if (($_FILES["file"]["size"] < 200000) && array_search($extension, $allowedExts)!==false) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When opening a new tkinter window, I only want the user to be able
Within my JStree i want the user to only be able to select leafs
I have a form in my html page, that prompts user to upload File
I want to create a read-only user in PostgreSQL. The intention is to have
I want to allow user to enter only english alphabets and characters. How can
I want my recaptcha to appear only after user clicks submit button on the
I want to allow a user to drag my Win32 window around only inside
If I have an object say @user and I want to render only certain
I want to make user upload his picture. After he selects the picture from
I want to create a html page that can be used to upload videos

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.