繁体   English   中英

unlink()函数不会删除文件

[英]unlink() function wont't delete a file

这个线程被讨论了很多次,但是并没有解决我的错误...当我尝试使用unlink($ path)函数时,它只是给我一个错误,但是我有权删除它,该文件和路径是正确的,我似乎没有找到错误。

这是我的代码(deleteuser.php):

  <?php
$path = "/thnk.php";
if (!unlink($path)) {
  echo "Error!"; 
} else {
  header("Location: index.php?deletesucces!");
}
?>

和HTML:

<html>
<body>
<form action="/step/deleteuser.php" method="POST">

<button name="submit" type="submit">Delete tha user</button>

</form>

</html>
</body>

PS我对PHP非常陌生,请尝试向新手尽可能多地解释它! 谢谢!

首先检查file available in folder or not是否有file available in folder or not

<?php
$filename = 'you file path';

if (file_exists($filename)) {
    echo "The file $filename exists";
    unlink($filename);
    header("Location: index.php?deletesucces!");
} else {
    echo "The file $filename does not exist";
}
?>

您已经给出了绝对路径,我非常怀疑您将文件放在/thnk.php ,而不是文档根目录中。

您的网络服务器的文档根目录和php文件位于此处:

/var/www/public_html/

当你写unlink('/thnk.php'); 它会在这里看起来:

/thnk.php

但是您希望它从此处删除文件:

/var/www/public_html/thnk.php

那是一个不同的“根”。 所以你必须写

unlink($_SERVER['DOCUMENT_ROOT'] . '/thnk.php');

或者使用类似的相对路径

unlink('../thnk.php'); // which can be unsafe in some situations

如果您不知道错误,但是某些操作不起作用,则仅启用错误报告总是有帮助的:

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM