
正文
php保存数据文件格式 php保存数据文件格式不对
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
PHP怎么把图片数据保存为jpg图片到服务器目录
第一步:通过$_FILES获取文件信息。
第二步:指定新文件名称以及路径,并赋值给一个变量。
第三步:通过move_uploaded_file上传文件。
第四步:上传成功后,将数值存入数据库服务器目录即可。
代码如下
1.conn.php
?
$host="localhost"; //数据库服务器名称
$user="root"; //用户名
$pwd="1721"; //密码
$conn=mysql_connect($host,$user,$pwd);
mysql_query("SET
character_set_connection=gb2312,
character_set_results=gb2312,
character_set_client=binary",$conn);
if ($conn==FALSE)
{
echo "center服务器连接失败!br请刷新后重试。/center";
return true;
}
$databasename="database";//数据库名称
do
{
$con=mysql_select_db($databasename,$conn);
}while(!$con);
if ($con==FALSE)
{
echo "center打开数据库失败!br请刷新后重试。/center";
return true;
}
?
2.upload.php
?php
if ($_GET['action'] == "save"){
include_once('conn.php');
include_once('uploadclass.php');
$title=$_POST['title'];
$pic=$uploadfile;
if($title == "")
echo"Scriptwindow.alert('对不起!你输入的信息不完整!');history.back()/Script";
$sql="insert into upload(title,pic) values('$title','$pic')";
$result=mysql_query($sql,$conn);
//echo"Scriptwindow.alert('信息添加成功');location.href='upload.php'/Script";
}
?
html
head
title文件上传实例/title
/head
body
form method="post" action="?action=save" enctype="multipart/form-data"
table border=0 cellspacing=0 cellpadding=0 align=center width="100%"
tr
td width=55 height=20 align="center" /TD
td height="16"
table width="48%" height="93" border="0" cellpadding="0" cellspacing="0"
tr
td标题:/td
tdinput name="title" type="text" id="title"/td
/tr
tr
td文件: /td
tdlabel
input name="file" type="file" value="浏览"
input type="hidden" name="MAX_FILE_SIZE" value="2000000"
/label/td
/tr
tr
td /td
tdinput type="submit" value="上 传" name="upload"/td
/tr
/table/td
/tr
/table
/form
/body
/html
3.uploadclass.php
?php
$uploaddir = "upfiles/";//设置文件保存目录 注意包含/
$type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型
$patch="upload/";//程序所在路径
//获取文件后缀名函数
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);
}
//生成随机文件名函数
function random($length)
{
$hash = 'CR-';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i $length; $i++)
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
$a=strtolower(fileext($_FILES['file']['name']));
//判断文件类型
if(!in_array(strtolower(fileext($_FILES['file']['name'])),$type))
{
$text=implode(",",$type);
echo "您只能上传以下类型文件: ",$text,"br";
}
//生成目标文件的文件名
else{
$filename=explode(".",$_FILES['file']['name']);
do
{
$filename[0]=random(10); //设置随机数长度
$name=implode(".",$filename);
//$name1=$name.".Mcncc";
$uploadfile=$uploaddir.$name;
}
while(file_exists($uploadfile));
if (move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile))
{
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
echo "上传失败!";
}
else
{//输出图片预览
echo "center您的文件已经上传完毕 上传图片预览: /centerbrcenterimg src='$uploadfile'/center";
echo "brcentera href='upload.htm'继续上传/a/center";
}
}
}
?
相关问答
Q1: PHP 当前表单数据保存为excel文件
构造函数php保存数据文件格式:
function down_xls($data, $keynames, $name='dataxls') {
$xls[] = "htmlmeta http-equiv=content-type content=\"text/html; charset=UTF-8\"bodytable border='1'";
$xls[] = "trtdID/tdtd" . implode("/tdtd", array_values($keynames)) . '/td/tr';
foreach($data As $o) {
$line = array(++$index);
foreach($keynames AS $k=$v) {
$line[] = $o[$k];
}
$xls[] = 'trtd'. implode("/tdtd", $line) . '/td/tr';
}
$xls[] = '/table/body/html';
$xls = join("\r\n", $xls);
header('Content-Disposition: attachment; filename="'.$name.'.xls"');
die(mb_convert_encoding($xls,'UTF-8','UTF-8'));
}
函数引用:
if(strval($_GET['download'])){
$orders = DB::LimitQuery('order', array(
'condition' = $condition,
'order' = 'ORDER BY id DESC',
));
if (!$orders) die('没有符合条件php保存数据文件格式的记录');
$name = 'order_'.date('Ymd');
$kn = array( //excel表列名与数据字段php保存数据文件格式的对应关系
'id' = '订单号',
'price' = '订单金额',
'card' = '代金券',
'create_time' = '下单时间',
'pay_time' = '付款时间',
);
foreach( $orders AS $one ){
$one['create_time'] =date("Y-m-d",$one['create_time']);
$one['pay_time']=date("Y-m-d",$one['pay_time']);
$eorders[] = $one;
}
down_xls($eorders, $kn, $name);
}
Q2: php保存网页上的数据为txt的代码?什么?
在存储的时候可以用serialize进行序列化,但取的时候要先用unserialize反序列化。
?php
$data = array("现代"="上海","文化"="西安","首都"="北京");
//将数组存到指定的text文件中
file_put_contents("E:/data.txt",json_encode($data));
//获取数据
$datas = json_decode(file_get_contents("E:/data.txt"));
print_r($datas);
?
php保存数据文件格式的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php保存数据文件格式不对、php保存数据文件格式的信息别忘了在本站进行查找喔。







