Showing posts with label query. Show all posts
Showing posts with label query. Show all posts

How to Parse/Divide URL in PHP?

parse_url is a simple tool/predefined function to parse/divide URL into individual components.

components are:

1. scheme
2. host
3. path
4. query


example:


<?php

//URL
$url = "http://www.phpjavascript.com/post.php?par1=one&par2=two";

$url = parse_url($url);

//print_r($url);

echo 'scheme = '.$url['scheme'];
echo 'host = '.$url['host'];
echo 'path = '.$url['path'];
echo 'query = '.$url['query'];

?>

Result:

scheme = http
host = www.phpjavascript.com
path = /post.php
query = par1=one&par2=two

Delete files from folder using php

Sometimes we need to free the space of the web server by deleting old or specific or all files from a directory. Means we want to remove files from the folder without know the name of the files. This type of functionality required for the following situation.
1. Need to remove all files in a folder for free the space of web server.
2. Need to remove old files in a folder which are used before the specific time.
3. Need to remove some files from a folder which holds a specific extension.

To delete files from directory we’ll use glob() and unlink() functions in PHP.
glob() function is used to get filenames or directories as an array based on the specified pattern.
unlink() function is used to delete a file.
Delete All Files from Folder:
The following script removes all files from the folder.
<?php
$files = glob('NewFolder/*'); //get all file names
// NewFolder is the name of folder
foreach($files as $file){   
 if(is_file($file))        
unlink($file); //delete file
}
?>
Delete Specific Type of file  from Folder:
The following script removes only those files which have a specific extension.(In this example delete all file with .jpg extension )
<?php
$files = glob('NewFolder/*.jpg'); //get all file names
// NewFolder is name of folder
foreach($files as $file){   
 if(is_file($file))        
unlink($file); //delete file
}
?>
Delete Old Files from Folder:
The following script removes the files which modified before the specified time.
<?php
$files = glob('NewFolder/*'); //get all file names
// NewFolder is the name of folder
foreach($files as $file) {    
$lastModifiedTime = filemtime($file);    
$currentTime = time();    
$timeDiff = abs( $currentTime - $lastModifiedTime) / (60 * 60); //in one hours   
 if (is_file($file) &&  $timeDiff > 10) //check if file is modified before 10 hours     
 unlink($file); //delete file
}

Labels

php (35) javascript (31) phpjavascript (30) jquery (23) html (20) mysql (14) database (9) codeigniter (4) json (4) bar chart (2) calendar (2) column chart (2) framework (2) google maps (2) query (2) tables (2) url (2) dropdown (1)

Popular Posts