1

我的代码有这个奇怪的问题:

共有三个输入file标签。这些是上传场景及其结果:

  1. 同时所有三个文件 - 成功
  2. 只有图片 - 成功
  3. 只有音频 - 失败
  4. 只有视频 - 失败
  5. 一图一音——图成功,音频失败
  6. 一图一视频——图片成功,视频失败
  7. 一个音频和一个视频 - 其中一个成功

    <?php 
    session_start(); 
    if(!isset($_SESSION["username"])){
        header('Location: index.php');
    }
    if(isset($_POST["year"])){
        ini_set('display_errors',1);
        error_reporting(E_ALL);
        var_dump($_FILES);
        $year = $_POST["year"];
        $height = $_POST["height"];
        $weight = $_POST["weight"];
        $school = $_POST["school"];
        $class = $_POST["class"];
        $target_dir_img = "./pictures/";
        $target_dir_aud = "./audios/";
        $target_dir_vid = "./videos/";
    
        $uploadOk = 1;
    
        $target_files = array();
    
        $target_img = basename($_FILES['uploadedFiles']['name'][0]);
        var_dump($target_img);
        if(strcmp($target_img,'')){
            $imageFileType = pathinfo($target_img,PATHINFO_EXTENSION);
    
            $newImageName = $target_dir_img . $_SESSION['username']."_".$_POST['year'].".". $imageFileType;
    
            if($imageFileType != "jpg" && $imageFileType != "jpeg") {
                echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                $uploadOk = 0;
            }
            else {
                $target_files[] = $newImageName;
            }
        }
    
        // Allow certain file formats
    
        $target_aud = basename($_FILES['uploadedFiles']['name'][1]);
        var_dump($target_aud);
        if(strcmp($target_aud,'')) {
    
            $audioFileType = pathinfo($target_aud, PATHINFO_EXTENSION);
    
            $newAudioName = $target_dir_aud . $_SESSION['username']."_".$_POST['year'].".". $audioFileType;
    
            if($audioFileType != "mp3") {
                echo "Sorry, only MP3 files are allowed.";
                $uploadOk = 0;
            }
            else {
                $target_files[] = $newAudioName;
            }
        }
    
        $target_vid = basename($_FILES['uploadedFiles']['name'][2]);
        var_dump($target_vid);
        if(strcmp($target_vid,'')){
    
            $videoFileType = pathinfo($target_vid, PATHINFO_EXTENSION);
    
            $newVideoName = $target_dir_vid . $_SESSION['username']."_".$_POST['year'].".". $videoFileType;
    
            if($videoFileType != "mp4") {
                echo "Sorry, only MP4 files are allowed.";
                $uploadOk = 0;
            }
            else {
                $target_files[] = $newVideoName;
            }
        }
    
        var_dump($target_files);
    
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";
        // if everything is ok, try to upload file
        } else {
            for($i=0; $i<sizeof($target_files); $i++){
                if (move_uploaded_file($_FILES['uploadedFiles']['tmp_name'][$i], $target_files[$i])) {
                    echo "The file ". basename( $_FILES['uploadedFiles']['name'][$i]). " has been uploaded.";
                } else {
                    echo "Sorry, there was an error uploading your file.";
                }
            }
        }
    }
    ?>
    
    
    <h1>Welcome <?php echo $_SESSION["username"]; ?>!</h1>
    
    <h2>Data</h2>
    <form action="" method="POST" enctype="multipart/form-data">
        <p><label>Select Year : </label>
        <select id="year" name="year">
            <?php 
                for($i=1901; $i<=2020; $i++){
                    echo '<option value="'.$i.'">'.$i.'</option>';
                }
            ?>
        </select></p>
        <p><label>Height : </label>
        <input type="text" id="height" name="height" placeholder="Height" /></p>
        <p><label>Weight : </label>
        <input type="text" id="weight" name="weight" placeholder="Weight" /></p>
        <p><label>School : </label>
        <input type="text" id="school" name="school" placeholder="School" /></p>
        <p><label>Class : </label>
        <input type="text" id="class" name="class" placeholder="Class" /></p>
        <p><label>Choose picture : </label>
        <input type="file" id="picture" name="uploadedFiles[]" accept="image/jpeg, image/jpg" /></p>
        <p><label>Choose Audio : </label>
        <input type="file" id="audio" name="uploadedFiles[]" accept="audio/mp3" /></p>
        <p><label>Choose Video : </label>
        <input type="file" id="video" name="uploadedFiles[]" accept="video/mp4" /></p>
    
        <input type="submit" id="submit" name="submit" value="submit" /></p>
    </form>
    

关于它如何发生的任何输入?

4

0 回答 0