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.