doushi4864
2014-11-29 11:28
采纳率: 100%
浏览 125
已采纳

在php上传视频和图片

i am creating a blog where on admin side i can upload images and videos, and on user side i can access them. now i want to store images on different directory and videos on different directory, but the problem is that how can i create a logic when the file is selected then the code knows that it is image or video? i try...

<?php
session_start();
include 'conn.php';
$title=$_POST['title'];
$post=$_POST['post'];
$tag=$_POST['tag'];
$cat='some cat';
$file_name=$_FILES['fileToUpload']['name'];
$file_size=$_FILES['fileToUpload']['size'];
$file_height=200;
$file_width=100;
$duration=24.00;
$target_dir_image = "../posts/images/";
$target_dir_video = "../posts/videos/";
$target_file_image = $target_dir_image . basename($_FILES["fileToUpload"]["name"]);
$target_file_video = $target_dir_video . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;

$imageFileType = pathinfo($target_file_image,PATHINFO_EXTENSION);
$videoFileType = pathinfo($target_file_video,PATHINFO_EXTENSION);

    if ($imageFileType == "jpg" && $imageFileType == "png" && $imageFileType == "jpeg" && $imageFileType == "gif") {
        $uploadOk = 0;
    }

// Check if file already exists
        if (file_exists($target_file_image)) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
// Check file size
        if ($_FILES["fileToUpload"]["size"] > 50000000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }
// Allow certain file formats

// Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            $_SESSION['error'] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            header('location:new-post.php');
// if everything is ok, try to upload file
        }
    else {
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_image)) {
                $conn->beginTransaction();
                $conn->exec("INSERT INTO post(Title,Post,Category,Tag,Post_Date)VALUES ('" . $title . "','" . $post . "','" . $cat . "','" . $tag . "',now())");
                $conn->exec("INSERT INTO images(Image_Name,Image_Size,Image_Width,Image_height,Image_Directory)VALUES ('" . $file_name . "','" . $file_size . "','" . $file_width . "','" . $file_height . "','" . $target_file_image . "')");
                $conn->commit();
                $_SESSION['success'] = 'Post has been successfuly published';
                header('location:new-post.php');
                $conn->rollBack();
                $_SESSION['error'] = 'Fail to publish the post';
                header('location:new-post.php');


            }

    }
if ($videoFileType == "mp4" && $imageFileType == "flv" && $imageFileType == "mpeg" && $imageFileType == "avi")
{
    $uploadOk = 0;
}

// Check if file already exists
        if (file_exists($target_file_video)) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
// Check file size
        if ($_FILES["fileToUpload"]["size"] > 50000000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }
// Allow certain file formats

// Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            $_SESSION['error'] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            header('location:new-post.php');
// if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_video)) {
                $conn->beginTransaction();
                $conn->exec("INSERT INTO post(Title,Post,Category,Tag,Post_Date)VALUES ('" . $title . "','" . $post . "','" . $cat . "','" . $tag . "',now())");
                $conn->exec("INSERT INTO videos(Video_Name,Video_Size,Video_Duration,Video_Dimension,Video_Directory)VALUES ('" . $file_name . "','" . $file_size . "','".$duration."','" . $file_width . "','" . $target_file_video . "')");
                $conn->commit();
                $_SESSION['success'] = 'Post has been successfuly published';
                header('location:new-post.php');
                $conn->rollBack();
                $_SESSION['error'] = 'Fail to publish the post';
                header('location:new-post.php');


            } else {
                echo "Sorry, there was an error uploading your file.";
            }

        }

?>

but the above code is not working properly. if i upload only image then the code will work fine but when i use if and else statment then the data is inserted only in image table?

update

i try the below code but it will also not working...

