Get Website Learn Coding Tech Story

PHP Programming Examples

<?php
	/* Create new file */
	$filename = "myFile.txt";
	
	/* Open file in write mode */
	$file = fopen($filename, "w") or die("Unable to open file!");
	$txt = "My first file!";
	
	/* Write text into file */
	fwrite($file, $txt);
	
	/* Close file */
	fclose($file);
	echo "File created successfully!<br />";
	
	
	/* Delete file */
	/* Checking is file exist or not */
	if(is_file($filename)) {
		
		/* Unlink(delete) file */
		if(unlink($filename))
			echo "File deleted successfully!";
		else
			echo "File can't be delete...try again!";
	}
	else
		echo "File no found!";
?> 



          /****** Output ******/
          

File created successfully!


File deleted successfully!

Learn more about fopen, fwrite, fclose and unlink