以下供参考
<?
function upload_file($files,$folder)//上传图片
{
$file_tyle = $files['type']
$file_type_arr = array('image/gif','image/x-png','image/jpg','image/pjpeg')
if(!in_array($file_tyle,$file_type_arr) )
{
exit('file type only can be: png,jpeg,jpg,gif')
}
$knamearray = explode(".",$files["name"])
$kname = $knamearray[count($knamearray)-1]
$rand_str = date("ymdhis")
$file_name = $rand_str.".".$kname
$savepath = "$folder/"
/*$savepath = "$folder/date_".date('YmdHis')."/"
if( !is_dir($savepath) ) mkdir($savepath)*/
$upfile = $savepath.$file_name
if( !move_uploaded_file($files['tmp_name'],$upfile) )
{
exit('upload error, please check your file type: png,jpeg,jpg,gif')
}
return $file_name//不要回传值此行可注释掉
}
?>
<?php//将图片存进数据库再读出,注意存储图片的字段类型必须为blob
$user=’root’
$password=’root’
$db=’test’
$connect=mysql_connect(‘localhost’,$user,$password)
mysql_set_charset(‘utf8′,$connect)
mysql_select_db($db)
$photo = “0x”.bin2hex(file_get_contents(“./test.jpg”))
$sql=”INSERT INTO `test`.`test` (`photo`) VALUES ($photo)”//$photo不需要用引号,切记
mysql_query($sql)
//$result=mysql_query(“SELECT *
//FROM `test`
//LIMIT 0 , 30〃)
//$img=mysql_fetch_array($result)
//echo $img['photo']
?>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)