通过写入图片的二进制流实现图片上传功能

<?php header("Content-Type: text/html;charset=utf-8");?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>上传图像</title>
<script type="text/javascript">
function check(){
    var image = document.imageUpload.image.value;
    if(image=='') {
        alert('请选择要上传的图片');
        return false;
    }
}
</script>
</head>
<body>
<?php
if(!empty($_FILES['image']['tmp_name'])) {
     $content = file_get_contents($_FILES['image']['tmp_name']);//得到该图片的二进制编码
     echo $_FILES['image']['tmp_name'];
     file_put_contents($_FILES['image']['name'],$content);//将得到的二进制码写入图片文件中
?>
<img src="<?php echo $_FILES['image']['name'];?>"  border="0"/>

<p>二进制编码是:<?php echo $content;?></p>
<?php
} else{
?>
    <form  method="post" action="#" name="imageUpload" enctype="multipart/form-data">
<table width="100%" border="0">
    <tr>
        <td>请选择图片:<input type="file" name="image"/><input type="submit" value="上传" onclick="return check();" /></td>
    </tr>
</table>
</form>

<?php
}
?>
</body>
</html>

 

posted @ 2014-10-14 23:03  WilliamHu  阅读(1348)  评论(0编辑  收藏  举报