Question

Im having some troubles unlinking files from a directory. I'll try to explain it as good as i can.

Im trying to delete a file from a directory, first i get the file name from my database, where is stored in field "avatar".

these are my involved vars:

$avatar1=mysqli_query($con,"Select avatar from users where user='$_SESSION[Username]'");
$avatar2=mysqli_fetch_array($avatar1);
$avatardirectory = $avatar2['avatar']; //(missunderstanding name, its actually a file).

So far, when i print $avatardirectory when my user is hodor, it shows "hodor.png"

Ok, here comes the nasty part, i try to go with this:

unlink('/var/www/html/test/img-gallery/$avatardirectory'); //This wont work.

Then I just do the same exact thing but with filename:

unlink('/var/www/html/prueba/img-gallery/hodor.png'); //This actually works.

And now I'm totally lost.

Was it helpful?

Solution

Variables arn't expanded inside single quote strings.

You might want to see the difference in

 echo '/var/www/html/test/img-gallery/$avatardirectory'

and

 echo "/var/www/html/test/img-gallery/$avatardirectory"

Note that unlink() removes files, not directories. use rmdir() to remove (an empty) directory. (though it sounds like your $avatardirectory holds the name of a file, not a directory(?) )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top