Home  >  Article  >  Backend Development  >  Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorial

Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 14:55:28721browse

  在上一篇中讲解了如何通过Ajax提交表单并由PHP处理底层数据,本篇将主要介绍图片的上传与处理。对于文件的上传很简单,只需一个Form便可实现,再通过PHP将源文件上传到目标目录。先上个效果图:

Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorial

  Sample6_1.php 中创建Form:

Copy to ClipboardLiehuo.Net Codes引用的内容:[www.bkjia.com] //显示上传状态和图片

//上传文件需要定义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">
Upload a File:


//上传文件


上传图片函数 Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorialimg:

Copy to ClipboardLiehuo.Net Codes引用的内容:[www.bkjia.com] function Ajax & PHP Learn and Practice Part 5 Image Processing_PHP Tutorialimg(theform){
//提交Form
theform.submit();
//在showimg
中显示上传状态
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 ClipboardLiehuo.Net CodesQuoted 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/

www.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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn