CodeIgniter File helper
0 2819
File helper contains functions that help us to work with files.
Related Topic:

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:
Step 3 Now, open the given URL to see the result.
3 delete_files(): This function deletes all files in the directory given by the user. Syntax:
Step 3 Open the given URL into the browser to delete the files.
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:
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:
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:
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:
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:
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:
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
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<?phpStep 2 Create a text file myfile.txt into the root directory of your application. myfile.txt
class Read_file extends CI_Controller {
public function index() {
$this->load->helper('file'); //load File helper
$string = read_file('myfile.txt');
echo $string;
}
}
?>
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
<?phpStep 2 Open the given URL into the browser to write the data into the given file.
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!';
}
}
}
?>
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<?phpStep 2 Create a folder dummy_files into the root directory of your application which contains some files to delete.
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 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
<?phpStep 2 Open the given URL into the browser to see the result.
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);
}
}
?>
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)
<?phpStep 2 Open the given URL into the browser to see the result.
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);
}
}
?>
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
<?phpStep 2 Open the given URL into the browser to see the result.
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);
}
}
?>
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
<?phpStep 2 Open the given URL into the browser to see the result.
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>";
}
}
?>
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
<?phpStep 2 Open the given URL into the browser to see the result.
class Symbolic_permissions extends CI_Controller {
public function index() {
$this->load->helper('file'); //load File helper
echo symbolic_permissions(fileperms('./index.php'));
}
}
?>

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
<?phpStep 2 Open the given URL into the browser to see the result.
class Octal_permissions extends CI_Controller {
public function index() {
$this->load->helper('file'); //load File helper
echo octal_permissions(fileperms('./index.php'));
}
}
?>
http://localhost/ci/index.php/Octal_permissions

Share:




Comments
Waiting for your comments