很多站长朋友们都不太清楚php图片上传源码,今天小编就来给大家整理php图片上传源码,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 有没有php动态上传图片的代码。网上大部分都是忽悠人的。只能上传一张。如果符合要求的话分全都给了。谢谢 2、 php上传图片到服务器的前端和php代码 3、 网上找到的一段PHP上传图片源码,测试可以用,但是上传名字改变了,高手给看下在哪可以去掉改名? 4、 php上传图片代码 5、 菜鸟一只,求PHP批量上传图片的整个代码。 有没有php动态上传图片的代码。网上大部分都是忽悠人的。只能上传一张。如果符合要求的话分全都给了。谢谢已在百度hi中解决,现附上源码:首先,创建一个upload.html;内容如下:
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; cha$resultet=utf-8" />
<title>
</title>
<script language="javascript">
function upload(id){
div = document.getElementById('light');
div.style.display='block';
document.getElementById('fade').style.display='block';
childs = div.childNodes;
for(i=0;i<childs.length;i++){
if(childs[i].nodeName.toUpperCase() == "IFRAME"){
childs[i].src="funs/up.php?label="+id;
}
}
}
//删除元素
function delpic(id){
a = document.getElementById("pics");
child = document.getElementById("span"+id);
a.removeChild(child);
}
//添加元素
function create(id){
span = document.createElement("span");
span.id = "span"+id;
span.style.display = "block";
span.style.height = "22px";
span.style.lineHeight = "22px";
span.innerHTML = "名称: <input type='text' name='pic_name[]' id='pic_name"+id +"' size='20'>nbspnbsp地址: <input type='text' name='pic_url[]' id='pic_url"+id +"' size='30'> [<a href='javascript:' onClick='upload("+id+")'><font color='#FF0000'>上传更换图片</font></a>] [<a href='javascript:delpic("+id+")'>移除</a>]";
return span;
}
function add_upload(){
div = document.getElementById("pics");
mynum = parseInt(document.getElementById("num").value,10);
//mynum = document.getElementById("num").value;
document.getElementById("num").value = mynum+1;
obj = create(mynum);
div.appendChild(obj);
}
</script>
<style type="text/css">
.white_content {
display:none;
position:absolute;
width:40%;
top:25%;
left:30%;
z-index:9999;
background-color:#fff;
}
.black_overlay{
display:none;
width:100%;
height:100%;
background-color:#ccc;
position:absolute;
top:0;
left:0;
z-index:1;
filter: Alpha(opacity=50);
-moz-opacity:.5;
opacity:0.5;
}
</style>
</head>
<body>
<div id="light" class="white_content">
<div class="white_top"><a href="javascript:void(0)" onclick="document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'"> 关闭</a></div>
<iframe src="funs/up.php" width="100%" height="auto" scrolling="auto" frameborder="0" style="clear:both; padding-top:5px;"></iframe>
</div>
<div id="fade" class="black_overlay"></div>
<div class="right">
<div class="right_body">
<table class="tab">
<tr>
<td align="center">人物照片:
</td>
<td><div id="pics"> <span id="span0">名称:
<input type="text" size="20" id="pic_name0" name="pic_name[]">
地址:
<input type="text" size="30" id="pic_url0" name="pic_url[]">
[<a href="javascript:" onclick="upload(0)"><font color="#ff0000">上传更换图片</font></a>]</span>
<input type="button" value="添加上传文件" onclick="add_upload()" >
</div>
<input type="hidden" id="num" name="num" value="1" >
</td>
</tr>
</table>
</div>
</div>
</body>
</html>
然后在本目录创建一个funs的文件夹,人后在里面创建一个up.php文件,放在,内容如下:
/******************************************************************************
参数说明:
$max_file_size : 上传文件大小限制, 单位BYTE
使用说明:
1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库;
2. 将extension_dir =改为你的php_gd2.dll所在目录;
******************************************************************************/
//上传文件类型列表
$uptypes=array(
'image/jpg',
'image/jpeg',
'image/png',
'image/pjpeg',
'image/gif',
'image/bmp',
'image/x-png'
);
$max_file_size=2000000; //上传文件大小限制, 单位BYTE
$destination_folder="uploadimg/"; //上传文件路径
?>
<html>
<head>
<title>添加上传图片</title>
<style type="text/css">
<!--
body
{
font-size: 12px;
margin:0px;
}
input, select, textarea { border:1px solid #CCC; font-size:12px; padding:2px; font-family:"宋体"; }
-->
</style>
</head>
<body>
<form enctype="multipart/form-data" method="post" name="upform">
<input name="upfile" type="file" size="30">
<input type="submit" value="上传"><br>
<span style="font-size:13px; color:#F00;">
允许上传的文件类型为:jpg, jpeg, png, pjpeg, gif, bmp, x-png</span>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//是否存在文件
{
echo "图片不存在!";
exit;
}
$file = $_FILES["upfile"];
if($max_file_size < $file["size"])
//检查文件大小
{
echo "文件太大!";
exit;
}
if(!in_array($file["type"], $uptypes))
//检查文件类型
{
echo "文件类型不符!".$file["type"];
exit;
}
if(!file_exists($destination_folder))
{
mkdir($destination_folder);
}
$filename=$file["tmp_name"];
$image_size = getimagesize($filename);
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo['extension'];
$destination = $destination_folder.time().".".$ftype;
if (file_exists($destination) $overwrite != true)
{
echo "同名文件已经存在了";
exit;
}
if(!move_uploaded_file ($filename, $destination))
{
echo "移动文件出错";
exit;
}
$pinfo=pathinfo($destination);
$fname=$pinfo[basename];
?>
<script type ="text/javascript">
<?php
if(isset($_GET['label'])){//如果是批量上传?>
window.parent.document.getElementById("pic_name<?php echo $_GET['label']?>").value = "<?php echo $file["name"]?>";
window.parent.document.getElementById("pic_url<?php echo $_GET['label']?>").value = 'uploadimg/<?php echo $fname?>';
window.parent.document.getElementById('light').style.display='none';
window.parent.document.getElementById('fade').style.display='none';
window.close();
<?php }
else{?>
window.parent.document.thisform.uploadfile.value='uploadimg/<?=$fname?>';
window.parent.document.getElementById("success").innerHTML=("上传成功! 宽度:<?=$image_size[0]?>px 长度:<?=$image_size[1]?>px 大小:<?=$file["size"].' '.bytes?>");
<?php }?>
</script>
<?
}
?>
</body>
php上传图片到服务器的前端和php代码前端 代码 使用 extjs 3.4
uploadPhotoWindow=Ext.extend(Ext.Window,{
title:" 上传图片 Upload Photo",
height:420 ,
width:600,
closeAction:'close',
modal : true,
iconCls:'btn-setting',
buttonAlign: 'center',
upload_ok:false,
haveUpload:false,
initComponent : function() {
Ext.form.Field.prototype.msgTarget = 'side';
var po_no=new Ext.form.TextField({name:'Po_no',fieldLabel: '单号 Po No',itemId:'Po_no', width:120,
allowBlank: false, value:this.cur_sele_po_no, hidden:true});
var OP=new Ext.form.TextField({name:'OP',itemId:'OP', width:12,
allowBlank: false, value:"uploadphoto", hidden:true});
var file_name=new Ext.form.TextField({name:'photo_file_name',itemId:'photo_file_name', width:180,
allowBlank: false, value:"",hidden:true,});
var imagebox = new Ext.BoxComponent({
itemId:'imagebox',
autoEl: {
tag: 'img', //指定为img标签
style: 'height:100%;margin:0px auto;border:1px solid #ccc; text-align:center;margin-bottom:10px',
src: 'img/userimg/nophoto.jpg' , //指定url路径
}
});
var form_set_field = new Ext.FormPanel({
frame:true,
itemId:'form_set_field',
layout:'form',
//tableAttrs: {border: 1},
defaults:{labelAlign:'right',labelWidth:110,bodyStyle: 'padding:0 30px 0 0;',frame:false,layout:'form'},
items:[po_no,OP,file_name,imagebox],
});
var file = new Ext.form.TextField({
name: 'imgFile',
fieldLabel: '文件上传',
inputType: 'file',
allowBlank: false,
blankText: '请浏览图片'
});
var form_set_file = new Ext.FormPanel({
frame:true,
fileUpload: true,
itemId:'form_set_file',
layout:'form',
//tableAttrs: {border: 1},
defaults:{labelAlign:'right',labelWidth:110,bodyStyle: 'padding:0 30px 0 0;',frame:false,layout:'form'},
items:[file],
});
var btnOK= new Ext.Button({text: '上传 Upload ', iconCls:'btn-save',width:70,handler: function(){
var form_set=this.ownerCt.ownerCt.getComponent('form_set_file');
var form_set_field=this.ownerCt.ownerCt.getComponent('form_set_field');
var po_no=form_set_field.getComponent('Po_no').getValue();
var file_name=form_set_field.getComponent('photo_file_name');
//alert(po_no);
var imgbox=form_set_field.getComponent('imagebox');
if (form_set.getForm().isValid()){
form_set.getForm().submit({
waitMsg : '正在上传数据 Uploading....',waitTitle:'请稍候 waiting....',
url:'php/toolsfile/photoUpload.php',
method : 'post',
success : function(form, action){
var out = action.result.success;
if (out != true){
Ext.Msg.alert('提示 Tips ', '上传数据失败,错误信息 Save failure :'+action.result.msg);
//alert(action.result.msg);
} else{
//Ext.Msg.alert('提示 Tips ', '上传数据成功,服务器信息: Save success '+action.result.msg);
file_name.setValue(action.result.file_name);
imgbox.getEl().dom.src=action.result.file_scr;
form_set.ownerCt.savePhoto();
//form_set.ownerCt.grid.store.load();
//form_set.ownerCt.dateChang=true;
//form_set.ownerCt.destroy( );
}
},
failure: function(form, action) {
switch (action.failureType) {
case Ext.form.Action.CLIENT_INVALID:
Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
break;
case Ext.form.Action.CONNECT_FAILURE:
Ext.Msg.alert('Failure', 'Ajax communication failed');
break;
case Ext.form.Action.SERVER_INVALID:
Ext.Msg.alert('Failure', action.result.msg);
break;
}
},
});
}else{
Ext.Msg.alert('提示 Tips :', '请选择文件! \n Please select Img file ');
}
}
});
var btnCancel = new Ext.Button({text: ' 关闭 Close ', iconCls:'btn-cancel',width:70,handler: function(){this.ownerCt.ownerCt.destroy( )}});
Ext.apply(this,{
items: [form_set_field,form_set_file],
buttons: [btnOK, btnCancel],
});
uploadPhotoWindow.superclass.initComponent.call(this);
},
savePhoto:function (){
//alert(this.cur_sele_po_no);
var form_set_field=this.getComponent('form_set_field');
var form_set_file=this.getComponent('form_set_file');
form_set_field.getForm().submit({
waitMsg : '上传成功,正在存储 saveing....',waitTitle:'请稍候 waiting....',
url:'php/jsonfile/po_nophotolist_json.php',
method : 'post',
success : function(form, action){
var out = action.result.success;
if (out != true){
Ext.Msg.alert('提示 Tips ', '存储失败,错误信息 Save failure :'+action.result.msg);
} else{
Ext.Msg.alert('提示 Tips ', '存储成功,服务器信息: Save success '+action.result.msg);
form_set_file.getForm().reset();
form_set_file.ownerCt.haveUpload=true;
}
},
failure: function(form, action) {
switch (action.failureType) {
case Ext.form.Action.CLIENT_INVALID:
Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
break;
case Ext.form.Action.CONNECT_FAILURE:
Ext.Msg.alert('Failure', 'Ajax communication failed');
break;
case Ext.form.Action.SERVER_INVALID:
Ext.Msg.alert('Failure', action.result.msg);
break;
}
},
});
},
isUpload:function(){
return this.haveUpload;
}
});
后台php photoUpload.php'
<?
require_once('classfile/guid.class.php');
if(!isset($_FILES['imgFile'])){
echo json_encode(array("success"=>false, 'msg'=>"Not get Imgfile"));
return;
}
$upfile=$_FILES['imgFile'];
$name=$upfile["name"];//上传文件的文件名
$type=$upfile["type"];//上传文件的类型
$size=$upfile["size"];//上传文件的大小
$tmp_name=$upfile["tmp_name"];//上传文件的临时存放路径
$error_cod=$upfile["error"];
if ($error_cod>0){
echo json_encode(array("success"=>false, 'msg'=>$error_cod));
}
$ext_file_name="";
switch ($type){
case 'image/pjpeg':
$okType=true;
$ext_file_name =".jpg";
break;
case 'image/jpeg':
$okType=true;
$ext_file_name =".jpg";
break;
case 'image/gif':
$okType=true;
$ext_file_name =".gif";
break;
case 'image/png':
$okType=true;
$ext_file_name =".png";
break;
}
if(!$okType){
echo json_encode(array("success"=>false, 'msg'=>"Not image "));
return;
}
$web_root="D:".DIRECTORY_SEPARATOR."Easy2PHP5".DIRECTORY_SEPARATOR."webSiteJfz".DIRECTORY_SEPARATOR;
$photo_tmp_path=$web_root."img".DIRECTORY_SEPARATOR."userimg".DIRECTORY_SEPARATOR."temp";
$temp_file_name= creat_guid(0).$ext_file_name;
$photo_tmp_file_name=$photo_tmp_path.DIRECTORY_SEPARATOR.$temp_file_name;
$photo_tmp_file_scr="img".DIRECTORY_SEPARATOR."userimg".DIRECTORY_SEPARATOR."temp".DIRECTORY_SEPARATOR.$temp_file_name;
move_uploaded_file($tmp_name,$photo_tmp_file_name);
echo json_encode(array("success"=>true, 'msg'=> "ok","file_name"=>$photo_tmp_file_name,"file_scr"=>$photo_tmp_file_scr));
//echo json_encode(array("success"=>false, 'msg'=> json_encode($_FILES['imgFile'])));
return;
?>
guid.class.php // 生成唯一的图片文件名
<?
function creat_guid($long){
$uuid="";
if (function_exists('com_create_guid')){
$uuid=com_create_guid();
}else{
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
$uuid = chr(123)// "{"
.substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12)
.chr(125);// "}"
//return $uuid;
}
if (!isset($long) || $long==0 ){
return substr($uuid,1, strlen($uuid)-2);
}else{
return $uuid;
}
}
网上找到的一段PHP上传图片源码,测试可以用,但是上传名字改变了,高手给看下在哪可以去掉改名?$_POST['upimg]
这个是你上传图片的属性
你可以在这后面输入:
echo '<pre>';
print_r($_POST['upimg']);
die();
输出上传图片的属性进行查看。本地上传图片输出的内容为一个数组,内容为:
Array(
[upimg]=>Array(
[name]=>'xxxxx这个就是你本地电脑图片的名称了',
[type]=>'image/jpg',
['tmp_name']=>'....',
......
)
)
然后在查看你的upload.php处理文件,看到保存名字的地方,改成$_POST['upimg']['name']的名称即可。(如需要传递,则传递一下这个参数)
若有不明白,欢迎追问.....
刚看了你给的文件,源文件是采用时间戳来命名文件,只需要修改一下这句话就可以的了
帮你修改了,你下载看看,再试试!
php上传图片代码等下,只是上传?那么简单,不用写到数据库么?
给个反映啊,不然我怎么帮你写。.....
好啦号啦,我写了:
<title>图片上传</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
图片上传:
<label>
<input type="file" name="file" id="file" />
</label>
<label>
<input type="submit" name="button" id="button" value="上传" />
</label>
</form>
<?php
if(isset($_POST['button']))//检测按下了上传按钮
{
$file=$_FILES['file']['tmp_name'];//$_FILES是二维数组,file是文件;名,tmp_name是固定的,是上传到系统的位置
$name=date("Ymdgid").'.jpg';//上传以后,文件的新名字,用时间来做名字
$copy=copy($file,"photo/$name");//copy函数,是把文件拷贝到站点下的photo文件夹中
if($copy)//如果拷贝成功
echo '上传成功';
else
echo '上传失败';
}
?>
</body>
</html>
菜鸟一只,求PHP批量上传图片的整个代码。这是我写的图片批量上传的类:你直接调用就行了:
<?php
function upload($fileName,$filePath)
{
//判断该文件是否是用户根据POST方式提交到服务器的上传文件
foreach($_FILES[$fileName]['tmp_name'] as $k=>$v)
{
if($_FILES[$fileName]['name'][$k]!="")
{
$result=check($_FILES[$fileName]['size'][$k],$_FILES[$fileName]['type'][$k],$_FILES[$fileName]['name'][$k]);
if($result['error']==1)
{
echo $result['msg']."<br>";
echo "出错文件:".$result['name']."<br>";
}
else
{
$arrTT=explode(".",$_FILES[$fileName]['name'][$k]);
$extName=$arrTT[count($arrTT)-1];
$NewName=sha1(microtime()).".".$extName;
if(move_uploaded_file($v,$filePath.$NewName));
$arrWW[]=$NewName;
}
}
}
return $arrWW;
}
function check($size,$types,$name)
{
if($size>=5242880)
{
$result['msg']='文件过大!';
$result['error']=1;
$result['name']=$name;
}
$arrType=array('image/pjpeg','image/gif','image/x-png','audio/mp3','application/msword','application/vnd.ms_excel','application/octet-stream','application/vnd.ms-powerpoint');
if(!in_array($types,$arrType))
{
$result['msg']='文件类型不匹配!';
$result['error']=1;
$result['name']=$name;
}
return $result;
}
?>
其实,有很多php的图片上传的类,你可以去下载几个,看看怎么调用就行了
关于php图片上传源码的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php图片上传源码 php上传图片到指定文件夹的详细内容...