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
The remove function in C/C++ can be used to delete a file. The function returns 0 if files is deleted successfully, other returns a non-zero value. Using remove() function in C, we can write a program which can destroy itself after it is compiled and executed.
The unlink() function shall remove a link to a file. If path names a symbolic link, unlink() shall remove the symbolic link named by path and shall not affect any file or directory named by the contents of the symbolic link.
Unlink() function: The unlink() function is an inbuilt function in PHP which is used to delete a file. The filename of the file which has to be deleted is sent as a parameter and the function returns True on success and False on failure.
To delete a file in PHP, use the unlink function. Let's go through an example to see how it works. The first argument of the unlink function is a filename which you want to delete. The unlink function returns either TRUE or FALSE , depending on whether the delete operation was successful.
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?
$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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With