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 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输