Strange Behaviour : unlink not deleting files in php
Somehow unlink is开发者_运维知识库 not deleting the file. If you see in the file, am copying from $incoming_file_path to $processing_file_path and than after copying is done. I am trying to delete the file in $incoming_file_path but somehow is it not deleting and I really wonder why it is happening so. Kindly advice.
<?php
ini_set('error_reporting',0);
$file = fopen("pid.txt","w+") or die('!fopen');
flock($file, LOCK_EX);
//Folder where xml files will be coming in from UPC
$incoming_file_path = "/home/xmlcontainer";
$processing_file_path = "/home/process_file";
$threshold = time() - 30;
foreach( glob($incoming_file_path.'/*')as $key => $value ) {
if ( filemtime($value) <= $threshold ) {
copy($incoming_file_path.$value,$processing_file_path.$value);
print_r($incoming_file_path.$value."\n");
unlink($incoming_file_path.$value);
print_r($incoming_file_path.$value."\n");
print_r($processing_file_path.$value."\n");
}
}
flock($file,LOCK_UN);
?>
readdir() returns the filename without the path. So, in your script instead of filemtime(/home/xmlcontainer/TestInput.xml)
only filemtime(TestInput.xml)
is executed.
Also $incoming_files contains a single file name (as string) within your while-loop. The nested foreach($incoming_files as ...)
will never work.
btw: why do you format the timestamp via date() and then compare the resulting strings against each other?
$file = fopen("pid.txt","w+") or die('!fopen');
flock($file, LOCK_EX);
//Folder where xml files will be coming in from UPC
$incoming_file_path = "/home/xmlcontainer";
$processing_file_path = "/home/process_file";
$threshold = time() - 30;
foreach( glob($incoming_file_path.'/*') as $source ) {
if ( filemtime($source) <= $threshold ) {
// copy / move
// process
// unlink
}
}
flock($file,LOCK_UN);
精彩评论