php 删除服务器文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4952194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting a server file
提问by Patrick
I'm looking for a way to delete a file from the server using PHP. Basically I have my files listed on a page in this manner:
我正在寻找一种使用 PHP 从服务器中删除文件的方法。基本上,我以这种方式在页面上列出了我的文件:
<ul>
<li><a href="delete_file.php?file=uploads/file_01.jpg">Delete File 01</a></li>
<li><a href="delete_file.php?file=uploads/file_02.jpg">Delete File 02</a></li>
<li><a href="delete_file.php?file=uploads/file_03.jpg">Delete File 03</a></li>
</ul>
The problem is I'm not sure how to get my delete_file.php file to work. I believe it needs to be something like this:
问题是我不知道如何让我的 delete_file.php 文件工作。我相信它需要是这样的:
<?php
$path="uploads/file_01.jpg";
if(unlink($path)) echo "File Deleted";
?>
...but I'm not sure how to get the $path to change to the file I had clicked on to delete.
...但我不知道如何让 $path 更改为我点击删除的文件。
回答by Patrick
while you have to be incredibly careful with giving a user the ability to delete files, I'll give you enough rope to hang yourself
虽然你必须非常小心地给用户删除文件的能力,但我会给你足够的绳子来吊死自己
define a base directory that will contain any files that will be deleted
定义一个包含所有将被删除的文件的基本目录
$base_directory = '/home/myuser/';
Then delete the file
然后删除文件
if(unlink($base_directory.$_GET['file']))
echo "File Deleted.";
回答by Brad Christie
<?php
$file_to_delete = $_GET['file'];
if (is_file($file_to_delete)){
echo (unlink($file_to_delete) ? "File Deleted" : "Problem deleting file";
}
?>
I'm not going to lie, don't know a better way to sanitize the $_GET['file'] other than check if it's a file. If this isn't a valid way, experts please chime in. (Maybe follow the guidelines present in this SO topic?)
我不会撒谎,除了检查它是否是一个文件之外,不知道有更好的方法来清理 $_GET['file'] 。如果这不是一种有效的方法,请专家插话。(也许遵循此 SO 主题中的指南?)
回答by Tolga
Sometimes you may want to create the path dynamically.
有时您可能希望动态创建路径。
For example, I am using a CMS in different places therefore I should not use fixed definitions.
例如,我在不同的地方使用 CMS,因此我不应该使用固定的定义。
My project structure:
我的项目结构:
-myProject
|-admin
|--app
|---controllers
|-upload
-myProject
|-admin
|--app
|---controllers
|-upload
$base_directory = dirname(__FILE__);
echo $base_directory; //'/home/myProject/public_html/admin/app/controlers/'
This is taking the path to the running php file.
这是正在运行的 php 文件的路径。
My php file in 'admin/app/controllers/'
我的php文件在 'admin/app/controllers/'
But upload file in 'upload/'
但是上传文件 'upload/'
We need to delete unnecessary directories for the correct path. The file in the upload folder so we dont need to 'admin/app/controllers/' is unnecessary. So we are removing this part.
我们需要为正确的路径删除不必要的目录。上传文件夹中的文件所以我们不需要'admin/app/controllers/'是不必要的。所以我们正在删除这部分。
$path = str_replace('admin/app/controllers/', '', $path);
echo $path; //'/home/myProject/public_html/upload/myFile'
Now we have correct path and we can delete the file.
现在我们有正确的路径,我们可以删除文件。
if (file_exists($path)){
if(unlink($path)){
echo "File deleted";
}
}else{
echo "File is not exists";
}