For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > January 2006 > trouble with a delete function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author trouble with a delete function
aaron

2006-01-15, 9:55 pm

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;
}
}
}
ZeldorBlat

2006-01-15, 9:55 pm


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);
}

Janwillem Borleffs

2006-01-15, 9:55 pm

David Haynes wrote:
> If you want to know if *any* unlink was unsuccessful:
> $success=true;
> foreach($file_arr as $file) {
> $success |= unlink($file);
> }


$success = false;
foreach (......)

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


if ( ! unlink($file) ) {
....
}


JW


David Haynes

2006-01-15, 9:55 pm

Janwillem Borleffs wrote:
> David Haynes wrote:
>
> $success = false;
> foreach (......)
>
>
> if ( ! unlink($file) ) {
> ....
> }
>
>
> JW
>
>

My bad ;-)
You are correct.

-david-

skare_krow

2006-01-15, 9:55 pm

David Haynes wrote:
> Janwillem Borleffs wrote:
>
> My bad ;-)
> You are correct.
>
> -david-
>


Thank you all. This is what I ended up doing, I will probably expand on
this testing for certain cases (existence of file, delete
success/failure) but a big thanks. Here is the code that worked for me.
I could probably do this differently.

Thanks,
Aaron


function file_delete($file) {
if(unlink($file))
return true;
return false;
}

//Use the function file_delete:
for($i = 0; $i <= 5; $i++) {
$file = "../images/".$file_arr[$i];
file_delete($file);
}
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com