
Write to File – PHP हमें कई ऐसे Functions Provide करता है, जिनका प्रयोग करके हम किसी Content को किसी File या Specified Resource में Write कर सकते हैं। File में Content को Write करने से संबंधित PHP द्वारा Provided कुछ Functions निम्नानुसार हैं:
fwrite() Function
इस Function का प्रयोग करके हम किसी Content को उस File में Write कर सकते हैं, जिसे हमने fopen() Function द्वारा Open किया है। ये Function तीन Arguments Accept करता है और एक Integer Type की Value Return करता है। इस Function का Syntax निम्नानुसार होता हैः
int fwrite(resource $handle, string $string, int $length);
इस Function के पहले Argument के रूप में हमें उस File का Handle Specify करना होता है, जिसे हमने fopen() Function का प्रयोग करके Create किया है।
दूसरे Argument के रूप में हमें वह Content या Data String Format में Specify करना होता है, जिसे हम पहले Argument में Specified File Handle वाली File में Write करना चाहते हैं।
इस Function का तीसरा Parameter एक Optional Parameter है। यदि हम इस Parameter को Specify करते हैं, तो Specified Length के बराबर Bytes File में Write हो जाने के बाद बचे हुए Contents File में Write नहीं होते।
यदि ये Function Successfully Run हो जाता है, तो File में Write की गई कुल Bytes की संख्या Return करता है जबकि Error की स्थिति में false Return करता है।
इस Function को ठीक से समझे के लिए हम निम्नानुसार एक PHP Page Create कर सकते हैं, जिसमें User अपने Name, Email Address, Username व Password के साथ Register करेगा और User की Information को Web Server के File System पर subscribers.txt नाम की एक File Create करके उसमें Store किया जाएगा।
//Filename: registration.php <?php $title = "Registration"; include_once('header.php'); ?> <form id="register" name="register" method="post" action="register.php"> <p> <label for="name">Name</label> <input type="text" name="name" id="name" /> </p> <p> <label for="email">Email</label> <input type="text" name="email" id="email" /> </p> <p> <label for="username">Username</label> <input type="text" name="username" id="username" /> </p> <p> <label for="password">Password</label> <input type="password" name="password" id="password" /> </p> <p> <input type="submit" name="submit" id="submit" value="Register" /> </p> </form> <?php include_once('footer.php'); ?> //Filename: register.php <?php $title = "Registered"; include_once('header.php'); $subscribers = fopen("subscribers.txt", "a+"); if(fwrite($subscribers, $_REQUEST['name'].", ".$_REQUEST['email'].", ".$_REQUEST['username'].", ".$_REQUEST['password']."\n")) echo "<h1>Registration Successful</h1>"; else echo "<h1>Registration Failed</h1>"; fclose($subscribers); ?> <?php include_once('footer.php'); ?>
जब User Registration Form पर Registration Information Fill करके Subscribe Button पर Click करता है, तो Registration Form की सारी Information register.php Page पर पहुंचती है। इस Page पर हमने subscribers.txt नाम का एक File Open किया है और File को Append Mode में Open किया है, ताकि हर Registration Information File के अन्त में Add हो।
फिर हमने एक Comma Separated List के रूप में subscribers.txt नाम की File में fwrite() Function का प्रयोग करते हुए Registration के रूप में आने वाली Information Write कर दिया है और अन्त में File को Close करके File द्वारा Reserved Resource को Free किया है।
चूंकि हम ये जानना चाहते हैं कि Registration Information ठीक तरह से File में Site हुई या नहीं, इसलिए fwrite() Function को हमने एक if Statement के बीच लिखा है। क्योंकि यदि fwrite() Function ठीक से Execute होता है, तो File में Write होने वाली कुल Bytes की संख्या Return करता है, जो कि if Statement के लिए True की तरह होता है।
लेकिन यदि ये Function ठीक से Execute नहीं होता, तो false Return करता है। जिससे if Statement का else Part Execute होता है और Registration Failed का Message मिलता है।
fputs() Function
ये Function वास्तव में fwrite() Function का Alias ही है। इसलिए यदि हम चाहें तो fwrite() Function के स्थान पर इस Function का भी प्रयोग कर सकते हैं।
ये Article इस वेबसाईट पर Selling हेतु उपलब्ध EBook PHP in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी।
PHP in Hindi | Page: 647 | Format: PDF