通过php代码将文件上传到数据库

时间:2011-01-05 12:42:30

标签: php html

我已经制作了一个上传文件的应用程序,但它运行良好。现在我想在数据库上传文件,我还想通过访问数据库在列表中显示上传的文件名。

所以请帮我这样做。我的代码如下:

function uploadFile() {
    global $template;
    //$this->UM_index = $this->session->getUserId();
    switch($_REQUEST['cmd']){


        case 'upload':


            $filename = array();
             //set upload directory
             //$target_path = "F:" . '/uploaded/';
            for($i=0;$i<count($_FILES['ad']['name']);$i++){
            if($_FILES["ad"]["name"])
            {
                $filename = $_FILES["ad"]["name"][$i];
                $source = $_FILES["ad"]["tmp_name"][$i];
                $type = $_FILES["ad"]["type"];

                $name = explode(".", $filename);
                $accepted_types = array('text/html','application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
                foreach($accepted_types as $mime_type)
                {
                    if($mime_type == $type)
                    {
                        $okay = true;
                        break;
                    } 
                }

                $continue = strtolower($name[1]) == 'zip' ? true : false;
                if(!$continue) {
                $message = "The file you are trying to upload is not a .zip file. Please try again.";
                }
                $target_path = "F:" . '/uploaded/'.$filename; 
                 // change this to the correct site path
                if(move_uploaded_file($source, $target_path )) {
                $zip = new ZipArchive();
                $x = $zip->open($target_path);
                if ($x === true) {
                $zip->extractTo("F:" . '/uploaded/'); // change this to the correct site path
                $zip->close();

                unlink($target_path);
                }
                $message = "Your .zip file was uploaded and unpacked.";
                } else {    
                $message = "There was a problem with the upload. Please try again.";
                }

            }
        }
            echo "Your .zip file was uploaded and unpacked.";
            $template->main_content = $template->fetch(TEMPLATE_DIR . 'donna1.html');
            break;
        default:
            $template->main_content = $template->fetch(TEMPLATE_DIR . 'donna1.html');
            //$this->assign_values('cmd','uploads');
            $this->assign_values('cmd','upload');
}
}

我的html页面是

<html>
    <link href="css/style.css" rel="stylesheet" type="text/css">
        <!--<form action="{$path_site}{$index_file}" method="post" enctype="multipart/form-data">-->
<form action="index.php?menu=upload_file&cmd=upload" method="post" enctype="multipart/form-data">

<div id="main">
    <div id="login">

            <br />
            <br />

            Ad No 1:
              <input type="file" name="ad[]" id="ad1" size="10" />&nbsp;&nbsp;Image(.zip)<input type="file" name="ad[]" id="ad1" size="10" />  Sponsor By : <input type="text" name="ad3" id="ad1" size="25" /> 
              <br />
              <br />

  </div>
</div>
</form>
    </html>

3 个答案:

答案 0 :(得分:0)

为什么不将上传的文件名保存为db?

中的字段

答案 1 :(得分:0)

查看您的代码,您实现了“上传”,您似乎不会将文件位置存储到数据库中,您需要执行以下操作:

  • 在上传时,将文件名和路径的详细信息存储到数据库表中
  • 要将这些显示为列表 - 查询数据库,然后回写HTML页面。

互联网上有很多这方面的例子,PHP.net是一个很好的起点。

如果您只需要显示目录的内容,那么您就可以在不需要数据库的情况下实现列表。

答案 2 :(得分:0)

如果你真的需要上传到数据库,你可以使用BLOB(二进制大对象)来实现这个目标:

请参阅以下链接:

Wikipedia - Binary large object

MySQL - The BLOB and TEXT Types

PostgreSQL - Large Objects (BLOBs)

另外,请改写你的问题!