Solved

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

Posted on 2012-03-21
1
290 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
1 Comment
 
LVL 108

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now