使用 php/codeigniter 删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9747897/
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 File using php/codeigniter
提问by Jetoox
I would like to delete a file that is found in my localhost.
我想删除在我的本地主机中找到的文件。
localhost/project/folder/file_to_delete
I'm using codeigniter for this.
我为此使用了 codeigniter。
I would like to use the unlink() function in php but I really can't understand how to use it.
我想在 php 中使用 unlink() 函数,但我真的不明白如何使用它。
回答by Taha Paksu
you can use the "file helper" in codeigniter.
您可以在 codeigniter 中使用“文件助手”。
http://codeigniter.com/user_guide/helpers/file_helper.html
http://codeigniter.com/user_guide/helpers/file_helper.html
and like this :
像这样:
$this->load->helper("file");
delete_files($path);
Late Edit:delete_files
method uses a path to wipe out all of its contents via unlink()
and same you can do within CI. Like this:
后期编辑:delete_files
方法使用一个路径来清除它的所有内容unlink()
,你可以在 CI 中做同样的事情。像这样:
unlink($path);
a valid path.
一个有效的路径。
回答by Pave
http://php.net/manual/en/function.unlink.php
http://php.net/manual/en/function.unlink.php
It is the best way to understand. Read it!
这是最好的理解方式。阅读!
$path_to_file = '/project/folder/file_to_delete';
if(unlink($path_to_file)) {
echo 'deleted successfully';
}
else {
echo 'errors occured;
}
回答by rafi
to delete file use
删除文件使用
unlink($file_name);
or to delete directory use
或删除目录使用
rmdir($dir);
回答by Subkhan Sarif
Try this, this works for me:
试试这个,这对我有用:
unlink("./path/to/folder/file_name_do_delete");
for example: I put my file inside uploads folder which is outside the application folder and my file name is 123.jpg. So it should be like this:
例如:我将我的文件放在应用程序文件夹之外的上传文件夹中,我的文件名为 123.jpg。所以它应该是这样的:
unlink("./uploads/123.jpg");
回答by Hkachhia
$file = "test.txt";
if (!unlink($file))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $file");
}
回答by Bablu Ahmed
Use FCPATH
in unlink. You can try as follows this works for me:
FCPATH
在取消链接中使用。你可以尝试如下这对我有用:
$file_name = $SBLN_ROLL_NO."_ssc";
$file_ext = pathinfo($_FILES['ASSIGNMENT_FILE']['name'],PATHINFO_EXTENSION);
//File upload configuration
$config['upload_path'] = $upload_path;
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
$config['file_name'] = $file_name.'.'.$file_ext;
//First save the previous path for unlink before update
$temp = $this->utilities->findByAttribute('SKILL_DEV_ELEMENT', array('APPLICANT_ID'=>$STUDENT_PERSONAL_INFO->APPLICANT_ID, 'SD_ID'=>$SD_ID));
//Now Unlink
if(file_exists($upload_path.'/'.$temp->ELEMENT_URL))
{
unlink(FCPATH . $upload_path.'/'.$temp->ELEMENT_URL);
}
//Then upload a new file
if($this->upload->do_upload('file'))
{
// Uploaded file data
$fileData = $this->upload->data();
$file_name = $fileData['file_name'];
}
回答by bharat
simply can use:
简单地可以使用:
$file = "uploads/my_test_file.txt";
if (is_readable($file) && unlink($file)) {
echo "The file has been deleted";
} else {
echo "The file was not found";
}
回答by Amir Ur Rehman
September 2018this solution worked for me.
2018 年 9 月,此解决方案对我有用。
if(unlink(FCPATH . 'uploads/'.$filename)){
echo "Deleted";
}else{
echo "Found some error";
}
回答by qwertzman
This code can also handle non-empty folders - just use it in a helper.
此代码还可以处理非空文件夹 - 只需在帮助程序中使用它。
if (!function_exists('deleteDirectory')) {
function deleteDirectory($dir) {
if (!file_exists($dir)) return true;
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') continue;
if (!deleteDirectory($dir . "/" . $item)) {
chmod($dir . "/" . $item, 0777);
if (!deleteDirectory($dir . "/" . $item)) return false;
};
}
return rmdir($dir);
}
}