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

  • Home
  • SEARCH
  • 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 6220185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:54:52+00:00 2026-05-24T07:54:52+00:00

I have an upload PHP script and before it uploads the file, it renames

  • 0

I have an upload PHP script and before it uploads the file, it renames it to to a random name, so no files that are already there will be overrided. The problem is that it is not working properly. It is putting in a bunch of weird symbols in the file name, and when I try viewing the file, it has a 404 error. It was working before I added this random name part in. Here’s the PHP script that I have right now:

<?php
// Where the file is going to be placed 
$target_path = "files/";

/* Rename file with random name and keep extension */
    $length = 10;
    $characters = ’123456789’;
    $string="";
    for($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
$pos = strrpos(basename( $_FILES['uploadedfile']['name']), ".");
$ext = str_split(basename( $_FILES['uploadedfile']['name']), $pos);
$target_path = $target_path.$string.$ext[1]; 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "<h2>Your file has been uploaded!</h2><br /><p>Your file has been uploaded successfully. <a href='".$target_path."'>Click here</a> to see the file you uploaded.
    <br /><br /><br />
    <h2>A link to your file</h2><br />The full link to your file is:
    <br /><br />
    <code>http://www.example.com/upload/".$target_path."</code></p>";
} else{
    echo "<span class='error'>There was an error uploading the file, please try again.</span>";
}
?>

For some reason, it is really acting weird. Then, when I view the folder /files/ in my hosting file manager and I try deleting a file with the weird symbols in it, and I refresh and they are all back. Take a look at this screenshot:

enter image description here

Weird, huh?

I will probably need a new random naming PHP script, but this is annoying because I delete all of those files, and, what do you know, they come right back when I reload! It’s like the PHP is forcing them to not delete or something.

Thanks for any help in advance,
Nathan

  • 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-24T07:54:53+00:00Added an answer on May 24, 2026 at 7:54 am

    You put forwardticks around your string of characters instead of quotes: ’123456789’. Changing those to quotes will probably fix your error.

    But You should be using tempnam(). A PHP function which generates a random and unique file name, creates a file and returns the file name.

    You just specify the folder you want the file in, and a prefix for the filename (only the first three characters are used on Windows), and it does all the “random” for you while guaranteeing uniqueness, so you generating the same random number twice and overwrite a file.

    PS. Despite the functions name, the file is not temporary. If you want to delete it you have to do so yourself. You can use unlink() for that.

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

Sidebar

Related Questions

I have a PHP script that processes file uploads. The script tries to organise
I have a question. I have a script that handles file upload, and after
I have a PHP script that receives uploads and it works fine in mamp,
I have a file upload form that is being posted back to a servlet
I have a form that excepts a file upload in ASP.NET. I need to
I have a bash script to upload some files to my webserver. The problem
i have this script but i want to rename the file: <?php $fieldname =
I have a script I'm working out to upload up to 10 files at
I have a scenario where I need to upload a file from one web
I have implemented a simple file upload-download mechanism. When a user clicks a file

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.