QUIZGUM

Coding Class

Quizgum : file function

file function

Phase I / O Operations on Files

Steps to open : The preparatory steps before manipulating the file, the method of manipulating the file, checking the existence of the file, and freeing up memory space.
Manipulation step : Operation step that can read or write the actual file
Closing step : Returning resources such as memory used for file operation

fopen() function to open a file

fclose() function to close a file

fopen() is a function that opens an external file such as a document or a picture. fclose() is a function that closes an open file.
The fopen() function accepts the path and file mode setting of the file to open, and the file mode must be set.
If a file cannot be found in the file path, false is returned.
After using a file with the fopen function, you must close the file with the fclose function.
$variable name = fopen(file name, file mode, inc path)
$variable name = fclose($variable name set by the
file mode of fopen()
r : readonly
w : file write-only(If the file exists, the size of the file is 0. If not, a new file is created.)
a : file write-only(If the file exists, append it to the end of the file, otherwise create a new file)
r+ : file read and write only
w+ : file read and write only(If the file exists, the size of the file is 0. If not, a new file is created.)
a+ : file read and write only(If the file exists, append it to the end of the file, otherwise create a new file)
b : Set to binary data(can be omitted)

at the file path separator
When loading a file in Windows, both forward slashes and backslashes are used as path separators. Other operating systems use only forward slashes.

php image
setting path Meanings

$fp = fopen('c:/mamp/htodcs/test.txt','r');

path of windows

$fp = fopen('https://www.startwebcoding.com','r');

path of http server

$fp = fopen('ftp://user:pass@tomodevel.jp','w');

path of ftp server

So let's use fopen fclose. Put image file to be object in htdocs/upload folder or input path of image file directly in source.
I'll put the image file in the htdocs/upload folder and implement it.

file name is fopen.php
/htdocs/upload/fopen.php

<?php
    echo "fopen fclose<br />";

    $fp = fopen("happyCatHeader.png","r");

    if($fp == null){
        echo "The file does not exist.<br />";
    }else{
        echo "The file has been opened.<br />";
        echo "<img src='happyCatHeader.png'><br />";
        fclose($fp);
    }
?>

atom

php image

result

php image

Now let's look at the file_exists() function.
This function tells you if a file exists. If present, it returns 1(true); otherwise it returns 0(false).
If you want to open a file when programming it, you can prevent the error if you check beforehand. It will be useful when you do it .

Test with the source under file_exists

htdocs/fileExists.php

<?php
    $fileName = 'kojiharu.txt';
    echo "file_exists() function that checks for the existence of a file<br />";
    echo "file to find : {$fileName}<br />";

    if(file_exists($fileName)){
        echo "kojiharu.txt file exists. <br />";
    }else{
        echo "kojiharu.txt file does not exist. <br />";
    }
?>

atom

php image

result

php image

This time put the kojiharu.txt file in the htdocs folder to test it.

php image

This time it is said to exist. Blah
and then let out a function for the filesize() function, indicating the size of the file.
This function gives the size of the file in bytes. Let's test it with the kojiharu.txt we created above

htdocs/fileSize.php

<?php
    echo "how to use the function filisize()  <br />";
    echo "the file kojiharu.txt has hello <br />";
    $size = filesize("kojiharu.txt");
    echo "the file size of kojiharu.txt is $size";
?>

atom

php image

result

php image

the content of kojiharu.txt is

kojiharu kawaii

The file size size is shown as 16Byte.
If you press space after kawaii, it will come out as 6, not 5, and if you hit enter it will come out as 7.
Space is 1 byte. The line break is 2 bytes.
this time
fread() - read the data function - fread(file pointer, size),
fwrite() - write data function - fwrite(file pointer, content) , let's look at the

fread() function in the file pointer It reads by size and returns a string and reads it as a binary file.
The file pointer is the address of the file.
Read size from file address.

The fwrite() function writes a file, writes a string at the file pointer, and writes it in binary.
If length is not given, the length of the given string is recorded.
If the length of the string longer than the actual string is given, only the length of the string is recorded.

This time,
I'll try to put the contents of the txt file called A into the txt file called B.
For example, if hello is written in a.txt, create a file called b and put hello in it.

/htdocs/a.txt

Do what you decide to do once.
Even if you have a lot of hardships in the middle, you will do it someday if you do not give up.
It took 18 years to build Asimo in Honda,
Edison had 9000 failures to make a light bulb,
Once a subcontractor for Sony, Samsung has now beat all Japanese electronics companies.
Let's not give up until we all set our goals.

atom

php image

/htdocs/textCopy.php

<?php
    echo "Function to read the contents of a file and write it to another file <br />";
    $fn = "a.txt";
    echo "File to read $fn <br />";
    if(file_exists($fn)){
        $fp = fopen($fn,"r");
        echo "file load success <br />";
        echo "Here is the file contents of $fn:<hr /><br />";
    } else {
        echo "there is no file <br />";
        exit;
    }

    $content = fread($fp, filesize($fn));
    $content = nl2br($content);
    echo $content;

    $fw = fopen("b.txt","a");
    fwrite($fw,$content);
    fclose($fp);
    echo "<hr />";
    echo "file explorer <br />";
    echo "please confirm b.txt <br />";
?>

atom

php image

result

php image

Then check if there is a b.txt. File in the htdocs folder and check its contents.

Now let's look at a function that checks disk space.

Check the total space on the disk: disk_total_space("disk name")
Check the free space on the disk: disk_free_space("disk name")

<?php
    echo "Let's check disk storage and total space.<br />";
    echo "drive's total capacity<br />";
    $dsize = disk_total_space("/");
    //if you use windows
    //$dsize = disk_total_space("c:/");
    $gsize =($dsize / 1024 / 1024 / 1024);
    $by = number_format($dsize);
    $gb1 = number_format($gsize);

    echo " {$by} byte <br />";
    echo "Convert to GB, {$gb1}GB <br /><br /><hr /><br />";
    echo "Remaining Drive Capacity<br /><br />";
    $dsize = disk_free_space("/");
    //if you use windows
    //$dsize = disk_free_space("c:/");
    $gsize =($dsize / 1024 / 1024 / 1024);
    $by = number_format($dsize);
    $gb2 = number_format($gsize);
    echo " {$by} byte <br />";
    echo "Convert to GB, {$gb2}GB <br />";
    $tot =($gb2 / $gb1) * 100;
    $total = number_format($tot);
    $usage = 100 - $total;
    echo "Total capacity is {$gb1} <br />";
    echo "Free capacity is {$gb2} <br />";
    echo "{$usage}% of total space in use <br />";
?>

atom

php image

result

php image