PHP Delete file
Posted by blogmeister on
March 24, 2008
Delete a file in the file system. Note that this file system we’re talking about is the server’s file system. PHP nor other web-based programming language deletes a file system on the client’s side.
function deleteFile($filename) {
$do = unlink($filename);
if ($do=="1") {
echo "The file was deleted successfully.";
} else {
echo "There was an error trying to delete the file.";
}
}
tags: delete file
No Comments
PHP Get difference between 2 dates
Posted by blogmeister on
March 24, 2008
This code gets the date difference manually, specifying a date value.
$digest_date = "2009-12-25";
$date_diff = abs(strtotime(date('y-m-d'))-strtotime($digest_date)) / 86400;
and another version in a method if you want to keep on reusing this code.
function dateDiff($dformat, $endDate, $beginDate) {
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}
PHP: What’s the deal with strpos() ?
Posted by blogmeister on
February 9, 2008
Languages like Java and Javascript provide a method called indexOf() that returns an int of the first position of the string key found in a string. strpos() in PHP acts differently. If it finds the substring within the string, it returns an int value of the position of the substring. If it does not, it returns a boolean instead.
I find this stupid. It would have been easier if it still returned an int value like the usual -1 to indicate it didn’t find any substring inside the string.
To do an if statement using strpos() of PHP just like this Java code:
|
1 2 |
String str = "This is an object"; if (str.indexOf("an") > -1) System.out.println("substring found"); |
Do this in PHP:
|
1 2 |
$str = "This is an object"; if (strpos($str, "an") !== true) echo "substring not found"; // if statement is false |
Notice the 2 equal sign succeeding after the exclamation point? That line checks if the statement is false. If you wish to change it to a true condition statement, just put in 3 equal signs.
Found this useful? Donations appreciated to help keep this blog alive.







