
In other words, this is where we want to move it to.

This is the path and filename that we want our new file to have.

Move the file using PHP's rename function. $newFilePath = 'directory_b/sample-file.txt' $currentFilePath = 'directory_a/sample-file.txt' In this case, we have a txt file called sample-file.txt The file path of the file that we want to move. Using PHP, we are going to move sample-file.txt from directory_a to directory_b. In directory_a, we have a text file called sample-file.txt. By renaming the path to the file, we are essentially moving it.įor this tutorial, we will create two directories called directory_a and directory_b. As the name suggests, this function renames a given file or directory.ĭon’t let the name of the function confuse you. To do this, we will be using PHP’s native rename function.
Php file renamer how to#
pdf, use: rename -v 's/.txt/.pdf/' *.In this tutorial, we will show you how to move a file using PHP. Returning to our last example, to change the file extension from. -v: Shows a verbose version of the output.-s: Renames the target instead of the symlink.Best combined with the verbose output ( -v). -n: Performs a dry run, making no permanent changes.-l: Replaces the last occurrence of the filename element instead of the first one.-i: Displays a prompt before overwriting existing files.-f: Forces an overwrite of existing files.-a: Replaces all the occurrences of the filename element instead of just the first one.The rename command uses the following options: In this example, every a character in the filename is replaced by an x, every b by a y, and every c by a z. This type of expression uses the following syntax: rename 'y///' Īn example of a rename command using a translate expression: rename 'y/abc/xyz/' : Defines the file you want to rename.Ī translate expression translates one string of characters into another, character for character.: Specifies a replacement for the part of the current filename.: Specifies the part of the filename you want to replace.: Provides an optional argument that changes the way the command executes.With this syntax, the command renames the file by replacing the first occurrence of the filename element with the replacement. They use the following syntax: rename 's///' Substitute expressions replace a part of the filename with a different string. The rename command uses substitute and translate expressions to change file and directory names. There are three types of Perl regular expressions: match, substitute and translate. For Arch Linux, use sudo pacman -S rename.
Php file renamer install#

On many Linux distributions, the rename command is not available by default. It offers more features than the mv command but can be more challenging to use since it requires basic knowledge of Perl expressions. The rename command is used to rename multiple files or directories in Linux. Note: Learn how to compare two files using the diff command.
