tqla Posted December 28, 2010 Share Posted December 28, 2010 Hello. I wish to get a file name from a DB and then delete it from the server. I have tried the code below. It gets the full url to the photo I wish to delete. It looks like this WITHOUT the spaces: http : // www . mysite . com/users/photo.jpg Then it attempts to unlink it. $result = mysql_query("SELECT PhotoURL FROM `$info` WHERE userid = '$UserID'") or die (mysql_error()); while ($row = mysql_fetch_array($result)) { $myFile = $row['PhotoURL']; unlink($myFile); } I get an error message like this (edited to remove my url, etc): Warning: unlink() [function.unlink]: Unable to locate stream wrapper in /home/user/public_html/code.php : eval()'d code on line 64 What am I doing wrong? Also is there another way to delete server files? Quote Link to comment https://forums.phpfreaks.com/topic/222835-unlink-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2010 Share Posted December 28, 2010 Unlink expects the file system path to the file, not a URL to the file. It would probably be better if you stored the file system path (relative to your document root folder) in the database without the http://www.mysite.com as part of it so that if you ever change domains, you don't need to change the data (in those cases where you need to output a link, you can prepend the http://www.mysite.com to the data before you output the link.) You are also apparently using eval() for this code, which is rarely needed. Why are you using eval()? Quote Link to comment https://forums.phpfreaks.com/topic/222835-unlink-help/#findComment-1152263 Share on other sites More sharing options...
sooner Posted December 28, 2010 Share Posted December 28, 2010 try using is_file($myFile)...then use the unlink()....so that we can check whether a file exist at that path.. if(is_file($myFile)) { unlink($myFile); } Quote Link to comment https://forums.phpfreaks.com/topic/222835-unlink-help/#findComment-1152269 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2010 Share Posted December 28, 2010 @sooner, since is_file() does not work with a HTTP URL, like unlink does not work with a HTTP URL, your code will produce the same exact error. Quote Link to comment https://forums.phpfreaks.com/topic/222835-unlink-help/#findComment-1152274 Share on other sites More sharing options...
tqla Posted December 28, 2010 Author Share Posted December 28, 2010 Thanks PFMaBiSmAd. I am rewriting to remove the URL and just store the filename. But this got me thinking. How then will I use unlink to unlink the photo? Each photo is in the appropriate user folder but the script runs from the root. How do I point it to the correct folder? Also I am not using eval in my script but my script is using JUMI in a Joomla site so my guess is it's coming from there. Quote Link to comment https://forums.phpfreaks.com/topic/222835-unlink-help/#findComment-1152279 Share on other sites More sharing options...
PFMaBiSmAd Posted December 28, 2010 Share Posted December 28, 2010 If the script is in the document root folder and the paths stored in the database are relative to the document root folder, just using the saved path in the unlink() statement should work. In this case, there should be no leading slash / on the path you put into the unlink() statement. Quote Link to comment https://forums.phpfreaks.com/topic/222835-unlink-help/#findComment-1152283 Share on other sites More sharing options...
tqla Posted December 28, 2010 Author Share Posted December 28, 2010 Worked like a charm! Thanks PFMaBISmAd! Quote Link to comment https://forums.phpfreaks.com/topic/222835-unlink-help/#findComment-1152318 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.