Use the SWFUpload plug-in to upload files and swfupload to upload files
The Demo code consists of two parts, including foreground and background files:
1.front file index.html
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2. Background File upload. php
<? Php // pass the session value (because Flash is incompatible with the session, it can only be obtained through parameter transfer) if (isset ($ _ POST ["PHPSESSID"]) {session_id ($ _ POST ["PHPSESSID"]);} else if (isset ($ _ GET ["PHPSESSID"]) {session_id ($ _ GET ["PHPSESSID"]);} session_start (); // set the maximum value of POST $ POST_MAX_SIZE = ini_get ('Post _ max_size '); $ unit = strtoupper (substr ($ POST_MAX_SIZE,-1); $ multiplier = ($ unit = 'M '? 1048576: ($ unit = 'K '? 1024: ($ unit = 'G '? 1073741824: 1); if (int) $ _ SERVER ['content _ length']> $ multiplier * (int) $ POST_MAX_SIZE & $ POST_MAX_SIZE) {header ("HTTP/1.1 500 Internal Server Error"); echo "POST exceeded maximum allowed size. "; exit (0);} // Basic settings $ save_path = getcwd (). "/file/"; // file upload location $ upload_name = "Filedata"; $ max_file_size_in_bytes = 2147483647; // 2 GB $ extension_whitelist = array ("doc", "txt ", "jpg", "gif", "png ");/ /File type $ valid_chars_regex = '. A-Z0-9 _! @ # $ % ^ & () + ={}\ [\] \ ', ~ '-'; // File name rules // other variables $ MAX_FILENAME_LENGTH = 260; $ file_name = ""; $ file_extension = ""; $ uploadErrors = array (0 => "File Uploaded successfully", 1 => "the uploaded file exceeds php. the setting in upload_max_filesize directive in the ini file ", 2 =>" the uploaded file exceeds the setting in MAX_FILE_SIZE directive in the HTML form file ", 3 => "only part of the uploaded file", 4 => "No File Upload", 6 => "Temporary Folder missing "); // check whether the file is uploaded correctly if (! Isset ($ _ FILES [$ upload_name]) {HandleError ("No upload found in \ $ _ FILES ". $ upload_name); exit (0);} else if (isset ($ _ FILES [$ upload_name] ["error"]) & $ _ FILES [$ upload_name] ["error"]! = 0) {HandleError ($ uploadErrors [$ _ FILES [$ upload_name] ["error"]); exit (0);} else if (! Isset ($ _ FILES [$ upload_name] ["tmp_name"]) |! @ Is_uploaded_file ($ _ FILES [$ upload_name] ["tmp_name"]) {HandleError ("Upload failed is_uploaded_file test."); exit (0);} else if (! Isset ($ _ FILES [$ upload_name] ['name']) {HandleError ("File has no name. "); exit (0);} // check the file size $ file_size = @ filesize ($ _ FILES [$ upload_name] [" tmp_name "]); if (! $ File_size | $ file_size> $ max_file_size_in_bytes) {HandleError ("File exceeds the maximum allowed size"); exit (0);} if ($ file_size <= 0) {HandleError ("File size outside allowed lower bound"); exit (0);} // The detection File name is blank $ file_name = preg_replace ('/[^ '. $ valid_chars_regex. '] | \. + $/I ', "", basename ($ _ FILES [$ upload_name] ['name']); if (strlen ($ file_name) = 0 | strlen ($ file_name)> $ MAX_FILENAME_LENGTH) {HandleError ("Invalid file name"); exit (0) ;}// check if (file_exists ($ save_path. $ file_name) {HandleError ("File with this name already exists"); exit (0 );} // check the suffix $ path_info = pathinfo ($ _ FILES [$ upload_name] ['name']); $ file_extension = $ path_info ["extension"]; $ is_valid_extension = false; foreach ($ extension_whitelist as $ extension) {if (strcasecmp ($ file_extension, $ extension) = 0) {$ is_v Alid_extension = true; break ;}} if (! $ Is_valid_extension) {HandleError ("Invalid file extension"); exit (0) ;}// save the file if (! @ Move_uploaded_file ($ _ FILES [$ upload_name] ["tmp_name"], $ save_path. $ file_name) {HandleError ("the file cannot be saved. "); exit (0);} // echo" File already ed "is successfully output; exit (0 );
Function HandleError ($ message) {header ("HTTP/1.1 500 Internal Server Error"); echo $ message;}?>