duanbeng6709 2012-09-29 14:51
浏览 43
已采纳

PHP上传具有不同文件名但公共文件夹的多个文件

I'm trying to upload multiple files at once but have a solo form field to define the name of the column that will define the gallery name... I tried different scripts and methods but the only one I've been successful with is this one, except I have to type the name of the gallery in each one, is there a way to do this?

Here's my HTML form

    <form action="php_multiple_upload2.php" method="post" name="form1" enctype="multipart/form-data">
        <input type="text" name="galnom1"><input type="file" name="fileUpload1" ><br>
        <input type="text" name="galnom2"><input type="file" name="fileUpload2"><br>
        <input type="text" name="galnom3"><input type="file" name="fileUpload3"><br>
        <input type="text" name="galnom4"><input type="file" name="fileUpload4"><br>
        <input name="hdnLine" type="hidden" value="4">
        <input name="btnSubmit" type="submit" value="Submit">
    </form>

Here's the PHP Code

    <?php
    include("../../cnf/cnctr.php");

    for($i=1;$i<=(int)($_POST["hdnLine"]);$i++)
    {
    if($_FILES["fileUpload".$i]["name"] != "")
      {
      if(copy($_FILES["fileUpload".$i]["tmp_name"],"img/".$_FILES["fileUpload".$i]["name"]))
        {
          $strSQL = "INSERT INTO galeria ";
          $strSQL .="(galnom,ttl) VALUES ('".$_POST["galnom".$i]."','".$_FILES["fileUpload".$i]["name"]."')";
          mysqli_query($fsn,$strSQL);
          echo "Copy/Upload ".$_FILES["fileUpload".$i]["name"]." completed.<br>";
        }
      }
    }
    echo "<br><a href='php_multiple_upload6.php'>View file</a>";
    mysqli_close($fsn);
    ?>

This is how I envision my html form:

    <form action="php_multiple_upload2.php" method="post" name="form1" enctype="multipart/form-data">
      <input type="text" name="gallery_name"><br> <!-- Just one field to name the gallery -->
      <input type="file" name="fileUpload1" ><br>
      <input type="file" name="fileUpload2"><br>
      <input type="file" name="fileUpload3"><br>
      <input type="file" name="fileUpload4"><br>
      <input name="hdnLine" type="hidden" value="4">
      <input name="btnSubmit" type="submit" value="Submit">
    </form>

As always thanks for the help :)

Best Regards

  • 写回答

1条回答 默认 最新

  • dtc9222 2012-09-29 15:10
    关注

    You could use the form like you envisioned it.

    Just swap the line

    $strSQL .="(galnom,ttl) VALUES ('".$_POST["galnom".$i]."','".$_FILES["fileUpload".$i]["name"]."')";
    

    with this line:

    $strSQL .="(galnom,ttl) VALUES ('".$_POST["gallery_name"]."','".$_FILES["fileUpload".$i]["name"]."')";
    

    As a side note just in case: be aware that your SQL code is completely unsecured against SQL-injections

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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