使用php从mysql数据库上传和检索视频

时间:2014-09-21 09:08:39

标签: php html

我有一个大学项目,可以创建一个虚拟教室,教师可以上传学生可以访问的视频讲座。

我试图将视频路径保存到数据库中,将视频保存到文件夹中,然后尝试访问它。

有时视频不会上传。尝试保存超过100 MB的视频时,路径无法保存在数据库中。如果视频大小较小,则路径将保存到数据库。我不明白问题是什么,因为我试图保存路径而不是视频。

***************这是我的上传脚本***********

<?php
session_start();

 require("/Applications/MAMP/htdocs/conn.php");

 function is_valid_type($file)
{
$valid_types = array('video/mp4', 'video/mpeg', 'video/mpg', 'audio/mpeg');

    if (in_array($file['type'], $valid_types))
        return 1;
    return 0;
}
 function showContents($array)
{
    echo "<pre>";
    print_r($array);
    echo "</pre>";
}

 $TARGET_PATH = "/Applications/MAMP/htdocs/home/";

 $id = $_POST['id'];
 $name = $_POST['name'];
 $video_path = $_FILES['video_path'];
 $TARGET_PATH .= $video_path['name'];
 if ( $id == "" || $name == "" || $video_path['name'] == "" )
{
    $_SESSION['error'] = "All fields are required";
    header("Location: index.php");
    exit;
}
 if (!is_valid_type($video_path))
{
    $_SESSION['error'] = "You must upload a jpeg, gif,mp4 or bmp";
    header("Location: index.php");
    exit;
}

 if (file_exists($TARGET_PATH))
{
    $_SESSION['error'] = "A file with that name already exists";
    header("Location: index.php");
    exit;
}

 if (move_uploaded_file($video_path['tmp_name'], $TARGET_PATH))
{
    $sql = "insert into video (id, name, video_path) values ('$id', '$name', '" .           $video_path['name'] . "')";
    $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
    header("Location: http://localhost:8888/htdocs/images.php");
    exit;
}
 else
{
    $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
    header("Location: index.php");
    exit;
}
?>

*******************这是我的显示脚本**************

<?php 
require("/Applications/MAMP/htdocs/conn.php");
?>

<html>
<head>
    <title>tutorial</title>
</head>

<body>

    <div>

        <?php   
            $sql = "select * from video";
            $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());

            while ($row = mysql_fetch_assoc($result))
            {   
 $src=$row['video_path'];

                    $path="http://localhost:8888/home/";
 $home=$path.$src;
                //echo "<video src=\"$home" ."\" height=\"200\" width=\"200\"/>";
                echo $row['id'] . " " . $row['name'] . "<br />";
                echo "</p>";
                ?>
 <video width="320" height="240" controls>
      <source src="<?php echo $home ?>" type="video/mp4"  >
    </video> 
 <?php

            }

        ?>


 </div>
 </body>
</html>

********************这是我的表格脚本**********

    <?php 
    session_start(); 
    ?>
    <html>
    <head>
        <title>Dream in code tutorial</title>

        <style type="text/css">
            label
            {
                float: left;
                text-align: right;
                margin-right: 10px;
                width: 100px;
                color: black;
            }

            #submit
            {
                float: left;
                margin-top: 5px;
                position: relative;
                left: 110px;
            }

            #error
            {
                color: red;
                font-weight: bold;
                font-size: 16pt;
            }
        </style>
    </head>

    <body>

        <div>
                <?php
                if (isset($_SESSION['error']))
                {
                    echo "<span id=\"error\"><p>" . $_SESSION['error'] . "</p></span>";
                    unset($_SESSION['error']);
                }
                ?>
                <form action="upload.php" method="post" enctype="multipart/form-data">
                <p>
                    <label>ID</label>
                    <input type="text" name="id" /><br />

                    <label>Video Name</label>
                    <input type="text" name="name" /><br />

                    <label>Upload Image</label>
                    <input type="file" name="video_path" /><br />
                    <input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
                    <input type="submit" id="submit" value="Upload" />
                </p>
                </form>
        </div>
    </body>
</html>

2 个答案:

答案 0 :(得分:1)

根据您的需要更改以下设置:

max_execution_time = 1800 #Time in seconds your script may run
memory_limit = 512M #max amount of memory a script may use
post_max_size = 128M #Maximum size of POST data that PHP will accept
file_uploads = On # Will never work if this isn't set to On
upload_max_filesize = 256M the maximum size any given uploaded file may have

不要忘记重新加载apache(或者如果你想重新启动)。

答案 1 :(得分:0)

上传文件大小限制,在您的情况下可能设置为100MB。

请参阅PHP change the maximum upload file size了解如何更改限制。

编辑:你必须在php.ini中更改这些设置,你不能在代码中更改它们:

  

如果你不能改变你的php.ini,那你就不走运了。您无法在运行时更改这些值;上传文件大于php.ini中指定的值将在执行到达ini_set时失败。