在上一篇中讲解了如何通过Ajax提交表单并由PHP处理底层数据,本篇将主要介绍图片的上传与处理。对于文件的上传很简单,只需一个Form便可实现,再通过PHP将源文件上传到目标目录。先上个效果图:
//上传文件需要定义enctype,为了显示图片将target设为Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorialframe
enctype="multipart/form-data" target="Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorialframe">
function Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorialimg(theform){
中显示上传状态
setStatus ("Loading...","showimg");
}
//上传状态函数
function setStatus (theStatus, theObj){
obj = document.getElementById(theObj);
if (obj){
obj.innerHTML = "
" + theStatus + "
";
}
}
process_Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorial.php provides file Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorial function:
Copy to ClipboardQuoted content:
[www.bkjia.com]
//Provide image type verification
$allowedtypes = array("image/jpeg","image/pjpeg","image/png", "image/x-png","image /gif");
//File storage directory
$savefolder = "images";
//Start working if there are files Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorialed
if (isset ($_FILES[' myfile'])){
//Check whether the Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorialed file matches the $allowedtypes type
if (in_array($_FILES['myfile']['type'],$allowedtypes)){
if ($ _FILES['myfile']['error'] == 0){
$thefile = "$savefolder/".$_FILES['myfile']['name'];
//Upload files through move_Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorialed_file
if (!move_Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorialed_file($_FILES['myfile']['tmp_name'], $thefile)){
echo "There was an error Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorialing the file.";
}
else{
?>
br />"http://www.w3.org/TR/xhtml1/DTD/ xhtml1-transitional.dtd">
< ;img src="" onload="doneloading(parent,'')" />
}
}
}
}
?>
The doneloading function in the last part of the above code is used to display images and modify the image size. Thumb.php will be used, which will generate three sizes of large, medium and small source images in the images directory. If you are interested, you can study it. Welcome everyone to make bricks ~
Source code download: Sample6.rar
Reprinted from: http://www.cnblogs.com/gnielee/
http://www.bkjia.com/PHPjc/364410.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364410.htmlTechArticleIn the previous article, we explained how to submit the form through Ajax and have the underlying data processed by PHP. This article will mainly introduce it. Uploading and processing of images. Uploading files is very simple, just a Form...