>  기사  >  백엔드 개발  >  Ajax+PHP边学边练 之五 图片处理_PHP

Ajax+PHP边学边练 之五 图片处理_PHP

WBOY
WBOY원래의
2016-06-01 12:22:28690검색

Ajax

先上个效果图:

upload 
Sample6_1.php 中创建Form:
复制代码 代码如下:
//显示上传状态和图片


//上传文件需要定义enctype,为了显示图片将target设为uploadframe
enctype="multipart/form-data" target="uploadframe">
Upload a File:


//上传文件




上传图片函数 uploadimg:
复制代码 代码如下:
function uploadimg(theform){
//提交Form
theform.submit();
//在showimg
中显示上传状态
setStatus ("Loading...","showimg");
}
//上传状态函数
function setStatus (theStatus, theObj){
obj = document.getElementById(theObj);
if (obj){
obj.innerHTML = "
" + theStatus + "
";
}
}

process_upload.php 提供文件上传功能:
复制代码 代码如下:
//提供图片类型校验
$allowedtypes = array("image/jpeg","image/pjpeg","image/png", "image/x-png","image/gif");
//文件存放目录
$savefolder = "images";

//如果有文件上传就开始干活
if (isset ($_FILES['myfile'])){
//检查上传文件是否符合$allowedtypes类型
if (in_array($_FILES['myfile']['type'],$allowedtypes)){
if ($_FILES['myfile']['error'] == 0){
$thefile = "$savefolder/".$_FILES['myfile']['name'];
//通过move_uploaded_file上传文件
if (!move_uploaded_file($_FILES['myfile']['tmp_name'], $thefile)){
echo "There was an error uploading the file.";
}
else{
?>
BR>"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">






Ajax+PHP边学边练 之五 图片处理_PHP


}
}
}
}
?>

上面代码最后部分的doneloading 函数就是用来显示图片及修改图片尺寸大小。其中会用到thumb.php,它会在images目录中生成出源图片的大、中、小三个尺寸,有兴趣可以研究一下。欢迎大家拍砖~
文中源码打包下载
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.