【发布时间】:2015-01-27 21:12:45
【问题描述】:
我正在创建一个博客,在其中我可以在管理员端上传图像和视频,在用户端我可以访问它们。现在我想在不同目录上的不同目录和视频上存储图像,但问题是如何在选择文件时创建逻辑,然后代码知道它是图像或视频?我试试……
<?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.";
}
}
?>
但是上面的代码不能正常工作。如果我只上传图片,那么代码可以正常工作,但是当我使用 if 和 else 语句时,数据只插入到图片表中?
更新
我尝试下面的代码,但它也不会工作......
<?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.";
}
}
?>
我想要什么
当一个文件被选中时,代码会检查它是图像还是视频,如果文件是图像,则将其上传到图像目录,与图像相关的数据将存储在图像表中,如果文件是视频然后它会上传到视频目录,与视频相关的数据将存储在视频表中,并建议我获取图像/视频高度和宽度以及视频时长的方法。
注意:我正在使用 php 5.5,可能我升级到 php 5.5x,所以请提供这样的解决方案,它不是过时的意味着代码支持或在 php 5.5 及更高版本中不被弃用。
【问题讨论】:
-
您可以通过简单的方式来完成,只需获取上传文件的名称并将该名称与
.的最后一个索引拆分即可获得文件的扩展名。根据扩展名,您可以相应地区分文件类型 -
请在我的代码中编辑它或给我一个示例链接/代码...
-
检查这个 [Link][1] 希望这会对你有所帮助。 [1]:stackoverflow.com/questions/4847752/…
标签: php mysql file-upload