使用php将文件上传到sql sever

时间:2016-01-23 03:16:32

标签: php sql upload

我尝试使用php将文件上传到数据库。 这是我的代码:

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$db ='testdb';
// Create connection
mysql_connect($servername, $username, $password);
mysql_select_db("testdb");

    if(isset($_POST['submit']))
    {
        $UploadName = $_FILES['UploadFileField']['name'];       
        $UploadTmp =  $_FILES['UploadFileField']['tmp_name'];       
        $UploadName = preg_replace("#[^a-z0-9.]#i","",$UploadName);
        if (!$UploadTmp)
         {
            die ("No File Selected, Please Upload Again");          
         } else
         {
            move_uploaded_file($UploadTmp,"uploaded/$UploadName");      
            $url = "http://localhost/uploadandview/uploaded/$UploadName";           
            mysql_query("INSERT INTO `videos` VALUE('1','$UploadName','$url')");           
          }}?>

但我有一个问题,我无法上传任何文件mp3 / mp4,只是上传文件文本,如.doc,pdf,css等... 请帮帮我!

1 个答案:

答案 0 :(得分:1)

试试此代码

   <?php
$servername = "localhost";
$username = "root";
$password = "";
$db ='testdb';
// Create connection
mysql_connect($servername, $username, $password);
mysql_select_db("testdb");

    if(isset($_POST['submit']))
    {
        $UploadName = $_FILES['UploadFileField']['name'];       
        $UploadTmp =  $_FILES['UploadFileField']['tmp_name'];       
        $UploadName = preg_replace("#[^a-z0-9.]#i","",$UploadName);
        if (!$UploadTmp)
         {
            die ("No File Selected, Please Upload Again");          
         } else
         {
            move_uploaded_file($UploadTmp,"uploaded/".$UploadName);      
            $url = "http://localhost/uploadandview/uploaded/".$UploadName;           
            mysql_query("INSERT INTO `videos` VALUE('1','$UploadName','$url')");           

      }} ?>