PHP文件不上传文件

时间:2018-06-30 10:53:05

标签: php html file-upload

我试图制作一个简单的网页,将图像和视频上传到两个不同的文件夹中,但是当我单击“提交”按钮时,php文件将加载并显示空白页面,并且图像和视频未上传到该文件夹​​中。 / p>

这是我的HTML代码:

<!DOCTYPE html>
<html>
<body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        Select an image and video to upload:  <br> 
        Image : 
        <input type="file" name="imgUpload" id="imgUpload" / > <br>
        Video :
        <input type="file" name="vidUpload" id="vidUpload" / > <br><br>
        <input type="submit" value="Upload image and vid" name="submit" / >

    </form>
</body>
</html>

这是我的upload.php文件:

<?php

    if(isset($_POST["submit"])){

        $target_dir = "images/";
        $target_file = $target_dir . basename($_FILES["imgUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

        $target_dir_vid = "videos/";
        $target_file_vid = $target_dir_vid . basename($_FILES["vidUpload"]["name"]);
        $uploadOk_vid = 1;
        $imageFileType_vid = strtolower(pathinfo($target_file_vid,PATHINFO_EXTENSION));

        $check = getimagesize($_FILES["imgUpload"]["tmp_name"]);
        if($check !== false){
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else{
            echo "File is not an image.";
            $uploadOk = 0;  
        }

        $check_vid = getimagesize($_FILES["vidUpload"]["tmp_name"]);
        if($check_vid !== false){
            echo "File is a video - " . $check_vid["mime"] . ".";
            $uploadOk_vid = 1;
        } else{
            echo "File is not a video.";
            $uploadOk_vid = 0;  
        }       

        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" ) {
            echo "Sorry, only jpg, png & jpeg files are allowed.";
            $uploadOk = 0;
        }

        // Allow certain file formats
        if($imageFileType_vid != "mp4" && $imageFileType_vid != "3gp" && $imageFileType_vid != "webm" && $imageFileType_vid != "mkv" ) {
            echo "Sorry, only mp4, 3gp, webm & mkv videos are allowed.";
            $uploadOk_vid = 0;
        }   


        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";

        } else {
            if (move_uploaded_file($_FILES["imgUpload"]["tmp_name"], $target_file)) {
                echo "The file ". basename( $_FILES["imgUpload"]["name"]). " has been uploaded.";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }   


        if ($uploadOk_vid == 0) {
            echo "Sorry, your file was not uploaded.";

        } else {
            if (move_uploaded_file($_FILES["vidUpload"]["tmp_name"], $target_file_vid)) {
                echo "The file ". basename( $_FILES["vidUpload"]["name"]). " has been uploaded.";
            } else {
                echo "Sorry, there was an error uploading your video.";
            }
        }
    }   
?>

最初,它仅适用于图像上传,但是当我为视频添加了其他输入时,它却不起作用。

0 个答案:

没有答案