The following PHP script lets you connect to a remote location via PHP’s FTP functionality and delete all files older than a certain amount of given days in that directory, it also provides functionality to list files that should be ignored in a array, these files listed in the array won’t be deleted even if they are older than the given days. This script can be used together with a...
How to remove a specific value within an array using PHP
If you have an array and you wish to remove the “None” values in the array you can use the method below, please replace the “None” value with the value you need to remove from the array $array = array("Apple","Orange","None"); //The array //Removing all None values from the array foreach (array_keys($array, 'none') as $key) { unset($array[$key]); }...
How to transform a month number to month name using PHP
$month = 1;
$month_name = date("F", mktime(0,0,0,$month,1,2011));
echo $month_name; //Will output January
Calculating the date difference in days using PHP
If you have 2 dates and you need to calculate the total days between these two dates using PHP you will need to do the following First, we need to convert the two dates into UNIX timestamps in seconds $date1 = strtotime('2010-10-12'); $date2 = strtotime('2011-11-12'); Calculate the difference between these two dates in seconds $diff = $date2-$date1; Get the total of Days $days =...
How to check if the CURL extention is installed and compiled with PHP
Add this line of code to a PHP script
var_dump(curl_version());
If there was a error similar to this Fatal error: Call to undefined function curl_version() or no results was shown, it means that CURL is not installed.
How to increase the file upload size for PHP using the .htaccess file
Copy the following lines into your .htaccess file, you may adjust the values as you see fit
php_value upload_max_filesize 10M
php_value post_max_size 20M
php_value memory_limit 128M
When generating PDF via PHP using wkhtmltopdf PDf contains garbled squares and characters
I recently installed WKHTMLTOPDF on a Centos 6 Box to generate PDF’s via PHP, after a huge struggle to get WKHTMLTOPDF working, I encountered another problem, whenever trying to generate a PDF via the PHP class that I got from the PDF generated with funny Square’s funny characters etc. I initially thought this might be an encoding problem, so after another few days, I figured it...
[function.file-put-contents]: failed to open stream: File name too long
Error: [<a href=’function.file-put-contents’>function.file-put-contents</a>]: failed to open stream: File name too long To solve this error you need to rename the filename so that the filename and extention together are no more than 255 characters, an easy way to do this is $filename=substr($name,0,251).'.pdf'; Note, that we used the character count 251 since 251 + the 4...
How to suppress PHP error messages
At this piece of code to the top of your PHP script
error_reporting(0);
It’s always better to try and fix the errors than to hide them!!
Nusoap – PHP Warning: Attempt to modify property of non-object in nusoap/lib/nusoap.php on line 4603
Open the file lib/nusoap.php and change line 4594 from
$this->schemas[$ns]->imports[$ns2][$ii]['loaded'] = true;
to
$this->schemas[$ns][$ns2]->imports[$ns2][$ii]['loaded'] = true;
Source:
PHP function used to read CSV files
The below function can be used to read CSV files and returning an array with all values obtained from the CSV file
USAGE:
Copy the below function to your PHP script
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
$line_of_text[] = fgetcsv($file_handle, 1024);
}
How to check if a Request was made via AJAX in PHP
// Check for Ajax Request
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
//Do stuff here
}
PHP function to get the number of days between two dates
The below function can be used to calculate the difference in days between two dates USAGE: /** * Getting the number of days between two dates * * @param string $date1 yyyy-mm-dd * @param string $date2 yyyy-mm-dd * @return integer */ function getDiffInDays($date1,$date2){ $datediff = strtotime($date1)- strtotime($date2); return floor($datediff/(60*60*24)); } $days =...
How to get the last character in a string using PHP
This can be done by using the substr which will return a part of a string.
The sample code below will return the letter f
$lastChar= substr("abcdef", -1);
The -1 is negative, the returned string will start at the last character from the end of the string.