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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:23:57+00:00 2026-06-14T12:23:57+00:00

I need help with adding the feature to check whether a file exists when

  • 0

I need help with adding the feature to check whether a file exists when uploading.

This is how the upload.php code looks like for uploading:

$file_name = $HTTP_POST_FILES['ljudfil']['name'];
$random_digit=rand(0000,9999);
$mp3 ='.mp3';
$pdf ='.pdf';
$datum = date('Ymd'); 
$new_file_name=$random_digit.$file_name;
$target_path1 = $target_path . $orgnr . '_' . $gsm . $pdf; 

$target_path3 = $target_path . 'AC' . $datum . $new_file_name . $mp3; 
$target_path11 = $target_path4 . $orgnr . '_' . $gsm . $pdf; 

$target_path33 = $target_path4 . 'AC' . $datum . $new_file_name . $mp3;
$targetljudfilftp = 'AC' . $datum . $new_file_name . $mp3;

move_uploaded_file($_FILES['avtalsfil1']['tmp_name'], $target_path1);
move_uploaded_file($_FILES["ljudfil"]["tmp_name"], $target_path3);
$sql = "INSERT INTO affarer (tid, cid, orgnr, ljudfilftp) VALUES 
        (CURDATE(),'$date','$cid','$orgnr', '$targetljudfilftp')";

As you can see, it renames the uploaded file including a random number.

Sometimes, it happens that it renames the file to a number that already exists.

When that happens, it overwrites the previous file on my server.

So, how can I add a function to check whether the target name exists before it is used for renaming?

  • 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-14T12:23:58+00:00Added an answer on June 14, 2026 at 12:23 pm

    You can use

    if (file_exists($target_path1))
    

    to verify whether a file exists.

    You would do better, though, to change strategy and employ tempnam:

    $target_path  = tempnam ($target_path, 'AC' . $datum . $file_name . $mp3)
    

    This will create a file such as “AC_2012_Anacreon.mp3_xTfKxy” but you have the guarantee of it being unique, while even using file_exists would expose you to the risk of a concurrency collision.

    Of course the file no longer has a .mp3 extension, so you have to take it into account when you scan the directory and supply files for download.

    A still not secure, but maybe easier way is this:

    for(;;)
    {
        $newname = // some strategy to generate newname, including a random
        if (!file_exists($newname))
            touch($newname);
        if (!filesize($newname))
            break;
    }
    

    or you can use a lock file to guarantee no concurrency (and therefore, that file_exists will return the truth and it will stay the truth):

    $fp = fopen('.flock', 'r+');
    if (flock($fp, LOCK_EX))
    {
        for(;;)
        {
            $newname = // some strategy to generate newname, including a random
            if (!file_exists($newname))
            {
                // this creates the file uniquely for us.
                // all other writers will find the file already there
                touch($newname);
            }
        }    
        flock($fp, LOCK_UN);
    }
    else
        die("Locking error");
    fclose($fp);
    // $newname is now useable.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help adding conditions to a linq query. Take this small example
I need help with my code: I am dynamically adding elements, creating new elements
Hi I need help adding a new CLAUSE, this one is pretty advanced for
I really need help with this one. I have written an adding that adds
I need help in adding an element at a particular location in the page
I need a little help clearing something up with Zend_Form and adding filters to
Need help writing a script downloads data from google insight using c# this is
I'm new to iPhone dev and need some help with adding subViews. I have
I need help with adding to a database. I want to call a javascript
I need help with searchBar in landscape mode. I am adding it to the

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.