【问题标题】:upload mutiple selected files to web server with php使用php将多个选定的文件上传到Web服务器
【发布时间】:2012-01-29 11:25:29
【问题描述】:

我有一个 php 表单,它允许用户提供参考编号,然后选择要上传的 pdf 文件。

表单有多个选择,因此我希望能够用一个表单上传多个文件。

我的表单代码如下:

<form enctype="multipart/form-data" method="post" action="capturelocal2.php">
<table id="hor-minimalist-a" border=0>
<tr><th><font color=red><b>Drop 1</b></font></th></tr>
<tr id="r0">
<th valign=bottom>Haulier Delivery #</th>
<th valign=bottom>Hulamin Load # A</th>
<th valign=bottom>Load # A POD</th>
<th valign=bottom>Hulamin Load # B</th>
<th valign=bottom>Load # B POD</th>
<th valign=bottom>Customer</th>
<th valign=bottom>Arrive Time</th>
<th valign=bottom>Depart Time</th>
</tr>
<tr id="r1">
<td valign=bottom  width=50><input type=text name=drop1del id=drop1del size=12></td>
<td valign=bottom  width=50><input type=text name=ref1 id=ref1 size=12></td>
<td valign=bottom  width=50><input type=file size=6 name=ref1pod id=ref1pod></td>
<td valign=bottom  width=50><input type=text name=ref1b id=ref1b size=12></td>
<td valign=bottom  width=50><input type=file size=6 name=ref1bpod id=ref1bpod></td>
<td>
    <SELECT NAME="drop1" id="drop1"  style="width:200px;" > 
    <OPTION VALUE=0>
    <?=$optionscustomers?> 
    </SELECT> 
</td>
<td><input type=text name="drop1arrivedatetime" id="drop1arrivedatetime" size=15><img src="images2/cal.gif" onclick="javascript:NewCssCal ('drop1arrivedatetime','yyyyMMdd','dropdown',true,'24')"  style="cursor:pointer"/></td>
<td><input type=text name="drop1departdatetime" id="drop1departdatetime" size=15><img src="images2/cal.gif" onclick="javascript:NewCssCal ('drop1departdatetime','yyyyMMdd','dropdown',true,'24')"  style="cursor:pointer"/></td>
</tr>
<tr><th><font color=red><b>Drop 2</b></font></th></tr>
<tr id="r2">
<th valign=bottom>Haulier Delivery #</th>
<th valign=bottom>Hulamin Load # A</th>
<th valign=bottom>Load # A POD</th>
<th valign=bottom>Hulamin Load # B</th>
<th valign=bottom>Load # B POD</th>
<th valign=bottom>Customer</th>
<th valign=bottom>Arrive Time</th>
<th valign=bottom>Depart Time</th>
</tr>
<tr>
<tr id="r3">
<td valign=bottom><input type=text name=drop2del id=drop2del size=12></td>
<td valign=bottom  width=50><input type=text name=ref2 id=ref2 size=12></td>
<td valign=bottom  width=50><input type=file size=5 name=ref2pod id=ref2pod></td>
<td valign=bottom  width=50><input type=text name=ref2b id=ref2b size=12></td>
<td valign=bottom  width=50><input type=file size=5 name=ref2bpod id=ref2bpod></td>
<td>
    <SELECT NAME="drop2" id="drop2" style="width:200px;"  > 
    <OPTION VALUE=0>
    <?=$optionscustomers?> 
    </SELECT> 
</td>
<td><input type=text name="drop2arrivedatetime" id="drop2arrivedatetime" size=15><img src="images2/cal.gif" onclick="javascript:NewCssCal ('drop2arrivedatetime','yyyyMMdd','dropdown',true,'24')"  style="cursor:pointer"/></td>
<td><input type=text name="drop2departdatetime" id="drop2departdatetime" size=15><img src="images2/cal.gif" onclick="javascript:NewCssCal ('drop2departdatetime','yyyyMMdd','dropdown',true,'24')"  style="cursor:pointer"/></td>
</tr>
    </table>
    </form>

我处理表单的 PHP 代码是:

    $ref1 = $_POST[ref1];
    $ref1pod = $_POST[ref1pod];
    $ref1bpod = $_POST[ref1bpod];
    $ref2 = $_POST[ref2];
    $ref2pod = $_POST[ref2pod];
    $ref2bpod = $_POST[ref2bpod];

    if (isset(ref1pod) || ($_FILES["ref1pod"]["type"] == "application/pdf")
      || ($_FILES["ref1pod"]["type"] == "image/jpeg")
      && ($_FILES["ref1pod"]["size"] < 50000))
      {
      move_uploaded_file($_FILES["ref1pod"]["tmp_name"],
        "/home/hulaminyxr/public_html/pod/" . ($ref1pod.".pdf"));
        echo "POD Successfully uploaded for delivery $ref1pod:<br><br>"; 
        }

    if (isset($ref1bpod) ||($_FILES["ref1bpod"]["type"] == "application/pdf")
      || ($_FILES["ref1bpod"]["type"] == "image/jpeg")
      && ($_FILES["ref1bpod"]["size"] < 50000))
      {
      move_uploaded_file($_FILES["ref1bpod"]["tmp_name"],
        "/home/hulaminyxr/public_html/pod/" . ($ref1bpod.".pdf"));
        echo "POD Successfully uploaded for delivery $ref1bpod:<br><br>"; 
    }

 if (isset(ref2pod) || ($_FILES["ref2pod"]["type"] == "application/pdf")
      || ($_FILES["ref2pod"]["type"] == "image/jpeg")
      && ($_FILES["ref2pod"]["size"] < 50000))
      {
      move_uploaded_file($_FILES["ref2pod"]["tmp_name"],
        "/home/hulaminyxr/public_html/pod/" . ($ref2.".pdf"));
        echo "POD Successfully uploaded for delivery $ref2:<br><br>"; 
        }

    if (isset($ref2bpod) ||($_FILES["ref2bpod"]["type"] == "application/pdf")
      || ($_FILES["ref2bpod"]["type"] == "image/jpeg")
      && ($_FILES["ref2bpod"]["size"] < 50000))
      {
      move_uploaded_file($_FILES["ref2bpod"]["tmp_name"],
        "/home/hulaminyxr/public_html/pod/" . ($ref2b.".pdf"));
        echo "POD Successfully uploaded for delivery $ref2bpod:<br><br>"; 
    }  


    echo "<br><br>All files uploaded successfully";

目前,在提交表单时,没有正确上传 pdf 文件?我的代码是正确的还是多个文件不可能做到这一点?

另外,我的表格有10行,我只提供了1行的代码,有没有更有效的方法来上传多个选定的文件?

谢谢, 瑞恩

【问题讨论】:

    标签: php forms file upload


    【解决方案1】:
    1. 可以上传多个文件。请阅读PHP manual for uploading multiple files
    2. 如果您的代码不起作用,请说明问题所在。
    3. 您的代码正在将 jpeg 文件保存为 pdf。请参阅下面的我的 cmets:

      # If file type is "pdf" or "jpeg" save it...
      if (isset(ref1pod) || ($_FILES["ref1pod"]["type"] == "application/pdf")
            || ($_FILES["ref1pod"]["type"] == "image/jpeg")
            && ($_FILES["ref1pod"]["size"] < 50000))
            {
      
        # .. with .pdf extension
        move_uploaded_file($_FILES["ref1pod"]["tmp_name"],
          "/home/hulaminyxr/public_html/pod/" . ($ref1pod.".pdf"));
      

      `

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      相关资源
      最近更新 更多