Jump to content

unlink help


tqla

Recommended Posts

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?

 

Link to comment
Share on other sites

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()?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.