PHP Tutorial Tutorials - PHP File Handling

PHP File Handling

PHP file system functions allow you to manipulate files on the web server side.
But you muse be very careful because you can do a lot of damage if something is wrong.
(specify a wrong file name, accidentally deleting a file's contents.)

How to create a file using php

In PHP the fopen function is used to open files. However, it can also create a file if it does not exist.
The modes we can use include: w, w+, a, a+, x, x+;

<?php
$filename = "data.txt";
 
if(file_exists($filename)){
    echo "The file $filename exists";
} else{
    echo "The file $filename does not exist, attempt to create it.";
    $handle = fopen($filename, "w");
}
?>

An alternative way to create file is using touch() function.
touch() is used to set access and modification time of file.But if the file does not exist, it will be created.

<?php
$filename = "data.txt";
 
if(!file_exists($filename)){
    echo "The file $filename does not exist, attempt to create it.";
    touch($filename);
}
?>

How to rename a file using php

You can rename a file or directory using the PHP's rename() function, like this:

<?php
// rename("old fileName with extension","New fileName with extension");
$oldFilename = "page.html";
$newFilename = "showPage.php";
 
// Check if the file exist or not
if(file_exists($oldFilename)){
    // Attempt to rename the file
    if(rename($oldFilename, $newFilename)){
        echo "File renamed successfully.";
    } else{
        echo "The file cannot be renamed.";
    }
}
?>

How to delete a file using php

You can delete files or directories using the PHP's unlink() function.
You muse be very careful when delete a file ,make sure the path and filename are correct.

<?php
unlink("data.doc");
unlink("d:\\data.txt");
unlink((realpath('../corephp.pdf'));
?>

How to copy a file using php

The PHP copy() function is used to copy files.If you wish to move a file, use the rename() function.
Syntax:

bool copy ( string $source , string $dest)

It will return TRUE on success or FALSE on failure.

<?php
$file = 'data.txt';
$newfile = 'data.txt.bak';

if (copy($file, $newfile)) {
    echo "copy $file successfully.\n";
}
?> 

How to check if a file exists or not

The PHP function file_exists() is used to check whether a file or directory exists.

<?php
$filename = "data.txt";
 
if(file_exists($filename)){
    echo "The file $filename exists";
} else{
    echo "The file $filename does not exist";
}
?>

PHP Filesystem Functions

PHP provides some other useful PHP filesystem functions,For more information, Please refer tothe PHP filesystem reference section.

Function Description
is_dir() Checks whether the file is a directory.
is_file() Checks whether the file is a regular file.
is_executable() Checks whether the file is executable.
realpath() Returns canonicalized absolute pathname.
rmdir() Removes an empty directory.
feof() Checks whether the end of the file has been reached.
Date:2019-10-01 02:52:35 From:www.Lautturi.com author:Lautturi