EchoFool Posted June 23, 2009 Share Posted June 23, 2009 I have an unlink function which deletes an image in a set location but it won't work and i don't know why... this is my script : <?php If(isset($_GET['banner'])){ $Field = 'Banner'; $Field2 = 'BannerUrl'; }ElseIf(isset($_GET['global'])){ $Field = 'Global'; $Field2 = 'GlobalBanner'; }ElseIf(isset($_GET['homepage'])){ $Field = 'HomePage'; $Field2 = 'HomePageUrl'; } $Check = mysql_query("SELECT $Field2 FROM listings WHERE UserID='{$_SESSION['Current_User']}' AND GameID='$GameID' AND $Field='1'") Or die(mysql_error()); If(mysql_num_rows($Check)<1){ $errors = 1; Echo 'This listing ID does not belong to you! Please try again!'; }Else{ $row = mysql_fetch_assoc($Check); If(file_exists('images/'.$GameID.'/'.$row[$Field2])){ unlink('images/'.$GameID.'/'.$row[$Field2]); } ?> What i don't get is this, i get the error: Warning: unlink(images/21/) [function.unlink]: Permission denied on line 35 Notice the image name is missing but when i echo $row[$Field2] it shows the image name... Also how come its denied? I used this same function on other images and its fine but then when i am using this script its denied. The location of the image is correct also.... what happened here exactly? Link to comment https://forums.phpfreaks.com/topic/163307-unlink-function-wont-work/ Share on other sites More sharing options...
pkedpker Posted June 23, 2009 Share Posted June 23, 2009 by looks of the warning images/21/ looks like $row[$Field2] is blank. which i guess should be the file extension u got alot of strange stuff in that query of yours like the SELECT $Field2.. thats pretty strange I know mysql accepts $Field2 fields? with dollarsigns is that how it is in your database table? with a dollarsign.. same goes to the end of it the query where it says AND $Field='1' same thing does $Field exist in table? pretty odd choice considering it might clause with the php variable names idk if it would but you also have the same variables in php $Field and $Field2 Link to comment https://forums.phpfreaks.com/topic/163307-unlink-function-wont-work/#findComment-861663 Share on other sites More sharing options...
EchoFool Posted June 23, 2009 Author Share Posted June 23, 2009 $Field2 is a php variable containing a string from what is set in the above if statements its great when you want to query a table with different options, you just change the string in the field rather than writing 3 queries in each if statement section, reduces code alot ... i've used it alot and has always worked.. but when i echo : $row[$Field2] it works yet in the unlink its not... Link to comment https://forums.phpfreaks.com/topic/163307-unlink-function-wont-work/#findComment-862007 Share on other sites More sharing options...
flyhoney Posted June 23, 2009 Share Posted June 23, 2009 Let's clean up the code a bit <?php if (isset($_GET['banner'])) { $Field = 'Banner'; $Field2 = 'BannerUrl'; } else if (isset($_GET['global'])) { $Field = 'Global'; $Field2 = 'GlobalBanner'; } else if (isset($_GET['homepage'])) { $Field = 'HomePage'; $Field2 = 'HomePageUrl'; } $query = " SELECT $Field2 FROM listings WHERE UserID='{$_SESSION['Current_User']}' AND GameID='$GameID' AND $Field='1' "; $Check = mysql_query($query) or die(mysql_error()); if (mysql_num_rows($Check) < 1) { $errors = 1; echo 'This listing ID does not belong to you! Please try again!'; } else { $row = mysql_fetch_assoc($Check); $file = 'images/'.$GameID.'/'.$row[$Field2]; if (file_exists($file)) { unlink($file); } } Link to comment https://forums.phpfreaks.com/topic/163307-unlink-function-wont-work/#findComment-862030 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.