PHP

PHP Script that deletes files within a remote directory via FTP older than a given amount of days

P

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

H

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]);   }...

Calculating the date difference in days using PHP

C

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 =...

When generating PDF via PHP using wkhtmltopdf PDf contains garbled squares and characters

W

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...

PHP function used to read CSV files

P

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);
 
        }

PHP function to get the number of days between two dates

P

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 =...

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me