Handy PHP File Functions Part 1

PHP contains thousands of extensions and hundreds of functions. These function give you the ability to perform multi precess programming means you can use these functions anywhere. I’m going to start a series of handy PHP file functions. This is the part 1 of the series.

 

basename()

basename file function returns the path of a specified file. It has two parameters first one is required and second one is optional.

Syntax:

[code type=”php”]basename(path_to_file, file_extensions)[/code]

Example:

[code type=”php”]
<?php
$path_to_file = ‘/testfolder/test.php’;
echo basename($path);
?>
[/code]

Output:

[code type=”php”]test.php[/code]

chgrp()

This function is used to change the file group. Idea behind this function is that superuser may change the file group but other users may change the file group of which that user is member.

Syntax:

[code type=”php”]chgrp(file_path, group_number_or_name)[/code]

Example:

[code type=”php”]
<?php
$name_of_file = “test.txt”;
$format_of_file = “Group Id: %d\n”;
printf($format_of_file, $name_of_file, date(‘r’), filegroup($name_of_file));
chgrp($name_of_file, 8);
?>[/code]

chmod()

This functions returns true or false and changes the file mode. You should specify mode that consists of three octal numbers.

Syntax:

[code type=”php”]chmod($name_of_file, $mode)[/code]

Example:

[code type=”php”]
<?php
chmod(“/directory/files”, 0600);
?>[/code]

chown()

By using chown() function you can change owner of a specified file.

Syntax:

[code type=”php”]chown($name_of_file, $user)[/code]

Example:

[code type=”php”]

<?php

$name_of_file= “test.php”;
$path_of_file = “/directory/files” . $name_of_file;
$testuser = “root”;

chown$path_of_file, $testuser);

$stat = stat($path);
print_r(posix_getpwuid($stat[‘uid’]));

?>
[/code]

copy()

If you want to create a copy of file by using PHP, copy() function does that.

Syntax:

[code type=”php”]copy($src , $dest)[/code]

Example:

[code type=”php”]
<?php
$src_file = “textcopy.txt”;
$dest_file = “textcopynew.txt”; //It will save the ingredients of textcopy into textcopynew
?>
[/code]

dirname()

This funtion returns path of the parent directory.

Syntax:

[code type=”php”]dirname($path)[/code]

Example:

[code type=”php”]
<?php
echo direname(“/text/”. PHP_EOL;
?>
[/code]

disk_free_space()

This function returns the available or free space of a disk partition or filesystem. Returns information in bytes you can convert the results to MB or GB by performing calculation on bytes.

Syntax:

[code type=”php”]disk_free_space($dir)[/code]

Example

[code type=”php”]
<?php
echo disk_free_space(“E:”); // For Windows
?>
[/code]

disk_total_space

Works like disk_free_space(), but it returns the total space of disk partition or file system.

Syntax:

[code type=”php”]disk_total_space[/code]

Example:

[code type=”php”]
<?php
echo disk_total_space(“E:”);
?>
[/code]

fclose()

Used for closing the opened file.

Syntax:

[code type=”php”]fclose($handle_to_close)[/code]

Example:

[code type=”php”]
<?php
$handle_to_close = fopen(‘test.txt’, ‘w’);
fclose($handle_to_close);
?>
[/code]

feof()

This function checks the end of file on a file pointer.

Syntax:

[code type=”php”]feof($handler)[/code]

Example:

[code type=”php”]
<?php

$file_to_read = @fopen(“name_of_file”, “r”); //If file does not exist or can’t be read return false

while(!feof($file_to_read))
{
}

fclose($file_toe_read);
?>
[/code]

Duan Lingxin

Duan Lingxin

Content crafter and chief editor at Scratching Info. Also regular contributor on other major online tech platforms. Security Specialist by day and a writer by night, he does his best to instill his knowledge about tech while delivering inspiring and life changing resources through his writing,

More Posts - Website

Leave a Comment