“unlink”在本地Wordpress实例上不起作用?

时间:2012-02-02 05:33:05

标签: php wordpress file-permissions unlink

我正在尝试修改插件,以便可以使用html链接删除目录中的图像文件。我的代码吐出一个表格,其中包含图片缩略图,图片链接以及删除文件的链接:

<?php                                                   
   $dirname = "../wp-content/themes/teenclub/images/slider/"; 
   $images = scandir($dirname); 
   $ignore = array(".", "..", ".DS_Store");

   foreach($images as $curimg){ 
       if(!in_array($curimg, $ignore)) {
       echo "<tr ><td><img width='200' src='$dirname$curimg'/></td><td><a href='$dirname$curimg'/>$curimg</a></td><td><a href='../wp-content/plugins/wp-easy-uploader/delete.php?file=$curimg'>Delete</a></td></tr>"; 
       };
   }                         
?>      

delete.php:

<?php
$dir = '/Users/edmcmanwich/Desktop/TEMP/dev.teenclub.com';
$file = $dir.'/'.$_GET["file"];

if(is_writable($file)) {
  unlink($file);
} else {
  echo 'you dont have perms dude';
}
?>

我收到消息说我没有权限,但是我将所有文件chmod到777.另外MAMP的php_error.log给我这个:

[01-Feb-2012 21:10:13] PHP Warning:  unlink(../wp-content/themes/teenclub/images/slider/kids.png) [<a href='function.unlink'>function.unlink</a>]: No such file or directory in /Users/edmcmanwich/Desktop/TEMP/dev.teenclub.com/wp-content/plugins/wp-easy-uploader/delete.php on line 4

目录和文件名是正确的,所以我只是不明白问题是什么......

2 个答案:

答案 0 :(得分:0)

您必须将目录设置错误。

unlink显示的文件位置为../wp-content/themes/teenclub/images/slider/kids.png,但您的目录设置为/Users/edmcmanwich/Desktop/TEMP/dev.teenclub.com。因此,您的完整路径应为/Users/edmcmanwich/Desktop/TEMP/dev.teenclub.com/../wp-content/themes/teenclub/images/slider/kids.png(或根据您的来源/Users/edmcmanwich/Desktop/TEMP/dev.teenclub.com/kids.png),根据您的错误消息情况并非如此。

运行echo getcwd();以查看运行删除脚本的目录,您应该看到文件路径不正确。或者,该文件已被删除,因此不存在。

此外,这是非常不安全的,因为任何人都可以将他们想要的任何内容传递给$_GET['file']并可能删除该文件。例如,如果您使用/etc/passwd上的权限,则有人可以使用../../../../../../../../../../../../etc/passwd将其删除。

答案 1 :(得分:0)

嗯,你已经知道这是不好的做法,但是你运行的问题很可能涉及你使用相对路径。

尝试使用realpath包装文件路径 并看看是否能解决问题。另请注意,如果使用null参数调用realpath,它将返回您肯定不想删除的当前目录。

有关详细信息,请参阅unlinkfile://协议的文档。

相关问题