7

This is the code

  $query = mysql_query("SELECT avatar FROM users WHERE UserID = ".$userID.""); 
        $row = mysql_fetch_array($query);
        $user_avatar = trim($row['avatar']);
unlink($user_avatar);

but for some reason i get this error Warning:unlink();

why $user_avatar returns empty ? and if i echo it shows t_cabbbccebbfhdb.jpg

Ben
  • 1,906
  • 10
  • 31
  • 47
  • what error do you get? Your post seems to miss the actual error message. Does the file really exist in the current working directory? – catchmeifyoutry Dec 25 '11 at 01:53
  • 3
    I get worried when I see PHP code not using [PHP Prepared Statements](http://php.net/manual/en/pdo.prepared-statements.php) to prevent [SQL Injection](http://en.wikipedia.org/wiki/SQL_injection) vulnerabilities. I hope you are sanitizing your variables in code that hasn't been pasted here. If not, please consider re-writing the code to use PDO Prepared Statements rather than trying to sanitize your variables. – sarnold Dec 25 '11 at 01:54

3 Answers3

12

unlink remove files whereas unset is for variables.

If the variable returns empty, perhaps the query does not return any records. Did you try to run the query manually?

Wesley van Opdorp
  • 14,888
  • 4
  • 41
  • 59
Gilbert Kakaz
  • 1,567
  • 9
  • 23
  • 6
    Please write complete, coherent sentences. "Writing style matters" - http://stackoverflow.com/questions/how-to-answer – BoltClock Dec 25 '11 at 01:53
1
$query = mysql_query("SELECT avatar FROM users WHERE UserID = ".$userID.""); 
        $row = mysql_fetch_array($query);
        $user_avatar = trim($row['avatar']);
unset($user_avatar);

//if you want to unlink file then

if(!empty($user_avatar)) {    
    unlink($home.$user_avatar); // $yourFile should have full path to your file
} 
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • the file exist and if i write $home = $_SERVER['DOCUMENT_ROOT']; unlink($home.'t_cabbbccebbfhdb.jpg'); its working. but if i change it to unlink($home.$user_avatar); the error message displays unlick(/home/naturecl/public_html.) and the $user_avatar is like empty? – Ben Dec 25 '11 at 02:00
  • check edited code, you can use unlink if the $user_avatar contains filename – Sudhir Bastakoti Dec 25 '11 at 02:03
  • $target = "$home/img/avatars/$user_avatar"; and if i echo $user_avatar is showing the file name but in the unlick function is empty and its shows warning:unlink(/home/naturecl/public_html/img/avatars/) – Ben Dec 25 '11 at 02:07
  • You keep missing out the actual error message (i.e. the part right after the the warning title). Posting the full details might make it easier for people to help you. – cmbuckley Dec 25 '11 at 02:12
  • Ok so this is the code $home = $_SERVER['DOCUMENT_ROOT']."/img/avatars/"; $query = mysql_query("SELECT avatar FROM users WHERE UserID = ".$userID.""); $row = mysql_fetch_array($query); $user_avatar = trim($row['avatar']); unlink($home.$user_avatar); and the error is Warning unlink(/home/naturecl/public_html/img/avatars/)[function.unlink]:No such file or directory in /home/naturecl/public_html/settings/portrait.php on line 213 – Ben Dec 25 '11 at 02:20
0

In PHP unlink is used to delete a file, make sure you are giving right path. see here for details http://se.php.net/unlink

try unset for variables. http://se.php.net/manual/en/function.unset.php

Kashif Khan
  • 2,615
  • 14
  • 14