PHP Program for Create, Delete, and Copying file from PHP Script.

PHP Create file - fwrite() 👇

<?php

   $myfile = fopen("newfile.txt", "w") or
   die("Unable to open file!");

    $txt = "Lost Variables \n";

    fwrite ($myfile, $txt);
    $txt = "Lost Variables\n";

    fwrite ($myfile, $txt);
    fclose ($myfile);
?>

w will Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file.


<?php
// PHP program to delete a file named newfile.txt 
// using unlink() function 

$file_pointer = "newfile.txt"; 

// Use unlink() function to delete a file 
if (!unlink($file_pointer)) { 
    echo ("$file_pointer cannot be deleted due to an error"); 
}   
else { 
    echo ("$file_pointer has been deleted"); 
} 

?>

Copy a file using PHP 👇

First Create New File named demo.txt and insert and dummy text to copy to another file.

<?php
    $file = "demo.txt";
    $newfile = "demo2.txt";

    if ( !copy( $file, $newfile ) )
    {
        echo "Faild to Copy $file .. \n";
    }

?>

demo2.txt will create by copy() function.

No Comments Yet