簡體   English   中英

使用 PHP 的兩個輸入文件上傳兩個文件

[英]Upload two files using two input files with PHP

我有一個 HTML5 表單,旨在通過兩個不同的輸入文件請求訪問者的照片和簡歷(我不想要兩個文件的單個輸入文件)。

該表單適用於具有以下代碼的 CV 或照片的單個輸入文件:

索引.html

<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
  Select file to upload:
  <input type="file" name="CV" id="CV">
  <input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>

上傳.php

<?php
$target_dir = "uploads/";
$CV_target_file = $target_dir . basename($_FILES["CV"]["name"]);
$CVFileType = strtolower(pathinfo($CV_target_file,PATHINFO_EXTENSION));
move_uploaded_file($_FILES["CV"]["tmp_name"], $CV_target_file)
?>

我的問題是一旦有兩個輸入文件,我就無法加載文件。 那么index.html文件的代碼就變成了:

<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
  Select file to upload:
  <input type="file" name="CV" id="CV">
  <input type="file" name="PHOTO" id="PHOTO">
  <input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>

我為兩個輸入文件嘗試的 PHP 代碼:

    <?php
    $target_dir = "uploads/";
    $CV_target_file = $target_dir . basename($_FILES["CV"]["name"]);
    $PHOTO_target_file = $target_dir . basename($_FILES["PHOTO"]["name"]);
    $CVFileType = strtolower(pathinfo($CV_target_file,PATHINFO_EXTENSION));
    $PHOTOFileType = strtolower(pathinfo($PHOTO_target_file,PATHINFO_EXTENSION));
    move_uploaded_file($_FILES["CV"]["tmp_name"], $CV_target_file);
    move_uploaded_file($_FILES["PHOTO"]["tmp_name"], $PHOTO_target_file);
?>

但我找不到正確的 PHP 代碼。 你對我有什么建議嗎? 太感謝了。

接受多個值的文件上傳字段

<input type="file"  name="files" multiple>

只需查看您的 PHP 代碼:

<?php
$target_dir = "uploads/";
$CV_target_file = $target_dir . basename($_FILES["CV"]["name"]);
$PHOTO_target_file = $target_dir . basename($_FILES["PHTO"]["name"]);
                                                  // typo above?
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
                                  // $target_file variable does not exist
move_uploaded_file($_FILES["CV"]["tmp_name"], $Bac_target_file)
                                           // $Bac_target_file does not exist
move_uploaded_file($_FILES["PHOTO"]["tmp_name"], $Bac_target_file)
                                                   // also these last two lines are
                                                   // lacking semicolons
?>

也許,試試這個:

<?php
    $target_dir = "uploads/";
    $CV_target_file = $target_dir . basename($_FILES["CV"]["name"]);
    $PHOTO_target_file = $target_dir . basename($_FILES["PHOTO"]["name"]);
    $imageFileType = strtolower(pathinfo($PHOTO_target_file,PATHINFO_EXTENSION));
    move_uploaded_file($_FILES["CV"]["tmp_name"], $CV_target_file);
    move_uploaded_file($_FILES["PHOTO"]["tmp_name"], $PHOTO_target_file);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM