PHP தமிழில் பகுதி 15: அடைவுகளுடன் பணியாற்றுதல் (Working with Directories)

கோப்புகளைப் கையாளுவது எப்படி? என்று முந்தைய பகுதியில் பார்த்தோம். இந்த பகுதியில் PHPயில் அடைவுகளை கையாளுவது எப்படி? என்று பார்ப்போம். புதிதாக ஒரு அடைவை உருவாக்குதல், ஏற்கனவே இருக்கும் ஒரு அடைவை நீக்குதல், அடைவுகளுக்குள் இருக்கும் கோப்புகளை பார்வையிடுதல் என நிறைய function கள் PHP யில் இருக்கின்றன.
புதிதாக அடைவுகளை உருவாக்குதல் (Creating Directories)
mkdir() function ஐப் பயன்படுத்தி நாம் புதிதாக ஒரு அடைவை உருவாக்கலாம். தற்போது இருக்கும் அடைவுக்குள்ளே புதிதாக ஒரு அடைவை உருவாக்க வேண்டுமானால் நேரடியாக புதிய அடைவை பெயரை mkdir() function க்கு உள்ளீடாக கொடுத்துவிடலாம். வேறொரு அடைவிற்குள் புதிதாக ஒரு அடைவை உருவாக்க வேண்டுமானால் எங்கு புதிய அடைவு உருவாக்கப்பட வேண்டுமோ அதனுடைய முழு பாதையையும் (full path) கொடுக்க வேண்டும்.
நீங்கள் விரும்பினால் அடைவிற்கான அனுமதியையும் இரண்டாவது உள்ளீடாக கொடுக்கலாம்.
கீழே உள்ள நிரலைப் பாருங்கள்
<?php
//create a new directory using PHP
$yourDirectoryName = “/tmp/phpintamil”;
if ( mkdir($yourDirectoryName) ) {
echo “$yourDirectoryName is successfully created.<br>”;
}
else {
echo “Directory creation failed.<br>”;
}
?>
வெளியீடு
அடைவை நீக்குதல் (Deleting Directory)
rmdir() function ஐ பயன்படுத்தி அடைவுகள் அழிக்கப்படுகின்றது. எந்த அடைவை நாம் அழிக்க வேண்டுமோ அந்த அடைவின் பெயரை உள்ளீடாக கொடுக்க வேண்டும். அடைவு காலியாக இருந்தால் மட்டுமே அடைவு அழிக்கப்படும். அடைவிற்குள் ஏதேனும் கோப்புகளோ அல்லது துணை அடைவுகளோ இருந்தால் அடைவானது அழிக்கப்படமாட்டாது. அடைவிற்குள் இருப்பவைகள் அழிக்கப்பட்டு காலியாகிய பின்புதான் அடைவை அழிக்க முடியும்.
கீழே உள்ள நிரலைப் பாருங்கள்
<?php
//create a new directory using PHP
$yourDirectoryName = “/tmp/phpintamil”;
if ( rmdir($yourDirectoryName) ) {
echo “$yourDirectoryName is successfully deleted.<br>”;
}
else {
echo “Can’t delete the directory.<br>”;
}
?>
வெளியீடு
Finding and Changing the Current Working Directory
தற்போது நாம் இருக்கும் அடைவின் பாதையை getCwd() functionஐ பயன்படுத்தி கண்டுபிடிக்கலாம்.
<?php
$currentDirectroy = getCwd();
echo “Current Directory is $currentDirectroy”;
?>
வெளியீடு
chdir() funtionஐ பயன்படுத்தி நாம் விரும்பிய அடைவிற்குள் மாற்றிக் கொள்ளலாம். அடைவின் பாதையை மட்டும் உள்ளீடாக கொடுக்க வேண்டும்.
<?php
$currentDirectroy = getCwd();
echo “Current Directory is $currentDirectroy<br>”;
$changeDirectory = “/home/kathirvel/Pictures”;
chdir($changeDirectory);
$currentDirectroy = getCwd();
echo “Current Directory is now $currentDirectroy”;
?>
வெளியீடு
அடைவிற்குள் இருக்கும் கோப்புகளை பட்டியலிடுதல் (Listing Files in a Directory)
அடைவுகளுக்குள் இருக்கும் கோப்புகளை scandir() function ஐப் பயன்படுத்தி பட்டியலிடலாம். scandir() இரண்டு உள்ளீடுகளைப் பெற்றுக் கொள்கிறது. அடைவினுடைய பாதையை முதலாவது உள்ளீடாகவும், எந்த வரிசையில் கோப்பு பட்டியலிடப்பட வேண்டும் என்பதை இரண்டாவது உள்ளீடாகவும் பெற்றுக் கொள்கிறது. 0 என்றால் alphabetical முறையிலும், 1 என்றால் reverse-alphabetical முறையிலும் வரிசைப்படுத்துகிறது.
<?php
chdir(“/tmp”);
$currentDirectory = getCwd();
echo “Current Directory is now $currentDirectory<br>”;
$dirArray = scandir(“.”, 1 );
print_r($dirArray);
?>
வெளியீடு
This writing is the work of GOBIKRISHNA D, who is solely responsible for its content. The views in it are the author's own and not those of NEERMAI. Writer responsibility · Report a concern
Encourage the writer
Comments
Become a reader on NEERMAI, free, and be part of a kind and safe community of writers and readers.
- Leave kind comments and replies
- Encourage writers with kind words
- Follow the writers you love
- Chat with writers, safely
This week: write about a letter you never sent
A poem, a story or a few honest lines. Every entry is read by our team.
Start writing









