Posted by Mike Willbanks on 11/05/43 11:16
Win,
> I'm trying to scan a directory for a certain file and create it if it's
> not found, but I'm having trouble with creating the file.
>
> I've included a small rough segment of code to illustrate what I was
> trying to do. And I'm working in a Windows environment (IIS)...just in
> case such information is needed.
>
> Any assistance would be appreciated.
Could you let us know what exactly your problem is? Is it scanning your
directory? If not use a fullpath.
Also with your date arrays you should really assign your variables like
the following:
$yr = $date_array['year'];
$mn = $date_array['mon'];
$dy = $date_array['mday'];
Otherwise you are using more memory than is needed.
Now for some better php code that might work a bit better... I used a
little less variables, and also added in some more checks so you weren't
trying to scan a directory if it did not exist.
<?php
$dir = '/path/to/fullacc';
$date = getdate();
$tfile = $date['year'].'-'.$date['mon'].'-'.$date['mday'].'.txt';
$found = false;
if (is_dir($dir)) {
if ($dh = opendir($dir) {
while(($file = readdir($dh) !== false) {
if ($file == $tfile) {
$found = true;
break;
}
}
closedir($dh);
}
}
if ($found === true) {
echo '<h3>'.$tfile.' found</h3>';
} else {
echo '<h3>'.$tfile.' not found, so will be created</h3>';
touch($dir.'/'.$tfile);
}
?>
[Back to original message]
|