- 论坛徽章:
- 0
|
这段代码可以传文件和图片以及压缩文件,都可以
<?php
// 如果提交了表单,代码将被执行:
if ($submit) {
// 连接到数据库
// (你可能需要调整主机名,用户名和密码)
MYSQL_CONNECT( "localhost", "sa", "");
mysql_select_db( "binary_data");
$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));
$result=MYSQL_QUERY( "INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".
"VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");
echo "INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')";
$id= mysql_insert_id();
print "<p>This file has the following Database ID: <b>$id</b>";
MYSQL_CLOSE();
} else {
// 否则显示储存新数据的表单
?>
<form method="post" action=" <?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
File Description:<br>
<input type="text" name="form_description" size="40">
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload/store in database:<br>
<input type="file" name="form_data" size="40">
<p><input type="submit" name="submit" value="submit">
</form>
<?php
}
?>
下面的代码可以上传文件,但是就是不能上传图片,本段代码和上段几乎是一模一样,可是就是不能传图片
upload.php
<html>
<head>
<title>文件上传表单</title>
</head>
<body>
<table>
<form enctype='multipart/form-data' name='myform' action='submit.php'
method='post'>
<INPUT TYPE = "hidden" NAME = "MAX_FILE_SIZE" VALUE ="1000000">
<tr><td>选择上传文件</td><td>
<input name='myfile' type='file'></td></tr>
<tr><td colspan='2'><input name='submit' value='上传'
type='submit'></td></tr>
</form>
</table>
</body>
</html>
submit.php
<?php
if($myfile != "none" && $myfile != "") { //有了上传文件了
//设置超时限制时间,缺省时间为 30秒,设置为0时为不限时
$time_limit=60;
set_time_limit($time_limit); //
//把文件内容读到字符串中
$fp=fopen($myfile, "r");
if(!$fp) die("file open error");
$file_data = addslashes(fread($fp, filesize($myfile)));
//fclose($fp);
//unlink($myfile);
//文件格式,名字,大小
$file_type=$myfile_type;
$file_name=$myfile_name;
$file_size=$myfile_size;
//连接数据库,把文件存到数据库中
$conn=mysql_connect("127.0.0.1","root","root");
if(!$conn) die("error : mysql connect failed");
mysql_select_db("njucx1",$conn);
mysql_query("set names gbk");
$sql="insert into receive (file_data,file_type,file_name,file_size) values ('$file_data','$file_type','$file_name','$file_size')";
$result=mysql_query($sql);
//下面这句取出了刚才的insert语句的id
$id=mysql_insert_id();
mysql_close($conn);
set_time_limit(30); //恢复缺省超时设置
echo "上传成功--- ";
echo "<a href='show_info.php?id=$id'>显示上传文件信息</a>";
}
else {
echo "你没有上传任何文件";
}
?>
[ 本帖最后由 gaojian_088 于 2009-12-11 10:28 编辑 ] |
|