Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从下拉列表中删除文件_Php_Html - Fatal编程技术网

PHP从下拉列表中删除文件

PHP从下拉列表中删除文件,php,html,Php,Html,我有一个下拉列表,其中填充了使用下面列出的PHP从目录中提取的文件,我正试图找出如何在选中这些文件时使用表单中的删除按钮删除它们 <input type="hidden" name="Action" value="EDIT" /><input type="hidden" name="Selection" id="Selection" value="-1"><div>Below is the list of your saved codes. To edit

我有一个下拉列表,其中填充了使用下面列出的PHP从目录中提取的文件,我正试图找出如何在选中这些文件时使用表单中的删除按钮删除它们

<input type="hidden" name="Action" value="EDIT" /><input type="hidden" name="Selection"  id="Selection" value="-1"><div>Below is the list of your saved codes. To edit your codes, select it from the list.</div>
<select size="1" name="CodeList" id="CodeList">
<?php
   $directory = $directory = 'users/' . $_SESSION['username'];
   $filesContents = Array();
   $files = scandir( $directory ) ;

   foreach( $files as $file )
  {
   if ( ! is_dir( $file ) )
  {
   $filesContents[$file] = file_get_contents($directory , $file);
  echo "<option>" . $file . "</option>";
  }
   }
   ?>
   </select>
下面是您保存的代码列表。要编辑代码,请从列表中选择它。
在运行时更新代码

<?php
   session_start();
    $directory = $directory = $_SERVER['DOCUMENT_ROOT'] . '/users/' . $_SESSION['username'];
    $file_to_delete = $_POST['CodeList'];
    if ( unlink ($directory.'/'.$file_to_delete) ) {
      echo $file_to_delete . " deleted.";
   } else {
echo "Error.";
}
?>

编辑和更新的代码

现在说警告:取消链接(/myhome/root/public_html/users/addicient/)[function.unlink]:第5行的/home/revo/public_html/evo/avdeleteprocess.php中没有这样的文件或目录 错误

所以它找到了正确的文件夹,只是没有从下拉列表选择中找到文件


也进行了编辑以添加我在删除脚本之后运行了一个var_dump($\u POST),查看它作为POST的值提取了什么,结果返回:Error.array(1){[“Action”]=>string(6)“delete”}

您的表单将不会使用

<button type="button">Delete</button>
删除
试一试

删除

您需要用
表单
标签包装代码。另外,$\u POST需要获取您的select的名称(在本例中),因此:

form.php:

<form action="avdeleteprocess.php" method="post">
<select size="1" name="CodeList" id="CodeList">
<?php
   $directory = 'users/' . $_SESSION['username'];
   $filesContents = Array();
   $files = scandir( $directory ) ;

   foreach( $files as $file )
  {
   if ( ! is_dir( $file ) )
  {
   $filesContents[$file] = file_get_contents($directory , $file);
  echo "<option value='".$file."'>" . $file . "</option>";
  }
   }
   ?>
   </select>
   <input type="submit" name="Action" value="DELETE" />
</form>
<?php
  session_start();
  $directory = 'users/'.$_SESSION['username'].'/';
  //here you can even check if user selected 'delete' option:
  if($_POST['Action'] == "DELETE"){
      $file_to_delete = $_POST['CodeList'];
      if(unlink($directory.'/'.$file_to_delete))
         echo $file_to_delete." deleted.";
      else
         echo "Error deleting file ".$file_to_delete;
  }
  if($_POST['Action'] == "SAVE"){
      ...
  }
?>


我完全没有注意到这一点,谢谢,我不确定我的php从下拉列表中获取要删除的正确文件是否正确,但很快就会进行测试。事实上,它甚至没有找到创建会话的地方,显然,它只是在查找用户时停止了/我不确定如何让它在下拉列表中找到正确的文件夹,然后找到正确的文件;以防万一,在我的php脚本顶部,它将错误更改为:Warning:unlink(/)[function.unlink]:是第6行error.Right上/home/revo/public_html/evo/avdeleteprocess.php中的目录。我之前的意思是,如果不创建会话用户名,比如:
$\u session['username']=1,它将始终返回empty.Right。这就是为什么它找不到它的原因。现在我只需要弄清楚为什么它找不到文件。改变了这一点,它仍然告诉我:警告:unlink(users/addicity/)[function.unlink]:是/home/revo/public\u html/evo/avdeleteprocess.php中第6行的一个目录错误。您的“删除”按钮在哪里?您是否将代码包装到
表单
标记中?是的,删除按钮位于主表单下方,这可能是问题所在。您可以在这里看到所有更新的代码:似乎我们错过了
选项
中的文件值。看看我更新的代码。另外,当你运行代码时,文件名会出现在“选择”下拉列表中?我在小提琴中更新的代码中更改了它。我将尝试使用该表单中的delete按钮和delete代码,看看它是否有效,但是表单在输入信息时也用于保存文件(下拉菜单从相同的文件中提取),不确定是否可以在同一表单上完成。如果没有,我可以创建两个页面和表单。我们将对此进行测试。
<?php
  session_start();
  $directory = 'users/'.$_SESSION['username'].'/';
  //here you can even check if user selected 'delete' option:
  if($_POST['Action'] == "DELETE"){
      $file_to_delete = $_POST['CodeList'];
      if(unlink($directory.'/'.$file_to_delete))
         echo $file_to_delete." deleted.";
      else
         echo "Error deleting file ".$file_to_delete;
  }
  if($_POST['Action'] == "SAVE"){
      ...
  }
?>