unlink() function not working with variables? [duplicate]

别来无恙 提交于 2019-12-12 03:59:03

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/20134546/unlink-function-not-working-with-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!