You are here: Re: trouble with a delete function « PHP Language « IT news, forums, messages
Re: trouble with a delete function

Posted by David Haynes on 11/21/31 11:37

ZeldorBlat wrote:
> aaron wrote:
>> Does anyone see anything wrong with this code? I've echoed out the
>> contents of the array so the array is fine, but when I run this the code
>> stops here and won't continue.
>>
>> Aaron
>>
>> for($i = 0; $i<=5; $i++)
>> {
>> $file = $file_arr[$i];
>> function file_delete($file) {
>> if(unlink($file)) {
>> return true;
>> } else {
>> return false;
>> }
>
>> }
>> }
>
> Because that code attempts to re-declare the function file_delete. I
> think what you really want is something like this:
>
> //Declare the function file_delete:
> function file_delete($file) {
> if(unlink($file))
> return true;
> return false;
> }
>
> //Use the function file_delete:
> for($i = 0; $i <= 5; $i++) {
> $file = $file_arr[$i];
> file_delete($file);
> }
>

Or even:
foreach($file_arr as $file) {
unlink($file);
}

Although file_delete() returns boolean it is never used.

If file_arr is more than 6 elements, then the code might read:
for($i=0; $i < 6; $i++) {
unlink($file_arr[$i]);
}

If you want to know if *any* unlink was unsuccessful:
$success=true;
foreach($file_arr as $file) {
$success |= unlink($file);
}
if( ! $success )
echo "unlink was not successful\n";

If you want to know which unlink failed:
foreach($file_arr as $file) {
if( unlink($file) ) {
echo "Cannot unlink $file\n";
}
}

etc.

-david-

 

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

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