<?php
session_start();
include 'conn.php';
$title=$_POST['title'];
$post=$_POST['post'];
$tag=$_POST['tag'];
$cat='some cat';
$file=$_FILES['fileToUpload'];
$file_name=$_FILES['fileToUpload']['name'];
$file_size=$_FILES['fileToUpload']['size'];
$file_height=200;
$file_width=100;
$duration=24.00;
$target_dir_image = "../posts/images/";
$target_dir_video = "../posts/videos/";
$target_file_image = $target_dir_image . basename($_FILES["fileToUpload"]["name"]);
$target_file_video = $target_dir_video . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$ext= pathinfo($file_name,PATHINFO_EXTENSION);


    if ($ext == "jpg" && $ext  == "png" && $ext  == "jpeg" && $ext == "gif")
    {


// Check if file already exists
    if (file_exists($target_file_image)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
// Check file size
    if ($_FILES["fileToUpload"]["size"] > 50000000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
// Allow certain file formats

// Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        $_SESSION['error'] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        header('location:new-post.php');
// if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_image)) {
            $conn->beginTransaction();
            $conn->exec("INSERT INTO post(Title,Post,Category,Tag,Post_Date)VALUES ('" . $title . "','" . $post . "','" . $cat . "','" . $tag . "',now())");
            $conn->exec("INSERT INTO images(Image_Name,Image_Size,Image_Width,Image_height,Image_Directory)VALUES ('" . $file_name . "','" . $file_size . "','" . $file_width . "','" . $file_height . "','" .$target_file_image. "')");
            $conn->commit();
            $_SESSION['success'] = 'Post has been successfuly published';
            header('location:new-post.php');
            $conn->rollBack();
            $_SESSION['error'] = 'Fail to publish the post';
            header('location:new-post.php');


        }
        else {
            echo "Sorry, there was an error uploading your file.";
        }

    }
}

    if ($ext == "mp4" && $ext == "flv" && $ext == "mpeg" && $ext == "avi") {


// Check if file already exists
        if (file_exists($target_file_video)) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
// Check file size
        if ($_FILES["fileToUpload"]["size"] > 50000000) {
            echo "Sorry, your file is too large.";
            $uploadOk = 0;
        }
// Allow certain file formats

// Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            $_SESSION['error'] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            header('location:new-post.php');
// if everything is ok, try to upload file
        }
    }
    else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_video)) {
            $conn->beginTransaction();
            $conn->exec("INSERT INTO post(Title,Post,Category,Tag,Post_Date)VALUES ('" . $title . "','" . $post . "','" . $cat . "','" . $tag . "',now())");
            $conn->exec("INSERT INTO videos(Video_Name,Video_Size,Video_Duration,Video_Dimension,Video_Directory)VALUES ('" . $file_name . "','" . $file_size . "','" . $duration . "','" . $file_width . "','" .$target_file_video. "')");
            $conn->commit();
            $_SESSION['success'] = 'Post has been successfuly published';
            header('location:new-post.php');
            $conn->rollBack();
            $_SESSION['error'] = 'Fail to publish the post';
            header('location:new-post.php');


        }     else {
            echo "Sorry, there was an error uploading your file.";
        }

    }

?>

What i want

when a file is selected then the code checks that it is image or video, if the file is image then it will uploaded to images directory and the data related to image will store in images table and if the file is video then it will uploaded to videos directory and the data related to video will store in video table and also suggest me the way to get the image/video height and width and video duration.

Note: i am using php 5.5 and may be i upgrade to php 5.5x so please provide such solution which is not out of date mean the code support or not deprecated in php 5.5 and above.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongxunhua2054 2014-12-03 04:38
    已采纳

    Try to use getid3() or ffmpeg. these are use to get information of files, i already upload a stackoverflow link through which you can understand how they are work.

    打赏 评论
  • doutou7961 2014-11-29 12:04

    First of all have single browse button with name you want eg. "my_blog_attachment"

    Now before uploading check the mimetype

    $_FILES['my_blog_attachment']['type'] - the MIME type of the uploaded file.

    if mime type is image/png, image/jpg etc then it is image

    OR

    if mime type is video/mp4 or etc then code according to if condition.

    打赏 评论
  • duanchun2349 2014-11-30 07:21

    Let suppose you have selected a file named sample_image.jpg. First of all you need to split this name with . to get the file extension and file name. In above example you are getting file name as below.

    $file_name=$_FILES['fileToUpload']['name'];
    

    So you have to split this like as:

    list($fname,$exten) = split('.', $file_name);
    

    Now you have file extensin and file name in two different variables. Use $exten to diffentiate file type and apply condition on db/file location according to file type.

    打赏 评论
  • duanning9110 2014-12-02 14:10

    Try to print the $_FILES so that u will find $_FILES contains the details of the uploaded files like name,type,tmp_name (where will be files stored temporarily), size, error (will contain the error message if upload is not successful)

    // The uploaded file type [image/jpeg] 
    $type = $_FILES["name of the file"]["type"]  
    
    // image format declaring in an array 
    $img_type = array('image/jpeg','image/jpg','image/png');
    
    // video format declaring in an array 
    $video_type = array('video/mp4','video/avi');
    
    // checking whether the uploaded file is an image type,video type not <br/>
    if(in_array($type,$img_type)) { 
        // if it is image type, move to the coresponding directory location
        $target_dir = "images/";
        // move it to the corresponding location
        // code for moving it to the image table
    
    }
    elseif(in_array($type,$video_type){
        $target_dir = "videos/";
        // move it to the corresponding location
        // code for moving it to the video table
    }
    else {
     // the uploaded files is not either image or video
    }
    
    打赏 评论

相关推荐 更多相似问题