How to Rename a File Using PHP

PHP



In this article, we show how to rename a file using PHP.

Renaming files may be necessary for many reasons. Files may require a name change. There may have been spelling errors in an existing file or files that you want to change. Your website may have changed; and, thus, this may require renaming files.

Renaming a file means that you take an exisitng file and change the name to something other than it is. For example, you may change the file NY.txt to newyork.txt.

So how is this done with PHP?

PHP has a rename() function that allows us to make a rename a file.

So the PHP code to rename a file is shown below.




So the code above is very simple. It's a single line.

The PHP rename() function takes in 2 parameters.

The first parameter is the original file that we want to rename. In this parameter, we must specify the file name along with its file extension. Do not leave off the file extension, or else the function will not work.

The second parameter represents the new renamed file. Again, we must specify the file name along with its file extension. In the second parameter, you specify what you want the name of the renamed file. This renamed file contains all the contents in it that the original file has; the only difference is the file name is different.

So this is all that is required to rename a file.

Even though the code is above is very effective and does the job of renaming a file, many times we would want to add more to it, so that we can get more information. For example, does the file we want to rename exist? If the file does exist, was it successfully renamed?

The PHP code below renames a file and gives us more information.



This code now is more forthwise. It tells us more.

We first see whether the file exists. If the file exists, the code advances to the next line. In this line, we create a variable named $renamed. This variable holds the result of the PHP rename function. If the file has been renamed successfully, the $renamed variable will hold a 1 or TRUE value. If it hasn't, the $renamed variable will hold a 0 or FALSE value. If the file has been renamed successfully, the line "The file has been successfully renamed" is output. If not, the line "The file has not been successfully renamed" is output.

If the file doesn't exist, the line "The original file that you want to rename doesn't exist" is output.

So this code above tells us everything. It tells us whether the file we want to rename exists. And it tells us whether the file rename was successful or not.

And this is all that is required to rename a file using PHP.


How to Rename All Files in a Directory with a Certain Criterion or Criteria

The above code is great and works well. But the true beauty of this PHP rename() function, if you want to tap into the true beauty of it, is that it can rename all files in a directory matching a certain criterion.

Say, you have a list of files that have the word, "MD" standing for Maryland, such as "flowers-in-MD", "gifts-to-buy-in-MD", "places-to-go-in-MD" and you've decided that you don't want to use the state abbreviation; instead you want to use the full state name, "Maryland".

Imagine if you have 70 of these files. Imagine if you have hundreds. You would have to go through each of these files and manually rename them.

This is the beauty of the PHP rename() function.

With a single block of code, we can rename all of those files in less than a minute, instead of it taking hours or days with us manually doing it.

So how do you loop through all the files in a directory and rename the files we want to?



So this code above searches the Articles directory. Of course you can set this to any direcory you want on your website.

The PHP code checks to see if this is a directory. If so, it opens the directory. The while loop loops through all the files in the directory.

We then write an if statement to see whether each file contains the phrase "MD". If it does, we then create another variable, $file2, which is the same string as the original file, except that 'MD' is replaced with 'Maryland' through the PHP str_replace() function. We then use the PHP rename() function to rename the original file to the now renamed filed. Lastly, we echo that the original file has now been renamed to the renamed file.

This is the beauty of programming and the PHP rename() function.


Related Resources

How to Read the Contents of a File Using PHP

How to Write Contents to a File Using PHP

How to Copy a File Using PHP

How to Delete a File Using PHP

How to List All Files in a Directory using PHP





HTML Comment Box is loading comments...