3

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.

  • You can do it in simple way, just get name of uploaded file and split this name with last index of `.` to get extension of file. According to extension you can differentiate the file type accordingly – Altmish-E-Azam Nov 29 '14 at 20:19
  • please edit it in my code or show me an example link/code... –  Nov 30 '14 at 03:44
  • 1
    Check this [Link][1] hope this will help you. [1]: http://stackoverflow.com/questions/4847752/how-to-get-video-duration-dimension-and-size-in-php –  Dec 03 '14 at 04:36

4 Answers4

3

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.

2

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.

Rana Soyab
  • 898
  • 5
  • 20
  • 1
    i check for mime type but is the MIME are compatible with PHP 5.5 or PHP 5.5X. –  Nov 29 '14 at 13:11
1

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.

Altmish-E-Azam
  • 1,561
  • 1
  • 13
  • 24
  • i don't understand the **list($fname,$exten) = split('.', $file_name);** you assign every thing to list after this which variable i call to get the extension? –  Nov 30 '14 at 07:54
  • In image name `sample_image.jpg` you will get first part of image in `$fname` variable and second part in `$exten` that is `jpg` which is an extension of a file. – Altmish-E-Azam Nov 30 '14 at 09:39
  • i am using php 5.5 and in my editor the split function is strikethrough, mean it is not supported in php 5.5, the http://php.net/manual/en/function.split.php also show some information –  Nov 30 '14 at 09:47
1

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
}
Orchid
  • 572
  • 4
  • 17