[thelist] PHP deleting a file

Jay Blanchard jay.blanchard at THERMON.com
Wed Oct 12 07:20:45 CDT 2005


[snip]
you're doing the same thing twice. It compares the 2 strings and they don't
seem to be the same. Maybe your $oldfile filename needs some convincing, did
you do all the stripslashes, trim and maybe strtolower as well? Are you sure
there's no linebreak or other hidden character? You checked the charset?

To debug this function, simply overwrite $oldfile, such as:

$oldphoto = "nophoto.gif"; //I c&p'd it from the if-statement, just taking
off the ! and you should do the same.

if ($oldphoto != "nophoto.gif") {
    // Delete the agents existing photo
    @unlink("$oldphoto");
  }

if THAT still deletes the file, then it's spooky. Otherwise your strings,
although they might look right, simply aren't the same.
[/snip]

Remove the quotes in your unlink statement. Echo $oldphoto to see what it
really contains. And restructure your conditional statement....

<tip type="conditional testing">
In order to eliminate possible coding errors when constructing if statements
you may want to consider putting the variable as the second arguement, i.e.

if("nophoto.gif" == $oldphoto){
   /* do dtuff */
}

The reason? If you mistype the statement the when putting the variable first
you may actually assign the value to the variable;

if($oldphoto = "nophoto.gif"){
   /* I just assigned the value instead of comparing it */
}

Doing it the suggested way (value first, variable second) will cause an
error to be thrown in most programming environments;

if("nophoto.gif" = $oldphoto){
   /* an error will be thrown */
}

</tip>


More information about the thelist mailing list