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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:07:22+00:00 2026-05-21T20:07:22+00:00

On my site I let users upload files. If the file is valid and

  • 0

On my site I let users upload files.

If the file is valid and uploaded it is moved to a folder (using PHP).

All users upload to the same folder.

I think I need to rename the uploaded files.

Is there something like a default naming convention to let users upload files with the same filename?

  • 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-21T20:07:23+00:00Added an answer on May 21, 2026 at 8:07 pm

    There are no standard conventions, but there a couple of best-practices:


    Organizing your files into (User and/or Date) Aware Folders

    Something like:

    /uploads/USER/ or
    /uploads/[USER/]YEAR/[MONTH/[DAY/[HOUR/[MINUTE/]]]]
    

    This will have some benefits:

    • organize files per user and/or date
    • make it harder to reach the maximum number of files per directory

    (Not) Renaming / Sanitizing Filenames

    Renaming or not is a choice you will have to make, depending on your website, user base, how obscure you would like to be and, obviously your architecture. Would you prefer to have a file named kate_at_the_beach.jpg or 1304357611.jpg? This is really up to you to decide, but search engines (obviouslly) like the first one better.

    One thing you should do is always sanitize and normalize the filenames, personally I would only allow the following chars: 0-9, a-z, A-Z, _, -, . – if you choose this sanitation alphabet. normalization basically means just converting the filename to either lower or upper case (to avoid losing files if for instance you switch from a case sensitive file-system to a case insensitive one, like Windows).

    Here is some sample code I use in phunction (shameless plug, I know 😛):

    $filename = '/etc/hosts/@Álix Ãxel likes - beer?!.jpg';
    $filename = Slug($filename, '_', '.'); // etc_hosts_alix_axel_likes_beer.jpg
    
    function Slug($string, $slug = '-', $extra = null)
    {
        return strtolower(trim(preg_replace('~[^0-9a-z' . preg_quote($extra, '~') . ']+~i', $slug, Unaccent($string)), $slug));
    }
    
    function Unaccent($string) // normalizes (romanization) accented chars
    {
        if (strpos($string = htmlentities($string, ENT_QUOTES, 'UTF-8'), '&') !== false)
        {
            $string = html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~i', '$1', $string), ENT_QUOTES, 'UTF-8');
        }
    
        return $string;
    }
    

    Handling Duplicate Filenames

    As the documentation entry on move_uploaded_file() states:

    If the destination file already
    exists, it will be overwritten.

    So, before you call move_uploaded_file() you better check if the file already exists, if it does then you should (if you don’t want to lose your older file) rename your new file, usually appending a time / random / unique token before the file extension, doing something like this:

    if (file_exists($output . $filename) === true)
    {
        $token = '_' . time(); // see below
        $filename = substr_replace($filename, $token, strrpos($filename, '.'), 0);
    }
    
    move_uploaded_file($_FILES[$input]['tmp_name'], $output . $filename);
    

    This will have the effect of inserting the $token before the file extension, like I stated above. As for the choice of the $token value you have several options:

    • time() – ensures uniqueness every second but sucks handling duplicate files
    • random – not a very good idea, since it doesn’t ensure uniqueness and doesn’t handle duplicates
    • unique – using an hash of the file contents is my favorite approach, since it guarantees content uniqueness and saves you HD space since you’ll only have at most 2 identical files (one with the original filename and another one with the hash appended), sample code:

    (Dummy text so that the next line gets formatted as code.)

    $token = '_' . md5_file($_FILES[$input]['tmp_name']);
    

    Hope it helps! 😉

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

Sidebar

Related Questions

i'm running a php/mysql based website. I already let my users upload p7m files
here is a site http://www.lyrkjsw.gov.cn that can let the registered user to upload file
I need to let users upload videos in GWT site, and after uploading I
I am thinking to let the users upload a css file and control the
I have a web site which handles files management. User can upload file, add
I'm developing a Wordpress site that will let (registered) users upload an image and
So I'm working on this site web app that should let users easily chat
Let's say I have a site where Users can add Entries through admin panel.
Users on my site can add nodes of a custom type (let's call it
Let us suppose we have a site entirely written on Silverlight. All navigation occurs

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.