PHP in Hindi File I/O




इस Chapter मे हम File Input Output के बारे मे चर्चा करेंगे कि केसे PHP मे File को Open, Read, Write और Close कर सकते है.

  • Opening a File

  • Reading a File

  • Writing a File

  • Closing a File

Opening a File

PHP मे File को open करने के लिये fopen() Function का उपयोग करते है. इस Function के लिए Two Arguments होते है जो पहले File का नाम बताते हैं फिर उस Mode में Opreate होते हैं.

Opening File मे यह Six File modes : r , r+ , w , w+ , a , a+ होते है.

Reading a File

एक बार File को fopen() Function का उपयोग करके खोला जाता है इसे fread() नामक Function से Read किया जा सकता है. इस Function मे दो Arguments की आवश्यकता होती है और ये File Pointer और Bytes मे File Expressed की Length के होना चाहिए.

Files की Length Files Filesize() Function का उपयोग करके पाई जा सकती है जो File Name को अपनी तर्क के रूप में लेती है और Bytes मे व्यक्त File का Size देती है.

For Example

<!DOCTYPE html>
<html>
   <head>
      <title>Reading a File Example</title>
   </head>
   <body>
      <?php
         $filename = "temp_file.txt";
         $filepoint = fopen( $filename, "r" );
         
         if( $filepoint == false )
         {
            echo ( "Error in opening file" );
            exit();
         }
         
         $filesize = filesize( $filename );
         $filetext = fread( $filepoint, $filesize );
         fclose( $filepoint );
         
         echo ( " The following is the content of the file <br/> " );
         echo ( " <pre> $filetext </pre> " );         
      ?>
   </body>
</html>

Output

Writing a File

PHP मे fwrite() Function का उपयोग करके एक New File को write किया जा सकता है या किसी मौजूदा File मे Text को जोड़ा जा सकता है. इस Function को दो Arguments को एक File Pointer और Data के String को Specify करने की आवश्यकता होती है जिससे यह Write किया जाता है.

वैकल्पिक रूप से एक तीसरी Integer Argument को लिखने के लिए Data की Length को Specify करने के लिए शामिल किया जा सकता है. यदि तीसरा Argument शामिल है तो Specify Length तक पहुंचने के बाद Writing बंद हो जायगी. File को Close करने के बाद file_exist() Function का उपयोग कर इसकी मौजूदगी की पुष्टि होती है जो Argument के रूप मे File नाम लेती है.

For Example

<?php
   $filename = "newfile.txt";
   $filepoint = fopen( $filename, "w" );   
   if( $filepoint == false )
   {
      echo ( "Error in opening new file" );
      exit();
   }
   fwrite( $filepoint, "The new file create and write text.\n" );
   fclose( $filepoint );
?>
<html>   
   <head>
      <title>Writing a file Example</title>
   </head>   
   <body>      
      <?php
         $filename = "newfile.txt";
         $filepoint = fopen( $filename, "r" );         
         if( $filepoint == false )
         {
            echo ( "Error in opening file" );
            exit();
         }
         
         $filesize = filesize( $filename );
         $filetext = fread( $filepoint, $filesize );
         
         fclose( $filepoint);
         
         echo ( " Total File size : $filesize bytes <br/> " );
         echo ( " Text in the file:: <br/> " );
         echo ( "  $filetext " );         
      ?>      
   </body>
</html>

Output

Closing a File

PHP मे File को Close करने के लिये fclose() Function का उपयोग करते है.

For Example

<!DOCTYPE html> 
<html>   
   <head>
      <title>Closing a file Example</title>
   </head>   
   <body>      
      <?php
         echo "<p>Opening the file <b>tutorialsroot.txt</b> .....</p>";
         $myfile = fopen("tutorialsroot.txt", "r");
         if($myfile == false)
         {
            echo "Error occurred...exiting...";
            exit();
         }
         echo "<p>File opened successfully.</p>";
         echo "<p>Closing the file....</p>";
         fclose($myfile);
         echo "<p>File closed successfully.</p>"         
      ?>      
   </body>
</html>

Output