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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:36:39+00:00 2026-06-03T07:36:39+00:00

So I have an HTML form where a user can upload an image. This

  • 0

So I have an HTML form where a user can upload an image. This posts to my action, image gets put in a directory unique to the user’s id (and creates that directory if it doesn’t already exist).

This has been working just fine since I implemented it about two months ago, and I haven’t had to touch it since. Initially I was getting UnauthorizedAccessExceptions but solved that by going into the security settings of my uploads directory and granting full control to ‘Everyone.’

Well out of the blue, I can no longer save images anymore. I started getting the UnauthorizedAccessExceptions again, despite changing the folder’s security settings again and again to try to fix it. Finally read about doing account impersonation in ASP.net.

So, now I am not getting the UnauthorizedAccessExceptions and am instead running into the issue of my site thinking the directory already exists.

Here is a snippet from my form action:

if (Directory.Exists(Path.GetDirectoryName(Server.MapPath("../uploads/" + currentUser.UserId))) == false)
{
   Directory.CreateDirectory(Path.GetDirectoryName("../uploads/" + currentUser.UserId));
}
if (Directory.Exists(Path.GetDirectoryName(Server.MapPath("../uploads/" + currentUser.UserId + "/" + newEntry.EntryId))) == false)
{
   Directory.CreateDirectory(Path.GetDirectoryName("../uploads/" + currentUser.UserId + "/" + newEntry.EntryId));
}

ImageFile.SaveAs(Server.MapPath("../" + savedFileName));

The code within the if statements never executes. So, I removed them and simply tried:

Directory.CreateDirectory(Path.GetDirectoryName("../uploads/" + currentUser.UserId));

Nothing happens! Then when it’s time to save the image, I get an exception thrown because the directory doesn’t exist! Can anyone tell me why Directory.Exists() is returning true, or why Directory.CreateDirectory() is doing absolutely nothing?

Or why this code has been untouched and working fine for months and suddenly decided to throw a fit?

Note: if I go to the folder and create the directories manually, the file saves just fine

  • 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-03T07:36:40+00:00Added an answer on June 3, 2026 at 7:36 am

    Path.GetDirectoryName just returns the directory name not the fully qualified path of the directory. Directory.Exists will take a fully qualified path of the directory and then check whether the directory exists or not.

    Your condition will fail if as Directory.Exists will check in the root of the web application whether the folder exists or not instead of looking inside the uploads folder.

    Your condition should be like this:

    if (Directory.Exists(Server.MapPath("/uploads/" + currentUser.UserId)) == false)
    {
        Directory.CreateDirectory(Server.MapPath("/uploads/" + currentUser.UserId));
    }
    

    NOTE: Removed the leading “..” from the path and it worked!!

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

Sidebar

Related Questions

I have an HTML form where the user can type in any number:5, 8,
I have the following multi-select box in a HTML form, where user can select
I'm trying to have the action of a HTML form built when the user
I have a form where users can upload an image. I have another page
I have a form where a user can upload a file to the sites
I have a HTML form which allows a user to add rows ad hoc
I have an HTML page with a simple form. When the user clicks submit,
I am using a HTML form to upload image files. Now i am using
I'm building an HTML form with which the user should be able to upload
i have a form which allows the user to upload some files to a

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.