I have the following php file which executes with a cron job. How can I add the functionality that once completed, the xml files in the directory get deleted.
<?php
// Create connection
$con=mysqli_connect("localhost","test","test","epg");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$dir = "xml-files/";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (($file !== '.') && ($file !== '..') ) {
$doc = simplexml_load_file($dir . $file);
foreach ( $doc->ScheduleData->ChannelPeriod as $channelPeriod )
{
$channelId = $channelPeriod->ChannelId;
foreach ( $channelPeriod->Event as $event )
{
$beginTime = $event['beginTime'];
$duration = $event['duration'];
$programName = $event->EpgProduction->EpgText->Name;
$description = $event->EpgProduction->EpgText->Description;
$EventId = $event->EventId;
$sql = "insert into `epg` (`EventId`,`ChannelId`, `BeginTime`,`Duration`, `ShortName`, `Description`) values ('$EventId','$channelId', '$beginTime','$duration', '$programName', '$description')";
if (mysqli_query($con,$sql))
{
echo "Database updated successfully";
}
else
{
echo "Error creating database: " . mysqli_error($con);
}
}
}
}
}
closedir($dh);
}
}
?>
Many thanks for your help guys! much appreciated.
At the end of your code;
function deleteFiles($dir) {
$files = glob($dir);
foreach($files as $file){
if(is_file($file))
unlink($file);
}
}
deleteFiles("xml-files/*"); This will delete all files under this dir
http://de2.php.net/manual/en/function.unlink.php
The unlink function deletes a file. If you have more than one file, Loop through the directory and then perform the function for each file.
you use can unlink($filename);
function to Delete files once PHP script is completed
http://php.net/manual/en/function.register-shutdown-function.php
You can define a shutdown function, whenever the script finishes or exits this function is called:
function delete_files() {
// delete files here
}
register_shutdown_function('delete_files');