-1

I got my first study task to make a script. Task is the following: Create a webform for uploading files, after file upload they are displayed on the same page in html table. This is my very first task and my priority is just to make it work. So I created a form and function to scan upload folder for files and then put them in array. Afterwards array is used in a cucle that builds up an html table. Please help me to build correct delete button, as I just can't get it to work. My code is here:

<?php
if ($_GET['action']=='delete' && isset($_GET['file'])) {
        unlink($dir.$filelist[$i]);
        echo "File ".$filelist[$i]. " has been removed";
}
 
 
//table with files
echo"<table border=1 cellpading=5 cellspasing=0>
<tr><th>#</th><th>Filename</th><th>Path</th><th>File extenstion</th><TH>File Size<th>Remove file?</th></tr>";
for ($i=0; $i <count($fileslist) ; $i++) {
        echo "<tr>";
        echo"<td>$i</td><td>". $fileslist[$i]."</td>";
        echo "<td><a href=".$dir.$fileslist[$i].">".$fileslist[$i]."</a></td>";
        echo "<td>".$ext=pathinfo(($dir.$fileslist[$i]), PATHINFO_EXTENSION)."</td>";
        echo "<td>".$size= filesize_get($dir.$fileslist[$i])."</td>";
        echo "<td> <a href=\"upload.php?action=delete&file=".$filelist[$i].">Delete </a></td>";
        echo "<tr>";
}
echo"</tabe>";

Get following errors:

Notice: Undefined index: action in D:\PHP(1)\xampp\htdocs\upload.php on line 32

Notice: Undefined variable: filelist in D:\PHP(1)\xampp\htdocs\upload.php on line 47

4

3 回答 3

1

代替

echo "<td> <a href=\"upload.php?action=delete&file=".$filelist[$i].">Delete </a></td>";

经过

echo "<td> <a href=\"upload.php?action=delete&file=".$fileslist[$i].">Delete </a></td>";

您只是缺少文件列表中的 s

于 2012-12-24T16:24:46.340 回答
1

我认为您应该从字面上理解该错误消息。$filelist您提供的代码示例中未定义您的变量。

$_GET['action'] == 'deleted'此外,您的第一个错误也被抛出,因为即使该参数未通过 GET 传递,您也会尝试检查。在使用它之前,您应该始终检查参数是否存在,如下所示:

if (isset($_GET['action']) && $_GET['action']=='delete' && isset($_GET['file'])) {
于 2012-12-24T16:26:42.990 回答
0

1-当 $i 尚未定义时,您正在删除第 i 个项目

2-如上述答案所述

为防止变量和函数名称出错,您可以使用支持代码完成的文本编辑器。例如,一个可以提供帮助的非常轻量级的工具是Geany。即使在 linux 和 windows 上的大型项目中,我仍然使用它。它不会使用大量 RAM 和 CPU,也不会花时间打开。

在 AND (&&) 条件块中,条件是从左到右读取的,一旦出现不正确的情况,该块就会中断,因此您更有可能首先测试关键的东西,这样它就不会在堵塞。

于 2012-12-24T16:49:31.737 回答