Remove Contents and Delete The Directory in PHP

With the below PHP code you can recursively delete all the contents in the directory and then the directory itself.  This function checks for sub files and sub directories and delete them first before deleting the main folder.


$directory      = "uploads";

if(is_dir($directory)){
recursiveRemoveDirectory($directory);
}

function recursiveRemoveDirectory($directory) {
foreach(glob("{$directory}/*") as $file) {
if(is_dir($file)) {
recursiveRemoveDirectory($file);
} else {
unlink($file);
}
}
rmdir($directory);
}

Hope this helps. Thank You.

Advertisements

Centering Bootstrap Modal Window Using JQuery

By default the Bootstrap Modal Window is centered horizontally, but not vertically. If you want to vertically center the Modal, you can use the following JQuery Code.


$('.modal').on('shown.bs.modal', function() {
$(this).find('.modal-dialog').css({
'margin-top': function () {
return -($(this).outerHeight() / 2);
},
'margin-left': function () {
return -($(this).outerWidth() / 2);
}
});
});

Hide Contents If Javascript is Disabled

Currently most of the content rich websites depend mostly on Javascript. But If the user has Javascript disabled in his browser your website will fail to perform as you expected.

To make sure that the user should enable Javascript to access your website, you can use the noscript tag.


<noscript>
<style type="text/css">
.pagewrapper{display:none;}
</style>
<div class="noscriptmsg">
You don't have javascript enabled in your browser. Please enable javascript to access this website. Thank you.
</div>
</noscript>

You can add this code just below closing the body tag.

File Upload Validation Using JQuery

If in HTML Form, you have a file input, you can validate the extensions as soon as the user upload a file using the JQuery Code given below :

Here’s the HTML Code :


<input id="fileupload" name="fileupload" type="file" />


Here’s the JQuery Code:


var fileTypes = ['jpg', 'jpeg', 'png', 'pdf']; //acceptable file types
function readURL(input) {
if (input.files && input.files[0]) {
var extension = input.files[0].name.split('.').pop().toLowerCase(), //file extension from input file
isSuccess = fileTypes.indexOf(extension) > -1; //is extension in acceptable types
if (isSuccess) {
var reader = new FileReader();
reader.readAsDataURL(input.files[0]);
} else {
alert("File should be in jpg, png, jpeg or pdf format.")
return false;
}
}
}
$("#fileupload").change(function(){ //Dynamically called when a file is uploaded
readURL(this);
});

When the user uploads a file, readURL function is called. In this function it will be checked if the extension of the uploaded file is in the permitted fileTypes array.

Get Date Range Of Current Week in PHP

In PHP many useful date functions are present. One I found very useful for a project was finding the Date range of the current week. With the below code, you can find the start and end date of the current week.

This Sunday will be the start of the week and this Saturday will be the end of the week.

$currentweekstart = date("M jS Y",strtotime('sunday last week'));
$currentweekend = date("M jS Y",strtotime("saturday this week"));

Here in the variable $currentweekstart , we can get the week start date and in $currentweekend we will get the end of the week.

So the week range will be :


$currentweekdate = $currentweekstart.' - '.$currentweekend;


Hope this helps!

URL Validation Using Regular Expression In JQuery

To validate the URL input you can use the function with regular expression (regex) given below :


function ValidateURL(urlToCheck) {
// Below regular expression can validate input URL with or without http:// etc
var pattern = new RegExp("^((http|https|ftp)\://)*([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
return pattern.test(urlToCheck);
}

And use the following code to check if the URL is valid:


var entered_url = $('#url').val();
if (ValidateURL(entered_url) {
//Valid URL
} else {
$('#url').focus();
alert("Please enter a valid URL");
return false;
}