duanbi8529 2015-03-11 07:19
浏览 48
已采纳

PHP删除错误

I have a dropdown list populated with files pulled from a directory using the PHP listed below and am trying to figure out how I can delete them using a delete button in the form when they're selected.

EDIT Unlink function error resolved.

Still getting error when trying to delete: Error.array(1) { ["Action"]=> string(6) "Delete" }

<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 value="'. $file .'">' . $file . '</option>';
   }
  }
?>

delete php script.

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

2条回答 默认 最新

  • doulei6778 2015-03-11 09:08
    关注

    You add the options, but no option value. A selectbox should look like this:

    <select name="identifier">
        <option value="value_1">Option 1</option>
        <option value="value_2">Option 2</option>
    </select>
    

    Now if you select option 2, you should get value_2 in your post array:

    echo $_POST['identifier']; // prints out 'value_2';
    

    Your options look like: <option>Option x</option>, which means you select an option, but it has no value (no value="" attribute).

    If you look at your PHP error, I do see a correct directory name, but there is no filename appended.

    You should also check if the file exists, to prevent a "no such file" error:

    if(is_file($directory .'/'. $file_to_be_deleted) && is_writable($directory .'/'. $file_to_be_deleted)) {
        // delete the file
    }
    

    Yes, I use the is_file AND the is_writable test, because the is_writable function will also return true if the checked path is a directory.

    EDIT

    To assign values, alter your foreach loop as follows:

    <select size="1" name="CodeList" id="CodeList">
        <?php
        // fetch files etc.
    
        foreach( $files as $file ) {
            if ( ! is_dir( $file ) {
                echo '<option value="'. $file .'">' . $file . '</option>';
            }
        }
    

    Oh and btw: get rid of the get_file_contents() part. You don't do anything with the contents of the file there, do you?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来