w3resource

PHP Exercises: Get the size of a file

PHP: Exercise-39 with Solution

Write a PHP program to get the size of a file.

Sample Solution:

PHP Code:

<?php
// Open a file "/home/students/ppp.txt" for writing, or display an error message if unable to open
$myfile = fopen("/home/students/ppp.txt", "w") or die("Unable to open file!");

// Text to be written to the file
$txt = "PHP Exercises\n";
// Write the first line to the file
fwrite($myfile, $txt);

// Additional text to be written to the file
$txt = "from\n";
// Write the second line to the file
fwrite($myfile, $txt);

// More text to be written to the file
$txt = "w3resource\n";
// Write the third line to the file
fwrite($myfile, $txt);

// Close the file after writing
fclose($myfile);

// Display the size of the created file "/home/students/ppp.txt"
echo "Size of the file: " . filesize("/home/students/ppp.txt") . "\n";
?>

Sample Output:

Size of the file: 30        

Flowchart:

Flowchart: Get the size of a file.

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP program to valid an email address.
Next: Write a PHP program to calculate the mod of two given integers without using any inbuilt modulus operator.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.