Category Archives: PHP
Image CRUD for Codeigniter
So by now if you’re in the CodeIgniter scene i’m sure you’ve heard of grocery CRUD?! Well – the same guy, Johnny Skoumbourdis, who brought you grocery CRUD now brings you image CRUD.
Image CRUD is an automatic multiple image uploader for Codeigniter. With the same philoshopy of grocery CRUD library. Just simple line of codes and you have all the functionality that you need.
It really is easy to use, easy to implement and overall just really great!
Thanks Johnny! Hope this becomes apart of grocery CRUD in the near future!
To download image CRUD or view examples, click here.
PHP 5.3 problem with CodeIgniter 1.7.X, DOMPDF and Others
Magic Quotes feature has been DEPRECATED as of PHP 5.3.0+ and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.
Magic Quotes is a process that automatically escapes incoming data to the PHP script. It’s preferred to code with magic quotes off and to instead escape the data at runtime, as needed. The Earlier versions of Codeigniter has used magic quotes functions of PHP… so, be aware of that.
If you haven’t update your CodeIgniter version to 1.7.3 (Or to CodeIgniter 2+) . Your site may crash and display errors any time if your server has upgraded to PHP 5.3. You may see the following errors when trying to access your website:
Message: Function set_magic_quotes_runtime() is deprecated
or
Severity: 8192 Message: Assigning the return value of new by reference is deprecated Filename: libraries/Loader.php Line Number: 255
If you are using DomPDF 5.x then you might get similar issues when trying to generate PDF documents. As far as I know the team is focussing on DomPDF version 6 and dont have a update yet. You can download the updated class here.
If you are using Joomla or WordPress and you are getting errors similar like the above just upgrade to the latest version.
Your issue with magic quotes should now be resolved!!
Using PHP Headers to Force Download
If you have documents that you are trying to download, such as a PDF document etc, you can use the script below to help you out instead of having to open up the file in a new tab.
Example:
Create a PHP file called “force_download.php” and insert the code below.
//PATH TO FILE
$file = 'my_file.pdf';
if(!file_exists($file)))
{
die('Error: File not found.');
} else {
//SET HEADERS
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
//READ FILE
readfile($file);
}
Next your links to your documents will look like this
Download File
That’s it, pretty easy!
Format Phone Numbers
Here is a simple PHP function to format a simple 10-digit phone number to a standard string format. You can easily edit the string to suit your needs, in this example the format looks like this: (xxx) xxx-xxxx
function phone_number($str)
{
$strPhone = preg_replace('/\s*/m', '', $str);
$strPhone = preg_replace("[^0-9]", '' , $strPhone);
if (strlen($strPhone) != 10)
return $strPhone;
$strArea = substr($strPhone, 0, 3);
$strPrefix = substr($strPhone, 3, 3);
$strNumber = substr($strPhone, 6, 4);
//edit the format of the string here
$strPhone = "(".$strArea.") ".$strPrefix."-".$strNumber;
return ($strPhone);
}
Example usage:
$number = "0845556666"; echo phone_number($number);
Result: (084) 555-6666
You can also modify this function to format other numbers such as identity numbers, credit card numbers etc.
Check if file exists on a remote server
Below is a relatively simple PHP function you can use in your code that will check if a file exists on a remote server using the PHP cURL library. Basically what it does it it reads your URL and returns true or false depending on whether the file could be located or not. The code can easily be customized too depending on what your require to use it for.
//FUNCTION TO CHECK STATUS OF FILE LOCATED ON ANOTHER SERVER
function check_file_status($url, $file) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.'/'.$file);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
$data = curl_exec($ch);
curl_close($ch);
// make sure url is valid
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
$code = end($matches[1]);
if(!$data) //file not found
{
//return your custom code here
return true;
}
else
{
if($code == 200)
{
//return your custom code here
return true;
}
elseif($code == 404) //404 page not found error
{
//return your custom code here
return false;
}
}
}
//CALL FUNCTION
check_file_status('http://www.kabadabra.com', 'robots.txt');
//EXAMPLE USAGE
if(check_file_status('http://www.kabadabra.com', 'robots.txt')) {
echo 'File found on server!';
} else {
echo 'File could not me located...';
}
Learn more about PHP cURL here.