How to use php predefine functions in codeigniter?
PHP Functions
file_exists( ) function
The file_exists() function is used to check a file or directory exists or not.
file path or directory is passed as a parameter to the file_exists() function which returns True and False.
file_exists() function accepts only one parameter
File Name :
file_exists($path)
Example
File Name :
$file_path = '/upload/mywork/sana.txt';
if (file_exists($file_path)) {
echo "file exist";
}else {
echo "Not exist";
}
is_dir() Function
Check the specified filename is a directory
File Name :
$file = "upload";
if(is_dir($file)) {
echo "$file is a directory);
} else {
echo "$file is not a directory";
}
?>
create directory
File Name :
if (!file_exists('upload/images')) {
mkdir('upload/images', 0777, true);
}
####################################################
$upload_path_dir = '/media/files/images/';
if (!is_dir($upload_path_dir))
{
$oldmask = umask(0);
mkdir($upload_path_dir,0777,TRUE);
umask($oldmask);
}
umask()
The umask() function changes the file permissions for files.
This function sets umask to mask & 0777 and returns the old umask.
File Name :
Specifies the new permissions. Default is 0777
The mask parameter consists of four numbers:
The first number is always zero
The second number specifies permissions for the owner
The third number specifies permissions for the owner's user group
The fourth number specifies permissions for everybody else
Possible values (to set multiple permissions, add up the following numbers):
1 = execute permissions
2 = write permissions
4 = read permissions
Checking whether a file exists
File name : index.php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
The result of file_exists() is cached, which means you first need to call the clearstatcache() function if you want to be absolutely sure a file exists.
We can use file_exists() to check whether a file exists or not, but keep in mind that this does not work properly with HTTP URLs. This function expects Path to the file or directory parameter. It will Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.
How to generate Random String?
File Name :
function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++)
{
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
File Name :
public function accept_terms()
{
//if (isset($_POST['accept_term_condition']))
if ($this->input->post('accept_term_condition'))
{
return TRUE;
}
else
{
$error = 'Please read and accept our terms and conditions.';
$this->form_validation->set_message('accept_terms', $error);
return FALSE;
}
}
public function gender_validate($gender)
{
// 'none' is the first option that is default "-------Choose Gender -------"
// if($gender=="none"){
if($gender==''){
$this->form_validation->set_message('gender_validate', 'Please Select Your Gender.');
return false;
}
else{
return true;
}
}
Previous
Next