【问题标题】:Upload video and image in php在php中上传视频和图像
【发布时间】: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


【解决方案1】:

首先有一个带有您想要的名称的浏览按钮,例如。 “我的博客附件”

现在在上传之前检查 mimetype

$_FILES['my_blog_attachment']['type'] - 上传文件的 MIME 类型。

如果 mime 类型是 image/png、image/jpg 等,那么它就是 image

如果mime类型是video/mp4等,则根据if条件编码。

【讨论】:

  • 我检查了 MIME 类型,但 MIME 是否与 PHP 5.5 或 PHP 5.5X 兼容。
【解决方案2】:

假设您选择了一个名为 sample_image.jpg 的文件。首先,您需要将此名称与. 分开以获得文件扩展名和文件名。在上面的例子中,你得到的文件名如下。

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

所以你必须将其拆分为:

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

现在您在两个不同的变量中拥有文件扩展名和文件名。使用$exten 区分文件类型并根据文件类型对db/file 位置应用条件。

【讨论】:

  • 我不明白 list($fname,$exten) = split('.', $file_name); 你把所有的东西都分配给 list 之后的哪个变量我打电话要分机?
  • 在图像名称sample_image.jpg 中,您将在$fname 变量中获得图像的第一部分,在$exten 中获得图像的第二部分,即jpg,它是文件的扩展名。
  • 我正在使用 php 5.5,在我的编辑器中拆分功能是删除线,这意味着它在 php 5.5 中不受支持,php.net/manual/en/function.split.php 也显示了一些信息
【解决方案3】:

尝试打印 $_FILES 以便您会发现 $_FILES 包含已上传文件的详细信息,例如 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
}

【讨论】:

    【解决方案4】:

    尝试使用 getid3() 或 ffmpeg。这些是用来获取文件信息的,我已经上传了一个stackoverflow链接,通过它你可以了解它们是如何工作的。

    【讨论】:

      猜你喜欢
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多