上传/保存不在PHP中运行的视频

时间:2017-06-06 03:24:46

标签: php video

如果用户输入" 0"在" media_type"文本框,它运行图像上传代码,如果你把" 1"它运行视频上传代码。问题在于视频部分,因为当我上传视频时,它不会显示任何内容。但是当我输入图像并转到图像部分时,它可以正常工作。这是我的代码

PHP

<?php

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

    $media_type = addslashes($_POST['media_type']);

    if($media_type !=0 && $media_type !=1){

        die (json_encode([
        "Status" => " Failed",
        "Message" => " Error 404"
    ]));
        exit;
    }

    if(@getimagesize($_FILES["file"]["tmp_name"])==false){

        echo json_encode([

            "Status" => " Failed",
            "Message" => " No file selected..."
        ]);
        exit;
    }

    if(@getimagesize($_FILES['file']['tmp_name']) ==FALSE || $_POST['user_id'] == "" || $_POST['media_type'] =="" || $_POST['category1'] ==""){

        echo json_encode([
            "Status" => "Failed",
            "Message" => "There were values missing"

        ]);

    die("");
    }


function imageUpload(){
    $userid = addslashes($_POST['user_id']);
    $category1 = addslashes($_POST['category1']);
    $category2 = addslashes($_POST['category2']);
    $category3 = addslashes($_POST['category3']);


//if file is there

    $target = "images/";

    $target_file = addslashes($target . basename($_FILES["file"]["name"]));

    if(file_exists($target_file)){

        echo json_encode([
            "Status" => " Failed",
            "Message" => " Sorry, file already exists..."
        ]);
        exit;
    }
if(move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) 
{

    $db = mysqli_connect("localhost", "root", "");
    mysqli_select_db($db, "magicsever");

    if(mysqli_connect_error()){

        die ("Database connection error");
    }

$image = addslashes($_FILES['file']['tmp_name']);
$name = addslashes($_FILES['file']['name']);    

$sql = "INSERT INTO classified_images (`user_id`,`image`, `img_path`, `img_category_1`,`img_category_2`, `img_category_3`)VALUES('$userid','$image','$name','$category1','$category2','$category3')";
$final = mysqli_query($db, $sql);

echo json_encode([
"Message" => "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.",
"Status" => "OK",
"userId" => $_POST["user_id"]
]);
}else{

echo json_encode([
"Message" => "Sorry, there was an error uploading your file.",
"Status" => "Error",
"userId" => $_POST["user_id"]
]);

}   
}

function videoUpload(){
    $userid = addslashes($_POST['user_id']);
    $category1 = addslashes($_POST['category1']);
    $category2 = addslashes($_POST['category2']);
    $category3 = addslashes($_POST['category3']);
    // Check if file was submited

    $target = "videos/";

    $target_file = addslashes($target . basename($_FILES["file"]["name"]));



    // Check if file already exists
    if(file_exists($target_file)){

        echo json_encode([
            "Status" => " Failed",
            "Message" => " Sorry, file already exists..."
        ]);
        exit;
    }

    // Check file size not > 500Mb
    if($_FILES["file"]["size"] > 500000000){

        echo json_encode([

            "Status" => " Failed",
            "Message" => " Sorry, file is too large."
        ]);
        exit;
    }

        if(move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)){

            $db = mysqli_connect("localhost", "root", "");
    mysqli_select_db($db, "magicsever");

    if(mysqli_connect_error()){

        die ("Database connection error");
    }

    $video = addslashes($_FILES['file']['tmp_name']);
        $name = addslashes($_FILES['file']['name']);

    $sql = "INSERT INTO classified_videos (`user_id`, `vid_path`, `vid_category_1`, `vid_category_2`, `vid_category_3`, `video`)VALUES('$userid','$name','$category1','$category2','$category3','$video')";
    $final = mysqli_query($db, $sql);

            echo json_encode([

                "Status" => " Success",
                "Message" => " The file ". basename($_FILES["file"]["name"]). " has been uploaded.",
                "userId" => $_POST["user_id"]
            ]);
        }else{

            echo json_encode([

                "Status" => " Failed",
                "Message" => " Sorry, there was an error uploaded your file"
            ]);
            exit;
        }

    //Connect to database


}
    if($media_type ==0){

        imageUpload();
    }else if($media_type ==1){

        videoUpload();
    }
}
?>

HTML

    <form method="post" enctype="multipart/form-data">

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

<br></br>
<input type="text" name="user_id" placeholder="User id...">
<br></br>
<input type="text" name="media_type" placeholder="(0)Image...(1)Video">
<br></br>
<input type="text" name="category1" placeholder="MediaTag#1...">
<br></br>
<input type="text" name="category2" placeholder="MediaTag#2...">
<br></br>
<input type="text" name="category3" placeholder="MediaTag#3...">
    <br></br>
<input type="submit" name="submit" value="upload">


</form>

2 个答案:

答案 0 :(得分:1)

如果是视频,您对图片大小的控制总是给出错误(或无意义的值):

if(@getimagesize($_FILES["file"]["tmp_name"])==false){

这就是您收到“未选择文件”消息的原因。 检查您是否正在获取视频或图像,并在正确的情况下运行该控件。

正如您可以在文档getimagesize()中看到的那样:

  

注意   此函数要求filename是有效的图像文件。如果提供了非图像文件,则可能会将其错误地检测为图像,并且该函数将成功返回,但该数组可能包含无意义的值。

答案 1 :(得分:1)

如果您上传的内容超过2MB,图片大小会超过2MB上传大小会增加php.ini文件中“文件上传大小”的大小