Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP unlink function

Tags:

php

mysql

avatars

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

like image 235
Ben Avatar asked Dec 25 '11 01:12

Ben


People also ask

Which function is used to destroy the file?

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.

What does unlink function do?

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.

What does the unlink () function do in PHP?

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.

Can PHP delete files?

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.


2 Answers

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?

like image 183
Gilbert Kakaz Avatar answered Sep 20 '22 18:09

Gilbert Kakaz


$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
} 
like image 35
Sudhir Bastakoti Avatar answered Sep 23 '22 18:09

Sudhir Bastakoti