Solved

PHP: Make file name original by adding an underscore and a number

Posted on 2012-03-21
1
299 Views
Last Modified: 2012-08-13
Using PHP, how can I automatically make a file name original by adding an underscore and a number to the end?

If you run the script 12 times, the files saved should be named:
hello.txt
hello_1.txt
hello_2.txt
hello_3.txt
hello_4.txt
hello_5.txt
hello_6.txt
hello_7.txt
hello_8.txt
hello_9.txt
hello_10.txt
hello_11.txt



<?php

file_put_contents('hello.txt','hello');

function original_name ($str){
 while(file_exists($str)) {
  // Add underscore and number after name before extention
 }
 return $str;
}

?>
0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37750248
Explode the basename using '.' as the separator.  You will get an array with the name in position zero and the extension in position one.

Increment your counter.

Implode the array using underscore, your counter, and the '.' as the glue.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question