通过php代码上传文件到数据库
问题描述:
我已经提出了一个应用程序来上传文件,它的工作良好。现在我想将我的文件上传到数据库,并且我还想通过访问数据库在列表中显示上传的文件名。通过php代码上传文件到数据库
所以请帮我做这个。我的代码是下面给出:
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" /> 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>
答
为什么不能上传的文件名保存在一个数据库中的字段?在你的代码
答
寻找你已经实现了“上传”您好像还可以存储文件的位置到数据库中,你需要做到以下几点:
- 上载,存储文件名的详细信息并将路径导入到数据库表中
- 将这些显示为列表 - 查询数据库并写回到HTML页面。
在互联网上有很多这样的例子,PHP.net是一个很好的开始。
如果您只需要显示目录的内容,那么您可以在不需要数据库的情况下实现列表。
答
如果你真的需要上传到数据库,你可以使用的BLOB(二进制大对象)来实现这一目标:
请参见以下链接:
Wikipedia - Binary large object
MySQL - The BLOB and TEXT Types
PostgreSQL - Large Objects (BLOBs)
另外,改说你的问题!
你的问题是什么? – 2011-01-05 12:44:12
你会好心为我写我的应用程序。谢谢。 – dqhendricks 2011-01-05 22:36:05