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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:16:58+00:00 2026-05-24T11:16:58+00:00

I am using this snippet below to get the filename and extension, which works,

  • 0

I am using this snippet below to get the filename and extension, which works, but how can i rename the file so that filename will equal bake23 + the extension of the file.

  $variable = 'baKe23';


  $filename = $_FILES['filename']['name'];
  $ext = $_FILES["filename"]["type"];

Rename if a filename already exists:

       $filename = $_FILES['filename']['name']; 
  $extension = substr($filename, strpos($filename,'.'), 
  strlen($filename)-1);

         $sql="SELECT filename FROM documents WHERE person_id= '$pid'";  

     $result=mysql_query($sql);

    $query = mysql_query($sql) or die ("Error: ".mysql_error());

    if ($result == "")
    {
    echo "";
     }
    echo "";


   $rows = mysql_num_rows($result);

   if($rows == 0)
   {
  print("");

   }
   elseif($rows > 0)
   {
  while($row = mysql_fetch_array($query))
   {

  $existing = $row['filename'];

  print("");
   }

    }

    if ( $filename === $existing ) {
$filename = $rnd_id.strrchr($_FILES['filename']['name'], ".");

   } else {
   $filename = $_FILES['filename']['name'];;
    }
  • 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-24T11:16:59+00:00Added an answer on May 24, 2026 at 11:16 am

    Get the extension from the submitted filename and add it to your variable:

    $filename = basename($filename);
    $filename = $variable . (string) strrchr($filename, '.');
    

    Edit: For your edited question, literally:

    $filename =  $variable . '.' . $ext;
    

    However, considering I would be uploading an image called banana-smiley.gif, the following would happen:

    $filename = 'baKe23.image/gif';
    

    This is not what you want I assume. You should not even rely on $_FILES[*]["type"] at all. What you need to do is inspect each uploaded file (determine the type, validate the type, run a virus checker on the uploaded file).

    Question related to this are Secure File Upload and validating it and Is it important to verify that the uploaded file is an actual image file?.

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

Sidebar

Related Questions

Using the snippet below throws me a null pointer exception, but i can't find
I have this certain method(snippet below) for which I want to get the XML
Currently I'm using the below snippet, which indent the resulting HTML by using several
In the snippet of code below, I'm using a class to get a reference
I have a small snippet of code below that I'm running using PellesC .
How to send triimed data using $.get() see code snippet below: $(#txtSearch).keyup(function() { $.get(/controller/method/,
I am using this code snippet to add KeyDown event handler to any element
I'm using this jQuery snippet to create tooltips on some JSF primefaces elements in
I currently am using this JavaScript code snippet to select 3 checkboxes at a
how i do validate two date textboxes using jquery. i came across this snippet

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.