上传照片文件并显示

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?
$uploaddir = "img/";//设置文件保存目录 注意包含/ 
$type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型 

//获取文件后缀名函数 
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['userimg']['name'])),$type)) 
{ 
    $text=implode(",",$type); 
    ?>
     <script language="javascript">
    alert("您只能上传以下类型文件<? echo $text?>!");
    top.location="index.php";
    </script>
    <?
} 
//生成目标文件的文件
else
{    
    $filename=explode(".",$_FILES['userimg']['name']); 
    do 
    { 
        $filename[0]=random(10); //设置随机数长度 
        $name=implode(".",$filename); 
        //$name1=$name.".Mcncc"; 
        $uploadfile=$uploaddir.$name; 
    } 
    while(file_exists($uploadfile)); 
    if (move_uploaded_file($_FILES['userimg']['tmp_name'],$uploadfile))
    { 
        echo $uploadfile;

        if(!is_uploaded_file($_FILES['userimg']['tmp_name']))
        { 
            
            $path="http://10.10.132.222/x0936008/personinfo/".$uploadfile;
            
            $name=$_POST["name"];
            $sex=$_POST["sex"];
            $birthday=$_POST["birthday"];
            $imf=file_get_contents($path);
            $info=$_POST["info"];
            $imf=addslashes($imf);

            $id=mysql_connect("localhost","x0936008","x0936008");
            mysql_query("set names utf8",$id);
            mysql_select_db("x0936008");
            $query="insert into personinfo values('','".$name."','".$birthday."','".$sex."','".$imf."','".$info."')";
            $result=mysql_query($query,$id);
            echo "<script language=javascript>alert('信息已存储成功点击确认查看!');location.href='firstdisplay.php';</script>";
              exit;

            mysql_close($id);
            ?>
            <script language="javascript">
            if(confirm("添加成功,是否继续添加?"))
            {
                top.location="index.php";
            }
            else
            top.location="show.php";
            </script>
            <?
        } 
        else
        {
             ?>
            
            <script language="javascript">
            alert("上传图片出现错误!请重传!");
            top.location="upimage.php";
            </script>
            
            <?
        } 
    } 
} 
///////////////////////////////////////////////////////////
?>
<?php
$name=$_POST["name"];
$sex=$_POST["sex"];
$birthday=$_POST["birthday"];
$image=$_POST["userimg"];
$info=$_POST["info"];

$id=mysql_connect("localhost","x0936008","x0936008");
mysql_query("set names utf8",$id);
mysql_select_db("x0936008");
$query="insert into personinfo values('','".$name."','".$birthday."','".$sex."','".$image."','".$info."')";
$result=mysql_query($query,$id);
echo "<script language=javascript>alert('信息已存储成功点击确认查看!');location.href='firstdisplay.php';</script>";
  exit;
?>
</body>
</html>

 注意使用文件上传之前注意要加上这一句:

enctype="multipart/form-data"

 

显示图片 注意千万不要加入html的文件头,否则图片显示不出来

<?php
$id=$_GET["id"];
$link=mysql_connect("localhost","x0936008","x0936008");
mysql_query("set names utf8",$link);
mysql_select_db("x0936008");
$query="select image from personinfo where id=".$id;
$result=mysql_query($query);
list($img)=mysql_fetch_array($result);
echo $img;
mysql_close($link);
?>

posted on 2012-06-11 14:35  0fengfan0  阅读(492)  评论(0编辑  收藏  举报

导航