Uploading File with PHP and Client Side Scripting
来源:互联网 发布:淘宝托管代运营 编辑:程序博客网 时间:2024/05/22 06:45
Step 1: The non-Javascript Implementation
The most basic structure of the script for doing the job is as following, this is from the description in <PHP MySQL All in One for Dummies>:
The user interface:
<html><head></head><body><form enctype="multipart/form-data" action="fileHdler.php" method="POST"><label>Upload File</label><br /><input type="hidden" name="MAX_FILE_SIZE" value="300000" /><input type="file" name="user_file" /><br /><input type="submit" value="Upload File" /></form></form> </body></html>
And handler on server side:
<?phpif(isset($_FILES['user_file'])){echo 'The file exits: ' . $_FILES['user_file']['name'];echo '<br />';print_r( $_FILES['user_file']);if($_FILES["user_file"]["error"] == 0){move_uploaded_file($_FILES['user_file']['tmp_name'], 'upload/'.$_FILES['user_file']['name']);}}else{echo 'file does not exist.';}?>
When going through this test, I found something:
1. you still can specify the max size of the uploaded size, by adding a hidden input field with name 'MAX_FILE_SIZE';
2. the temp directory of the uploaded file is specified in php.ini, but you can never find that file under the temp directory;
3. the array of info for one uploaded file is like: Array ( [name] => cms.png [type] => image/png [tmp_name] => C:\php5\tmp\php127.tmp [error] => 0 [size] => 45006 )
4. even the submit type input, is accessible from server by $_POST['name'].
Step 2: To Improve User Experiences By Using Javascript
The way to handle file uploading just shown above has a problem with respect to uses' experience: the process must navigate the page away, and maybe you would say we could use the same page to play both the roles of uploader interface and handler, but that still cause the reload of the page. The concept of Ajax is asynchronously updating, that means only change the parts which need to be changed.
Can XMLHTTPRequest post file to server side? So far I have no idea, since no tutorial mentioned. Here is a sample work out that with pure Javascript, although it is called 'AJAX Blablabla':
The source code can be downloaded from the site. The idea of this method can be illustrated by this picture:
First of all, you need a form, it is where you place your file uploader HTML element, and then the trick comes into play: let's add an iFrame element into the HTML page, but set its width and height are zero pixel to make it invisible. And the next up is to specify that form targeting at that iFrame.
When user press the upload button, we play an loading animation by revealing a GIF image, which always stands by there. Then on the server side, process the file(saving it into some path for instance) and then sleep for some time in case the file is large-size, the sleeping time of the server just the interval between the server handle the file and output the JS function code to the browser. Then output a literal Javascript code to the client, which will be rendered within the iFrame; but that only carries function but not visual element. So the browser will run the code and keep the iFrame invisible, and in the Js function, we hide the loading animation and output the result to the user.
So, in this way, we simulate the asynchronously upload process, but there is a problem: it is not easy to predicate the size of the file and estimate the uploading time, some time the file size could be small, but the animation is over-played for the user to wait. There is no case that the browser has removed the loading animation, but the server has not yet finished the processing, because the sleep function is invoked after that.
With that thinking, you can easily figure out how to implement a multiple file uploader.
http://www.coursesweb.net/ajax/multiple-upload-files_t
And now, shall we take a look at a more advanced sample, in terms of design and encapsulation, which is:
It is listed in this article, which collecting the Ajax file uploader. Here is a link talk about it in Chinese:
http://www.zhangxinxu.com/wordpress/2009/11/ajax-upload%E5%A4%9A%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E6%8F%92%E4%BB%B6%E7%BF%BB%E8%AF%91%E5%8F%8A%E4%B8%AD%E6%96%87%E6%BC%94%E7%A4%BA/
Step 4: With Help of XHR
Step 4: With Help of an Ajax Framework
- Uploading File with PHP and Client Side Scripting
- Preview image when uploading it on client side. Available for IE and FF
- Trouble with flex file-uploading and twisted.web2 FileSaver(烙铁)
- Uploading Files With PHP
- client-side and server-side
- Client-side data filtering with E4X and XMLListCollection
- Client Extension and Scripting Capabilities
- Jax-ws server side and client side
- Scripting with Scala and SBT
- File uploading
- File Handling at Client Side Using Javascript
- couldn't open file on client side trying server side
- Working with Client-Side Script(MSDN)
- Detect missing images with client-side script
- Using jQuery with Client-Side Data Binding
- Expand and Collapse Rows on Client-Side
- About client-side form validation and frameworks
- Riak, haproxy, and client side applications
- 为UIKeyboardTypeNumberPad增加自定义按键
- 位操作符的一些实用方法介绍
- 利用javascript是表单隔行换颜色
- log4j配置操作
- Eclipse+CDT 头文件无法解析的问题(Ubuntu 11.10,Eclipse 3.7)
- Uploading File with PHP and Client Side Scripting
- acm细节
- Package-Pipeline
- cocos2d-x 之 CCTextureAtlas 简介
- libgdx之粒子特效
- 一些函数
- JQueryUI的autocomplete调用远程数据(ashx和webservice)
- ASIHTTPRequest 在工程中的运用
- Android 获取联系人资料。