dongxing5525 2019-06-11 16:50
浏览 40

如何定义'。$ file。' 作为关键的POST数组

i had a problem with my php script. im using php to show all my picture in folder "gambar" and then i want to add button command in every picture i've showed to other folder i already make. im using POST syntax and had trouble with it. button show up but not executing command to moving file i've choose. this is script i've use, could somebody help me with this?

    <?php

    $folder = "gambar"; //folder tempat gambar disimpan
    $handle = opendir($folder);
    echo '<table cellspacing="5" cellpadding="10" width="90%" >';
    echo '<tr>';
    $i = 1;
    while(false !== ($file = readdir($handle))){
    if($file != '.' && $file != '..'){
    echo '<td style="border:1px solid #000000;" align="center" >

      <div class="hvrbox">
      <img src="gambar/'.$file.'" alt="Mountains" class="hvrbox-layer_bottom" 
      width="100%" />
      <div class="hvrbox-layer_top">
      <div class="hvrbox-text"> '.$file.'.</div>
      </div>
      </div>
      <script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
      <script type="text/javascript" src="js/hover-box.js"></script>    
            </select>
            <form method="post">
            <input type="hidden" select name="datae" value="'.$file.'" /> 

            >>> <input type="submit" name="upload" value="Upload" />
            --- 
            <input type="submit" name="delete" value="Delete" /><<<
            </form>
            <br /><b>NAMA FILE:</b>   '.$file.'</td>';

      if(($i % 1) == 0){
      echo '</tr><tr>';
      }
      $i++;
      }
      }
      echo '</tr>';
      echo '</table>';
      ?>

and to progress POST im using this script

            <?php
            // Turn off all error reporting
            error_reporting(0);

            if (isset($_POST['upload']) && isset($_POST['datae'])) {
            rename('gambar/'.$_POST['datae'], 'Upload/'.$_POST['datae']);
            echo "<meta http-equiv='refresh' content='1'>";
            }

            if (isset($_POST['delete']) && isset($_POST['datae'])) {
            rename('gambar/'.$_POST['datae'], 'delete/'.$_POST['datae']);
            echo "<meta http-equiv='refresh' content='1'>";
            }
            ?>

cz when i use

      <form method="post">
      <input type="hidden" select name="datae" value="'.$file.'" />

so did

     ($_POST['datae']

still not moving the files to other folder, its like the script was working but no files moved to destination folder.

PS. im sorry cz my English is BAD. but i wish you could understand what i mean.

  • 写回答

1条回答 默认 最新

  • donglu1881 2019-06-11 17:37
    关注

    You forgot to include the <form>. So when you click your "submit" button there's nothing to submit. Wrap a form element around your buttons and include an input for the file name:

    echo '
        ...
        <form method="post">
            <input type="hidden" name="file" value="'.$file.'" />
            >>> <input type="submit" name="upload" value="Upload" />
            --- 
            --- <input type="submit" name="delete" value="Delete" /><<<
        </form>
        ...';
    

    Also in your server-side code you are incorrectly getting a value from the POST array:

    $_POST['.$file.']
    

    The syntax is broken, and it appears that you're trying to use the file name itself as the key in the POST array. Posted data is in key/value pairs. You want to use the name of the value in order to get the value. Notice the name attribute in my hidden input above. That's what you'd use:

    $_POST['file']
    

    A few important things to note which are outside the scope of the question being asked but definitely relevant:

    1. If your server-side code is on a different page then you'll need to specify that in the action attribute of the <form> element and handle going back and forth between the pages.
    2. If your server-side code is on the same page but the data is modified after the output is rendered then you won't see updates until you reload the page again. Perform the requested actions before building the output.
    3. WARNING: You are allowing users to specify the file name that they want to modify on your server. Users can abuse this access. You're going to want to add some security to make sure that user is permitted to modify that file. As it stands there's nothing stopping a user from manually specifying important system files and your code will blindly move/delete/etc. those files, which can corrupt your server. In general it's best to store records of your files in a database and use an identifier from the database for user interaction, so the user never knows or has any interaction with the file system but rather just specifies IDs. This also allows you to control permissions by associating certain files with certain users, etc.
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?