You are here: Re: phpNewbie - Problem writing to a directory « PHP Programming Language « IT news, forums, messages
Re: phpNewbie - Problem writing to a directory

Posted by Mike Willbanks on 11/10/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);
}
?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация