我试图使用PHP unlink()
函数删除文件夹中的特定文档。该特定文件夹已被分配给IIS用户的完整权限。如何使用Unlink()函数
代码:
$Path = './doc/stuffs/sample.docx';
if (unlink($Path)) {
echo "success";
} else {
echo "fail";
}
保持它的回报失败。 sample.docx确实驻留在该特定路径上。好心提醒。
我试图使用PHP unlink()
函数删除文件夹中的特定文档。该特定文件夹已被分配给IIS用户的完整权限。如何使用Unlink()函数
代码:
$Path = './doc/stuffs/sample.docx';
if (unlink($Path)) {
echo "success";
} else {
echo "fail";
}
保持它的回报失败。 sample.docx确实驻留在该特定路径上。好心提醒。
我发现这个信息in the comments of the function unlink()
在Windows系统和Apache,拒绝访问文件是平常 错误断开链接文件。要删除文件,您必须更改文件的所有者。 一个例子:
chown($tempDirectory . '/' . $fileName, 666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody"
unlink($tempDirectory . '/' . $fileName);
因此,尝试这样的事:
$path = './doc/stuffs/sample.docx';
chown($path, 666);
if (unlink($path)) {
echo 'success';
} else {
echo 'fail';
}
编辑1
尝试的路径,使用此:
$path = '.'
. DIRECTORY_SEPARATOR . 'doc'
. DIRECTORY_SEPARATOR . 'stuffs'
. DIRECTORY_SEPARATOR . 'sample.docx';
试试这个:
$Path = './doc/stuffs/sample.docx';
if (file_exists($Path)){
if (unlink($Path)) {
echo "success";
} else {
echo "fail";
}
} else {
echo "file does not exist";
}
如果你得到的文件不存在,你走错了路。如果不是,它可能是一个权限问题。
define("BASE_URL", DIRECTORY_SEPARATOR . "book" . DIRECTORY_SEPARATOR);
define("ROOT_PATH", $_SERVER['DOCUMENT_ROOT'] . BASE_URL);
$path = "doc/stuffs/sample.docx";
if (unlink(ROOT_PATH . $Path)) {
echo "success";
} else {
echo "fail";
}
// http://localhost/book/doc/stuffs/sample.docx
// C:/xampp/htdocs\book\doc/stuffs/sample.docx
你检查,如果你有权限删除的文件?另外,尝试使用绝对路径,如下所示:'$ Path ='/ doc/stuffs/sample.docx'' – 2012-07-13 03:05:17
yes。我可以下载那个特定的文件,这样我的路径就可以工作了。 – JLearner 2012-07-13 03:07:28
如果你确定权限是正确的,我猜测路径是错误的,就像马西奥所说的那样。你有没有试过用'file_exists()'检查它? – SilverSnake 2012-07-13 03:08:03