CodeIgniter File helper
×

CodeIgniter File helper

0 266

File helper contains functions that help us to work with files.


Related Topic:

Codeigniter Interview Questions
CodeIgniter Date helper
CodeIgniter HTML Helper

Loading this Helper:

Use the given line in your controller or model file to load File helper.

$this->load->helper('file');

The following functions are available in this helper:

1 read_file(): This function is used to get the data contained in the file specified in the path. Working of this function is same as the PHP native function file_get_contents().

Syntax:

read_file();

Parameter Description:

  • (string) – File path

The return type of this function is a string and it returns file contents or FALSE on failure.

Note:

This function might not work if you are trying to access a file If your server is running an open_basedir restriction.

Example:

Step 1 Open the application/controllers directory and create a new controller file Read_file.php

<?php
class Read_file extends CI_Controller {

public function index() {
$this->load->helper('file'); //load File helper
$string = read_file('myfile.txt');
echo $string;
}
}
?>

Step 2 Create a text file myfile.txt into the root directory of your application.

myfile.txt

This is the txt file.

Step 3 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Read_file


2 write_file(): This function is used to write into the file specified in the path. In case of file does not exist, it will create a new file by the given name.

Syntax:

write_file($path, $data[, $mode = 'wb']);

Parameter Description:

  • $path (string) – File path
  • $data (string) – Data to write to file
  • $mode (string) – fopen() mode

The return type of this function is bool and it returns TRUE if the write was successful, FALSE in case of an error.

Example:

Step 1 Open application/controllers directory and create a new controller file Write_file.php

<?php
class Write_file extends CI_Controller {

public function index() {
$this->load->helper('file'); //load File helper
$data = 'Some file data';
if ( ! write_file('myfile.txt', $data))
{
echo 'Unable to write the file';
}
else
{
echo 'File written!';
}
}
}
?>

Step 2 Open the given URL into the browser to write the data into the given file.

http://localhost/ci/index.php/Write_file

Step 3 Now, open the given URL to see the result.

http://localhost/ci/index.php/Read_file


3 delete_files(): This function deletes all files in the directory given by the user.

Syntax:

delete_files($path[, $del_dir = FALSE[, $htdocs = FALSE]]);

Parameter Description:

  • $path (string) – Directory path
  • $del_dir (bool) – Whether to also delete directories
  • $htdocs (bool) – Whether to skip deleting .htaccess and index page files
Note:

In order to be deleted, the files must be writable or owned by the system.

The return type of this function is bool and it returns TRUE on success, FALSE in case of an error.

Example:

Step 1 Open the application/controllers directory and create a new controller file Delete_file.php

<?php
class Delete_file extends CI_Controller {

public function index() {
$this->load->helper('file'); //load File helper
if ( ! delete_files('dummy_files'))
{
echo 'Unable to delete the files';
}
else
{
echo 'File deleted successfully !';
}
}
}
?>

Step 2 Create a folder dummy_files into the root directory of your application which contains some files to delete.

Step 3 Open the given URL into the browser to delete the files.

http://localhost/ci/index.php/Delete_file

Now, your folder will be empty.


4 get_filenames(): This function is used to takes a server path as input and returns an array containing the names of all files contained within it.

Syntax:

get_filenames($source_dir[, $include_path = FALSE]);

Parameter Description:

  • $source_dir (string) – Directory path
  • $include_path (bool) – Whether to include the path as part of the filenames

The return type of this function is an array and it returns an array of file names.

Example:

Step 1 Open the application/controllers directory and create a new controller file Get_filenames.php

<?php
class Get_filenames extends CI_Controller {

public function index() {
$this->load->helper('file'); //load File helper
s = get_filenames(APPPATH.'models/'); //it will give the all files in models folder
echo "<pre>";
print_r(s);
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Get_filenames


5 get_dir_file_info(): This function is used to read the specified directory and builds an array containing the filenames, filesize, dates, and permissions.

Syntax:

get_dir_file_info($source_dir, $top_level_only);

Parameter Description:

  • $source_dir (string) – Directory path
  • $top_level_only (bool) – Whether to look only at the specified directory (excluding sub-directories)

The return type of this function is an array and it returns an array containing info on the supplied directory’s contents.

Example:

Step 1 Open the application/controllers directory and create a new controller file Get_dir_file_info.php

<?php
class Get_dir_file_info extends CI_Controller {

public function index() {
$this->load->helper('file'); //load File helper
$models_info = get_dir_file_info(APPPATH.'models/');
echo "<pre>";
print_r($models_info);
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Get_dir_file_info


6 get_file_info(): This function is used to return the name, path, size, and date modified information attributes for a file by giving the path of the file.

Syntax:

get_file_info([, $returned_values = array('name', 'server_path', 'size', 'date')]);

Parameter Description:

  • (string) – File path
  • $returned_values (array) – What type of info to return

The return type of this function is an array and it returns an array containing info on the specified file or FALSE on failure.

Example:

Step 1 Open the application/controllers directory and create a new controller file Get_file_info.php

<?php
class Get_file_info extends CI_Controller {

public function index() {
$this->load->helper('file'); //load File helper
_info = get_file_info(APPPATH.'controllers/Get_file_info.php');
echo "<pre>";
print_r(_info);
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Get_file_info


7 get_mime_by_extension(): This function is used to translates a filename extension into a MIME (Multipurpose Internet Mail Extensions) type based on application/config/mimes.php file.

Syntax:

get_mime_by_extension(name);

Parameter Description:

  • name (string) – File name

The return type of this function is a string and it returns MIME type string or FALSE on failure.

Example:

Step 1 Open the application/controllers directory and create a new controller file Mime_extension.php

<?php
class Mime_extension extends CI_Controller {

public function index() {
$this->load->helper('file'); //load File helper
= 'uploads/icon-1.png';
echo ." is has a mime type of <strong>".get_mime_by_extension()."</strong>";
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Mime_extension


8 symbolic_permissions(): This function is used to return standard symbolic notation of file permissions by using numeric permissions (such as is returned by fileperms()).

Syntax:

symbolic_permissions($perms);

Parameter Description:

  • $perms (int) – Permissions

The return type of this function is a string and it returns Symbolic permissions string.

Example:

Step 1 Open application/controllers directory and create a new controller file Symbolic_permissions.php

<?php
class Symbolic_permissions extends CI_Controller {

public function index() {
$this->load->helper('file'); //load File helper
echo symbolic_permissions(fileperms('./index.php'));
}
}
?>

Step 2 Open the given URL into the browser to see the result.


http://localhost/ci/index.php/Symbolic_permissions

9 octal_permissions(): This function is used to return a three-character octal notation of file permissions by using numeric permissions (such as is returned by fileperms()).

Syntax:

octal_permissions($perms);

Parameter Description:

  • $perms (int) – Permissions

The return type of this function is a string and it returns an Octal permissions string.

Example:

Step 1 Open application/controllers directory and create a new controller file Octal_permissions.php

<?php
class Octal_permissions extends CI_Controller {

public function index() {
$this->load->helper('file'); //load File helper
echo octal_permissions(fileperms('./index.php'));
}
}
?>

Step 2 Open the given URL into the browser to see the result.

http://localhost/ci/index.php/Octal_permissions



Share:


Leave a Reply


Comments
    Waiting for your comments