简体   繁体   English

unlink()函数不会删除文件

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

This thread was discussed many times but it didn't fix my error... When I try to use unlink($path) function it just gives me an error, however I have permissions to delete it, the file and the path is correct, I just don't seem to find an error. 这个线程被讨论了很多次,但是并没有解决我的错误...当我尝试使用unlink($ path)函数时,它只是给我一个错误,但是我有权删除它,该文件和路径是正确的,我似乎没有找到错误。

Here is my code (deleteuser.php): 这是我的代码(deleteuser.php):

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

And HTML: 和HTML:

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

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

</form>

</html>
</body>

PS I am very new to PHP, try to explain it as much understandable for newbies as possible! PS我对PHP非常陌生,请尝试向新手尽可能多地解释它! Thanks! 谢谢!

first check the file available in folder or not 首先检查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";
}
?>

You have given an absolute path and I highly doubt you put the file in /thnk.php , rather than your document root. 您已经给出了绝对路径,我非常怀疑您将文件放在/thnk.php ,而不是文档根目录中。

Example

Your webserver's document root and your php files are here: 您的网络服务器的文档根目录和php文件位于此处:

/var/www/public_html/

When you write unlink('/thnk.php'); 当你写unlink('/thnk.php'); it will look here: 它会在这里看起来:

/thnk.php

But you want it to delete the file from here: 但是您希望它从此处删除文件:

/var/www/public_html/thnk.php

That's a different "root". 那是一个不同的“根”。 So you have to write 所以你必须写

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

Or use a relative path like 或者使用类似的相对路径

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

If you don't know an error but something does not work, it is always helpful to simply enable error reporting: 如果您不知道错误,但是某些操作不起作用,则仅启用错误报告总是有帮助的:

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

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

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