Jagr
Jagr

Reputation: 502

Uploading/Saving videos not working in PHP

If a user inputs "0" in the "media_type" text box, it runs the image upload code and if you put "1" it runs the video upload code. The problem is with the video section because when i upload a video, it doesn't display anything. But when i input an image, and go to the image section, it works. Here is my code

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>

Upvotes: 0

Views: 145

Answers (2)

swapnil kambe
swapnil kambe

Reputation: 123

The size of images cross 2MB upload size if you have upload more than 2MB data increase the size "File upload size " from php.ini file

Upvotes: 1

user3380963
user3380963

Reputation:

In case of video, your control for image size is always giving false (or nonsensical values):

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

That is why you are receiving the "No file selected" message. Check if you are getting video or images and run that control just in the right case.

As you can read in the documentation getimagesize() :

Caution This function expects filename to be a valid image file. If a non-image file is supplied, it may be incorrectly detected as an image and the function will return successfully, but the array may contain nonsensical values.

Upvotes: 1

Related Questions