本文介绍了unlink() 函数不适用于变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从目录中取消链接文件时遇到了一些麻烦.我会尽量解释清楚.

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

我试图从目录中删除一个文件,首先我从我的数据库中获取文件名,该文件名存储在avatar"字段中.

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

这些是我涉及的变量:

$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).

到目前为止,当我的用户是 hodor 时,当我打印 $avatardirectory 时,它显示hodor.png"

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.

现在我完全迷失了.

推荐答案

变量不能在单引号字符串内展开.

Variables arn't expanded inside single quote strings.

您可能希望看到

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

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

注意 unlink() 删除文件,而不是目录.使用 rmdir() 删除(一个空的)目录.(虽然听起来你的 $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(?) )

这篇关于unlink() 函数不适用于变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 22:51