PHP 的 unlink 函数可以与路径一起使用吗?
我想从 PHP 中的文件夹中删除文件,但我只有该文件的路径,是否可以提供取消链接的路径?例如
unlink('path/to/file.txt');
,如果这不起作用,删除这些文件的唯一方法是在 path/to/ 目录中创建一个 .php 文件,并以某种方式将其包含在我的文件中,并在那里调用一个方法来删除该文件,正确的?
I would like to remove a file from a folder in PHP, but I just have the path to this file, would it be ok to give the path to unlink? For example
unlink('path/to/file.txt');
If this doesn't work, the only way to get rid of those files would be to create a .php file in the path/to/ directory and include it somehow in my file an call a method there to remove the file, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
有一个简单的方法来解决您的问题
使用此代码从文件夹中删除文件,
该文件可以在取消链接函数中使用
工作代码
Got an easy method for your question
Use this code to remove a file from a folder
this can be used inside the unlink function
worked code
查看
unlink
文档:和
所以它仅采用字符串作为文件名。
确保可以通过执行脚本的位置的路径访问该文件。这不是绝对路径的问题,但相对路径可能会出现问题。
Have a look at the
unlink
documentation:and
So it only takes a string as filename.
Make sure the file is reachable with the path from the location you execute the script. This is not a problem with absolute paths, but you might have one with relative paths.
取消链接可以很好地处理路径。
如果遇到权限被拒绝错误的问题,有时是在您尝试删除工作目录层次结构中较高文件夹中的文件时引起的(即,当尝试删除以“../”开头的路径时) )。
因此,要解决此问题,您可以使用 chdir() 将工作目录更改为要取消链接的文件所在的文件夹。
unlink works fine with paths.
In case had a problem with the permissions denied error, it's sometimes caused when you try to delete a file that's in a folder higher in the hierarchy to your working directory (i.e. when trying to delete a path that starts with "../").
So to work around this problem, you can use chdir() to change the working directory to the folder where the file you want to unlink is located.
不要忘记检查文件是否存在,否则如果不存在,您将收到错误消息:
Don't forget to check if the file exists, or you will get an error if it doesn't:
您可以将 unlink 与路径一起使用。
您还可以对目录执行取消链接,只要先清空该目录即可。
这是手册:http://php.net/manual/en/function.unlink。 php
You CAN use unlink with a path.
You can also perform unlink on a directory, as long as you have emptied it first.
Here is the manual: http://php.net/manual/en/function.unlink.php
根据文档,
unlink
接受路径的字符串参数。http://php.net/manual/en/function.unlink.php
换句话说...您拥有删除文件所需的一切。
According to the documentation,
unlink
accepts string parameter for the path.http://php.net/manual/en/function.unlink.php
In other words... you have what you need to delete the file.
它不仅可以,而且是 PHP 中删除文件的唯一方法(除了系统调用之外)。
Not only is it OK, it is the only way to delete a file in PHP (besides system calls).
我们可以使用这段代码
We can use this code