...

View Full Version : isset not working correctly



Kersh86
09-08-2011, 02:27 PM
the isset in my code doesnt seem to do what its suppose to, have i gone wrong somewhere,


echo "1 ".$_FILES['cons_image'.$id[3]]['name']."<br />";
if (isset($_FILES['cons_image'.$id[3]]['name'])){
$q = "SELECT image FROM bands WHERE id = '$id[3]'";
echo "2 ".$q."<br />";
$r = mysql_query($q) or die($q."<br/><br/>".mysql_error());
while($row = mysql_fetch_array($r)){
echo "4 ".$row['image']."<br />";
$file = "../".$row['image'];
echo "5 ".$file."<br />";
}
if (isset($row['image'])){
echo "6 Unlink Area";
exit;
unlink($file);
$file = explode("$table/thumb",$file);
$file = $file[0]."$table".$file[1];
unlink($file);
}
}
exit;

echo output.

1 testing-testing-123.jpg
2 SELECT image FROM bands WHERE id = '44'
4 images/bands/thumb/test102.jpg
5 ../images/bands/thumb/test102.jpg

As you can see $row['image'] has content so it is "set" so it should run the unlink code yet it skips it all together.

with the
if (isset($_FILES['cons_image'.$id[3]]['name'])){ if no file is being uploaded then it should just skip all this code yet it is being run.

am i going about this the wrong way or have i missed something out.

durangod
09-08-2011, 02:35 PM
isset has been having issues from what i know for some time now. not sure what the issues are but try this and see what happens

my understanding is that if you use if strait up, if it has contents (set) it will be true if it is empty it will be false.




if($_FILES['cons_image'.$id[3]]['name']){




let us know if that works please.

Kersh86
09-08-2011, 02:41 PM
I tried just using plain if but it didnt work

durangod
09-08-2011, 02:52 PM
ok well thanks for that info, thats why i wanted to ask because i have been just removing the isset all together and going strait and it seems to work for me every time, but i wanted to ask to make sure it worked for you also.

are you getting this from a form input by chance.

if so can you post your form please

tangoforce
09-08-2011, 02:52 PM
isset has been having issues from what i know for some time now.

No, you're wrong. There is nothing wrong with isset and it has no issues. It's the way its used and the way that its understood that has issues.

@Kersh: I suspect your isset is working properly but you may be confused with where you're expecting to see a file name. You show the output for number 1 and 2 and then say that when no file is uploaded the code still runs. If no file is uploaded then number 1 should fail at the first hurdle because there will be no file name to print yet you're not mentioning any error messages (Do you have error reporting on?).

With regards to the unset of your file I suspect you have a file path fault. It's easy in php to be convinced you've got it right, spend hours on it pulling your hair out and then find its very slightly wrong. Trust me I've done it plenty of times. You should be using:



if (file_exists($file))
{
print "$file found";

if (unlink($file))
{
print "$file deleted";
}
}
else
{
print "$file not found or deleted";
}


This will show you if you have the right file path.

durangod
09-08-2011, 03:01 PM
then i misunderstod, my apologies tango.

Kersh86
09-08-2011, 03:28 PM
Ok just been doing a bit more testing and
if ($_FILES['cons_image'.$id[3]]['name']){ does work correctly as tested it without running round after anyone here. (sorry bout that)

i put my $row['image'] in to a $image variable and then set the if to check that and it works now,

seems $row['image'] was empty outside the while so thats why


thanks for advice

tangoforce
09-08-2011, 04:02 PM
if (isset($row['image'])){
echo "6 Unlink Area";
exit;
unlink($file);


Of course.. calling exit before unlink will also guarantee that unlink doesn't work ;)

durangod
09-08-2011, 04:27 PM
lmao :eek: i didnt even notice that omg wow tango has eyes like an eagle or im just old and blind lol.

Fou-Lu
09-08-2011, 07:59 PM
$row['image'] *must* be false outside of the while. The condition states that it will iterate UNTIL it can be evaluated to false. A recordset fetch of NULL produces this false condition meaning that $row is now populated as NULL and not array.
So yeah, you're options are either to deal with $row['image'] within the scope where it will exist, or capture the results in a predeclared variable for use outside of the while.


Actually, there is an exception to this rule. If you break from the loop, it will still be populated with the last known $row since a false condition was never required in order to terminate the loop.

mindblaster
09-09-2011, 09:48 AM
isset function is not working properly on internet explorer. did you test these things on internet explorer.

Wanna
09-09-2011, 09:49 AM
isset function is not working properly on internet explorer. did you test these things on internet explorer.

The browser shouldn´t care, The PHP is excecuted on the server, not on the client´s browser

Kersh86
09-09-2011, 11:55 AM
if (isset($row['image'])){
echo "6 Unlink Area";
exit;
unlink($file);


Of course.. calling exit before unlink will also guarantee that unlink doesn't work ;)

yeah i put that there so it didnt do the unlink, got fed up of reuploading the image to test. :P



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum