PHP image upload using form form upload

Source: Internet
Author: User

, using form form to upload
This is the most primitive way to upload, the front end is a simple form form, back end we have PHP processing transmitted over the file.
First look at the front-end code upload.html

1234 <form action="handle.php" name="form" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" name="submit" value="上传" /></form>

Then create a new PHP file handle.php code as follows

12345678910111213141516171819202122 $file = $_FILES[‘file‘];//得到传输的数据//得到文件名称$name = $file[‘name‘];$type = strtolower(substr($name,strrpos($name,‘.‘)+1)); //得到文件类型,并且都转化成小写$allow_type = array(‘jpg‘,‘jpeg‘,‘gif‘,‘png‘); //定义允许上传的类型//判断文件类型是否被允许上传if(!in_array($type, $allow_type)){ //如果不被允许,则直接停止程序运行 return ;}//判断是否是通过HTTP POST上传的if(!is_uploaded_file($file[‘tmp_name‘])){ //如果不是通过HTTP POST上传的 return ;}$upload_path = "D:/now/"; //上传文件的存放路径//开始移动文件到相应的文件夹if(move_uploaded_file($file[‘tmp_name‘],$upload_path.$file[‘name‘])){ echo "Successfully!";}else{ echo "Failed!";}

Of course, this PHP processing code is not very perfect, where the error situation is not processed, but the use of PHP processing upload file principle is this, we can optimize on this basis, so that it is more perfect.
Second, using uploadify to achieve no refresh and with progress bar upload
First download the Uploadify plugin, save it under a folder in your project, and then introduce three files

?
123 <link href="js/uploadify/uploadify.css" rel="stylesheet" type="text/css" /><script type="text/javascript" src="js/uploadify/jquery.min.1.7.js"></script><script type="text/javascript" src=js/uploadify/jquery.uploadify.min.js"></script>

Then reference the following code

?
1234567891011121314151617181920212223242526272829 <script type="text/javascript"> $(function(){ $(".uploadbutton").each(function(){ bidtype="上传"; $(this).uploadify({ swf: ‘js/uploadify/uploadify.swf‘, uploader: "handle.php", //处理上传的php文件或者方法 multi: true, //是否开启一次性上传多个文件 queueSizeLimit:20, //最大允许上传的文件数量 buttonText: bidtype, //按钮文字 height: 34, //按钮高度 width: 82, //按钮宽度 auto:false, //选择完图片以后是否自动上传 method:‘post‘, fileTypeExts: "*.jpg;*.png;*.gif;*.jpeg;", //允许的文件类型 fileTypeDesc: "请选择图片文件", //文件说明 postData:{}, formData: { "imgType": "normal","timestamp":"asdfsa","token":"48f262516b3912a060d21ef6af564668" }, //提交给服务器端的参数 onUploadSuccess: function (file, data, response) { //一个文件上传成功后的响应事件处理 var data = $.parseJSON(data); } }); }) }); </script><body> <span id="commercial_upload" class="uploadbutton"></span></body>

Of course, the style of this button I changed, you can modify the style file according to your needs, change the style of this button
The same PHP code handle.php

12345678910111213141516171819202122 $file = $_FILES[‘Filedata‘];//得到传输的数据//得到文件名称$name = $file[‘name‘];$type = strtolower(substr($name,strrpos($name,‘.‘)+1)); //得到文件类型,并且都转化成小写$allow_type = array(‘jpg‘,‘jpeg‘,‘gif‘,‘png‘); //定义允许上传的类型//判断文件类型是否被允许上传if(!in_array($type, $allow_type)){ //如果不被允许,则直接停止程序运行 return ;}//判断是否是通过HTTP POST上传的if(!is_uploaded_file($file[‘tmp_name‘])){ //如果不是通过HTTP POST上传的 return ;}$upload_path = "D:/now/"; //上传文件的存放路径//开始移动文件到相应的文件夹if(move_uploaded_file($file[‘tmp_name‘],$upload_path.$file[‘name‘])){ echo "Successfully!";}else{ echo "Failed!";}

The above two ways to upload can be said that the principle of PHP processing is the same, but the foreground display of different styles, the second way is no refresh, and with the return value, you can easily do other processing. Although commonly used, but will always be restricted, use is not very flexible.
With the development of HTML5 more and more mature, we can use some of the methods in H5 to achieve the upload function, the background PHP processing code is completely different from the above two ways, and use is very flexible. Due to space limitations, this article I do not introduce this way, interested can refer to, I hope you will like.

"Wang Danling listen to the joke response:" You seven years ago, how old, seven years! Please, you know love when you are seven years old, I don't know now? "The two Hands grabbed the dragon a neck of clothing, loudly said:" You m playing this girl is not? This girl looks ugly, the amount is very difficult to see. What's that, you itch for a fight, you little prick? Damn, when is this girl an imbecile? Seven years old still love seven years, go to die you. "Bang ~ ~" Wang Danling right hand changes into a fist, a type of hook, the dragon will be knocked down to the ground. Muten Roshi smiled and said: "These three Zhanglingjing cards, to you, very Chen should be enough for me to study." "Muten Roshi from the space ring to the Dragon 13 Zhang Ling Crystal card, Dragon one took the card to throw into the evil star, and then took out a letter from the evil Star:" This letter to you, the content is what I do not know, ah ~ trouble you sign. "Muten Roshi stretched out his hand, to the dragon a letter, the dragon a letter back put on the back way:" Call me Messenger said, this is very important letter, must be handed to Muten Roshi himself.
Http://www.cn-teacher.com/kfMARREX/SMCinmaa/index.html
Http://www.cn-teacher.com/kfnvvqg/VLuyVuwD/index.html
Http://www.cn-teacher.com/kfMFTWCR/hgzHuRf/index.html
Http://www.cn-teacher.com/kfapxS/xmzl3/index.html
Http://www.cn-teacher.com/kf99s9m/JQsViiF/index.html
Http://www.cn-teacher.com/kfex3iq/fKuZYh/index.html
Http://www.cn-teacher.com/kfdbxoykok/5aik49/index.html
Http://www.cn-teacher.com/kfTVAOFA/JNcWIz/index.html
Http://www.cn-teacher.com/kfkXvF/3ZQzbRa/index.html
Http://www.cn-teacher.com/kfk9x1273/1w2pkl/index.html
Http://www.cn-teacher.com/kfmssfet/vlai1/index.html
Http://www.cn-teacher.com/kfCHEODK/b8ctb8/index.html
Http://www.cn-teacher.com/kfFYYDQ/kjwr2s/index.html
Http://www.cn-teacher.com/kfmvech/44jyb/index.html
Http://www.cn-teacher.com/kfjw4el/SRNXAF/index.html
Http://www.cn-teacher.com/kfigvlwtg/bfkq/index.html
http://www.cn-teacher.com/kfrvQbNFn/
Http://www.cn-teacher.com/kfIUqHpy/KADNX/index.html
Http://www.cn-teacher.com/kfmkmqa6a/lbweef3p/index.html
Http://www.cn-teacher.com/kfVDGd6Py/yMdB/index.html
Http://www.cn-teacher.com/kfIPZMYM/DMOBMkj/index.html
Http://www.cn-teacher.com/kfnyvbnc/0nstl/index.html
Http://www.cn-teacher.com/kfljvzYul/TDEKI/index.html
Http://www.cn-teacher.com/kfije4/1bemrb/index.html
Http://www.cn-teacher.com/kfTAUQVALV/GVPTT/index.html
Http://www.cn-teacher.com/kfEFMFUT/EyWLDo9/index.html
Http://www.cn-teacher.com/kfb1d8hf/EHDCQBNL/index.html
Http://www.cn-teacher.com/kfR19fp/DWNVZ/index.html
Http://www.cn-teacher.com/kfoXGgA/CXAQ/index.html
Http://www.cn-teacher.com/kfNi1Tr/COCLNS/index.html
Http://www.cn-teacher.com/kfCWzhfzpF/mhkcee/index.html

PHP image upload using form form upload

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.