PHP 5 ways to upload files

Source: Internet
Author: User
Tags yii
PHP 5 ways to upload files
Author: zccst

one, ordinary file upload method
There are two ways in which the last file was previously available, as follows:
1, use the original eco-PHP last file.
(1) Front-end code

(2) PHP code (upload_file.php)
 
  0) {    echo "Return Code:". $_files["File" ["Error"]. "
"; } else { echo "Upload:". $_files["File" ["Name"]. "
"; echo "Type:". $_files["File" ["type"]. "
"; echo "Size:". ($_files["File" ["Size"]/1024). The Kb
"; echo "Temp file:". $_files["File" ["Tmp_name"]. "
"; if (file_exists ("upload/". $_files["File" ["Name"])) { echo $_files["file" ["Name"]. "already exists."; } else{ move_uploaded_file ($_files["File" ["Tmp_name"], "upload/". $_files["File" ["name"]); echo "Stored in:". " Upload/". $_files["File" ["Name"];}} } else { echo "Invalid file";}? >


2, use the cupload of the YII framework.
See 1:http://zccst.iteye.com/blog/1114948
See 2:http://zccst.iteye.com/blog/1271104
However, the same problem that both faces is when the page is refreshed after submission. That is, the non-asynchronous last method.

Second, asynchronous file upload mode (3 ways)
Now need asynchronous upload way, after classmates help and check data, know PHP asynchronous upload file has two ways to implement.
1, use plug-ins (e.g., jqueryupload).
2, upload using IFRAME, no additional plugins required.

Based on current requirements: use as few plugins as possible. The latter is then used to upload files asynchronously using an IFRAME
Specifically added later
(1) Front-end HTML


function Startupload () {var spanobj = document.getElementById ("info"); spanobj.innerhtml = "Start uploading"; document.getElementById ("Upform"). Sumbit ();} callback function Stopupload (responsetext) {var spanobj = document.getElementById ("info"); spanobj.innerhtml = "Upload succeeded"; spanobj.innerhtml = responsetext;}


(2) server-side code
$file = $_files[' myfile '); $fileName = UploadFile ($file);//$result = ReadFromFile (". /upload/". $fileName), echo "", Function UploadFile ($file) {//upload path     $destinationPath = "... /upload/"; if (!file_exists ($destinationPath)) {mkdir ($destinationPath, 0777);} Rename $filename = Date (' Ymdhis '). '_' . Iconv (' Utf-8 ', ' gb2312 ', basename ($file [' name '])), if (Move_uploaded_file ($file [' Tmp_name '], $destinationPath. $ FileName) {return iconv (' gb2312 ', ' utf-8 ', $fileName);} Return ';}


Code comment/*1, about basename method $path = "/testweb/home.php";//displays file name with filename extension echo basename ($path);//displays filenames without file name extensions Echo BaseName ($path, ". php"), 2, about Iconviconv (' gb2312 ', ' utf-8 ', $fileName);//convert $filename from gb2312 to utf-8 format. Note: This function needs to open the php_iconv.dll3 inside php.ini, about $_files[' myfile ']$_files equivalent to a two-dimensional array, and $_files[' myfile ' is equivalent to a one-dimensional array. So you can $f = $_files[' myfile '];echo $f [' name '], and if you directly access the $_files[' myfile ', you will be reported undefined index:myfile. Now add if (!isset ($_files[' myfile ')) {die (' upload file does not exist! ');} */


3, using Yii+iframe method
See also: http://zccst.iteye.com/blog/1271091

Attached: The relevant information found on the Internet is as follows

Http://www.cnblogs.com/spemoon/archive/2011/01/07/1930221.html

Http://wenku.baidu.com/view/80522df6ba0d4a7302763abb.html

Http://www.jb51.net/article/28427.htm

http://apps.hi.baidu.com/share/detail/20419052

  • 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.