开发者

Upload file to database through php code

I have made an application to upload files and it's working out well. Now I want to upload my files on a database, and I also want to display the uploaded files names on my list by accessing the database.

So please help me do this. My code is given below:

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');
}
}

my html page is

<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 : <i开发者_开发知识库nput type="text" name="ad3" id="ad1" size="25" /> 
              <br />
              <br />

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


Why not save the uploaded filename as a field in the db?


Looking at your code you have implemented the "Upload" you dont seem to be storing the file location into a database, you need to do the following:

  • On upload, store the details of the filename and path into a database table
  • To display these as a list - query the database, and write back to HTML page.

There are loads of examples of this on the internet, PHP.net is a good place to start.

If all you need to do is display the contents of a directory, then you can achieve a listing without the need of a database.


If you really need to upload onto the database you can use BLOBs (Binary Large Object) to achieve this:

See these links:

Wikipedia - Binary large object

MySQL - The BLOB and TEXT Types

PostgreSQL - Large Objects (BLOBs)

Also, rephrase your question!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