以下代码不是
<?php
$deletinglist = addQuotes($_POST['delimglist']);
$deletelist = array($deletinglist);
foreach ($deletelist as $filename) {
unlink(dirname(__FILE__) . "/uploads/" . $filename);
}
function addQuotes($string) {
return '"'. implode('","', explode(',', $string)) .'"';
}
?>
此处$_POST['delimglist'] = "C0d49a7de7b635477125ffffa8df7b932.jpg,C0d49a7de7b635477125ffffa8df7b934.jpg,C0d49a7de7b635477125ffffa8df7b935.jpg";
如果我使用$deletelist = array("C0d49a7de7b635477125ffffa8df7b932.jpg","C0d49a7de7b635477125ffffa8df7b934.jpg","C0d49a7de7b635477125ffffa8df7b935.jpg");
它的工作正常,但如果我使用$deletelist = array($deletinglist);
则无效。
尝试使用上述
时,我收到以下警告Warning: unlink(/home/...somepath.../uploads/"C0d49a7de7b635477125ffffa8df7b932.jpg","C0d49a7de7b635477125ffffa8df7b934.jpg","C0d49a7de7b635477125ffffa8df7b935.jpg"): No such file or directory in /home/...somepath.../deletefile.php on line 9
答案 0 :(得分:1)
您所做的只是将$deleteList
作为单个元素放入数组中。您希望将值分隔为&#39;,&#39;。使用$deleteList = explode(',', $deleteList);
答案 1 :(得分:1)
我不确定为什么要在文件名周围添加引号? 代码应该像这样简单:
<?php
$deletelist = explode(',', $_POST['delimglist']);
foreach ($deletelist as $filename) {
unlink(dirname(__FILE__) . "/uploads/" . $filename);
}
?>
答案 2 :(得分:1)
以下是不一样的事情:
$arr1 = Array("a", "b", "c");
$str = "a,b,c";
$arr2 = Array($str);
第一个例子中的逗号是一种语言结构:将它们写入单个变量并不意味着它们神奇地获得语言构造能力;在字符串里面,它们只是字符。
同样,这:
$str = "a,b,c";
foo($str);
与此相同:
foo("a,b,c");
而不是这个:
foo("a", "b", "c");
您必须使用明确拆分字符串$_POST['delimglist']
的函数:
$deleteList = explode(',', $_POST['delimglist']);