File Handling Deleting and renaming Files in php. Before going to start read File handling writing to the file. In this tutorials we will see deleting and renaming the file which we had uploaded to the server by php functions or by php scripts. We will go through couple of functions deleting files and rename files. we had a specific function to delete file and had function that rename the file to a file name of our choice. Why may you need to delete or renaming the files. For example you may want your user to delete file which had been upload by providing link to it and php function you can delete particular file. Same way you want user to rename the file by there choice you provide such task by php functions.
As you can see in screenshot1 we had 2 files filetodelete.txt is the file which we will delete in examples and filetorename so keep eye on it. File.php is file we performing task or writing php scripts for file handling deleting files and renaming files.
As you can see in screenshot1 we had 2 files filetodelete.txt is the file which we will delete in examples and filetorename so keep eye on it. File.php is file we performing task or writing php scripts for file handling deleting files and renaming files.
Code For File Handling deleting files (Screenshot2)
Screenshot1 |
Screenshot2 |
Output (Screenshot 3)
Screenshot3 |
For File handling deleting files we had php function called unlink functions which will delete the file by php scripts. In unlink function we have to provide argument the name of the file which we want to delete. In this example we had if condition in php which we file provide value of true and false. If file is on the server it will be deleted by unlink function of php otherwise if the value is false it will show the error message file cannot be deleted or file cannot find as you wish. We are saving name of the file in variables in php such as $filename = 'filetodelete.txt'; .
Screenshot4 |
Screenshot5 |
You can see output screenshot3 File had been deleted from the directory You can see in Screenshot4. If We run the code again you can see in screenshot5 we will have warning message unlink(filetodelete.txt) [function.unlink]: No such file or directory in C:\xampp\htdocs\series\filehandling\file.php on line 5. Also it echo out else part File cannot be deleted.
Renaming Files In Php
If we want to rename File By php Script. If we want to generate random number for file name we can use rand function. Here we will rename filetorename.txt to any random generated number in Php Programming Language.
Code for Renaming File In Php Programming Language.
Screenshot6 |
<?php $filename = 'filetorename.txt'; $rand = rand(10000,99999); if (rename($filename, $rand.'.txt')) { echo 'File <strong>'.$filename.'</strong> has been renamed to <strong>'.$rand.'txt </strong>'; } else { echo 'Error Renaming.'; } ?>
Output
Screenshot7 |
Screenshot8 |
We are generating random number to file by renaming file name by php script. Firstly we declaring filetorename.txt to $filename. Also making rand function we make function with rand () It takes lower and upper limit Here our lower limit is 10000, and our upper limit is 99999. It will generate five numbers. To rename the file we had function rename() function it will take two parameters File which we want to rename and the name we want to be to file by renaming, So, Here example piece of code is
rename($filename, $rand'.txt');
Here we are appending file extension to .txt If we will not define type it will be an unknown file type. We will use if else statement to check whether file had been renamed or not.
As you can see output screenshot 7 and Screenshot 8.
In This tutorials we had seen Unlink function and rename function. Unlink function in php is used to delete the file from the particular directory. Rename function is used to rename the particular file from directory.
No comments:
Post a Comment
Thanks For Comment Will get you Soon..