Today, I spent a little time studying flex file uploading and using PHP in the background. The code in this article is to integrate the online
Some codes found are reprinted and reprinted. The original texts are not available and will not be listed here. Thank you for sharing them with others.
Learn from them.
1. First, we will introduce some basic knowledge, including the global variable $ _ FILES array at the PHP end.
$ _ FILES ['userfile'] ['name'] the original name of the client machine file.
$ _ FILES ['userfile'] ['type'] indicates the MIME type of the file, which must be supported by the browser, for example, "image/GIF ".
$ _ FILES ['userfile'] ['SIZE'] size of the uploaded file, in bytes.
$ _ FILES ['userfile'] ['tmp _ name'] temporary file name stored on the server after the file is uploaded.
$ _ FILES ['userfile'] ['error'] Error Code related to the file upload.
2. php file upload size setting
File_uploads =
On // whether the system supports File Upload
; Upload_tmp_dir // storage path of temporary files. If not set, it is the default path of the system.
Upload_max_filesize = 2 m // maximum size of File Upload allowed
Post_max_size = 2 m
// The maximum data capacity that PHP can accept when the POST method is used for PHP
Max_execution_time = 30
// Maximum execution time of each script
Memory_limit = 8 m
// Maximum memory that each script can consume
The above values are the default values of PHP. ini. If we want to upload a larger file, we need to modify some specific parameters.
Generally, the information of the uploaded files is saved in the $ _ FILES array. Let's take a look at how to handle it on the PHP side. We know that the client uploads
The file is saved in the default temporary folder of the system. Our goal is to copy the files in the Temporary Folder to the address we need to save.
Let's take a look at the PHP code and explain it one by one:
<? Php <br/> echo "/n temporary file name = ". $ _ FILES ['filedata'] ['tmp _ name']. "/N"; <br/> echo "file name = ". $ _ FILES ['filedata'] ['name']. "/N"; <br/> echo "file size = ". $ _ FILES ['filedata'] ['SIZE']. "/N"; <br/> echo "attempting to move file... /n "; <br/> $ uploaddir = '. /upload/images/'; <br/> // $ filename = date ("ymdhis "). rand (100,999); <br/> $ filename = date ("ymdhis "). mt_rand (1000,9999); <br/> $ upload File = $ uploaddir. $ filename. substr ($ _ FILES ['filedata'] ["name"], strrpos ($ _ FILES ['filedata'] ["name"], ". "); <br/> if ($ uploadfile) {<br/> $ file_size_max = 8*1024*1024; // 8 MB limit the maximum file upload capacity (bytes) <br/> $ accept_overwrite = 1; // whether to overwrite the same file <br/> if ($ upload_file_size> $ file_size_max) {<br/> echo "sorry, your file size exceeds the specified upload limit "; <br/> exit; <br/>}< br/> If (file_exists ($ uploadfile) & $ accept_overwrite) {<br/> Ech O "the same file name exists"; <br/> exit; <br/>}< br/> $ moved = move_uploaded_file ($ _ FILES ['filedata'] ['tmp _ name'], $ uploadfile ); <br/> If (empty ($ moved) <br/>{< br/> echo "file copy failed"; <br/> exit; <br/>}< br/> echo "file moved ". $ moved. "/N"; <br/> $ errorno = $ _ FILES ['upload _ file'] ['error']; <br/> switch ($ errorno) {<br/> case 0: <br/> echo "uploaded successfully"; break; <br/> case 1: <br/> echo "the uploaded file exceeds PHP. the value of the upload_max_filesize option in ini. "; Break; <br/> case 2: <br/> echo "the size of the uploaded file exceeds the value specified by the max_file_size option in the HTML form. "; Break; <br/> case 3: <br/> echo" only part of the file is uploaded "; break; <br/> case 4: <br/> echo "no file is uploaded"; break; <br/>}< br/>?>
The PHP code is relatively simple. It generates a unique file name for the uploaded file and makes a simple judgment on the file size and file name uniqueness,
Finally, use the move_upload_flie function of PHP to move files. The Rand and mt_rand functions can be used for the random number. It is said that mt_rand
It is much faster than Rand. If you are interested, you can write a test program to test it.
Now we are switching to the front-end flex processing. Flex uses the ActionScript language + XML language. The Code contains detailed comments and is not described in detail.
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" <br/> backgroundcolor = "# ffffff"> <br/> <mx: style source = "styles/styles.css"/> <br/> <mx: hrule x = "10" Y = "37" width = "90%"/> <br/> <mx: text x = "10" Y = "10" text = "uploading a file" stylename = "headerstyle" id = "label1"/> <br/> <mx: SCRIPT> <br/> <! -- [CDATA [<br/> // enter the path to the file upload script on your server <br/> Public var uploadfile: String = "http: // localhost: 8080/PHP/upanddownload/PHP/file_upload.php "; <br/>]> <br/> </MX: SCRIPT> <br/> <mx: button x = "10" Y = "70" label = "Upload" Click = "{upload ()}"/> <br/> <mx: button x = "83" Y = "70" label = "Check If php file exists" Click = "{test ()}"/> <br/> <mx: SCRIPT> <br/> <! [CDATA [<br/> Import flash.net. navigatetoURL; <br/> Import flash. events. dataevent; <br/> Import MX. events. closeevent; <br/> Import MX. controls. alert; <br/> Import flash. events. *; <br/> // we declare the file reference here so it is not destroyed by memory garbage collection <br/> Public var fileref: filereference = new filereference (); <br/> // a class that is similar to a HTML form <br/> Public var request: URLRequest; </P> <p> // opens a browser window for the user to select a file to upload <br/> Public Function upload (): void {<br/> // listen for the upload events <br/> fileref. addeventlistener (event. select, selecthandler); fileref. addeventlistener (event. open, openhandler); fileref. addeventlistener (progressevent. progress, progresshandler); fileref. addeventlistener (event. complete, completehandler); fileref. A Ddeventlistener (dataevent. upload_complete_data, uploadcompletehandler); fileref. addeventlistener (securityerrorevent. security_error, httpsecurityerrorhandler); fileref. addeventlistener (httpstatusevent. http_status, httperrorhandler); fileref. addeventlistener (ioerrorevent. io_error, httpioerrorhandler); <br/> // browse for the file to upload <br/> // when user selects a file the select handler is called <br /> Try {<br/> var imagetypes: filefilter = new filefilter ("Images (*. JPG ,*. JPEG ,*. PNG )","*. JPG ;*. JPEG ;*. PNG "); <br/> var alltypes: array = new array (imagetypes); <br/> var success: Boolean = fileref. browse (alltypes); <br/>}< br/> catch (error: Error) {<br/> trace ("unable to browse for files. "); <br/> textarea1.text =" unable to browse for files. "; <br/>}< br/> // checks that the upload PHP fi Le is where we think it is <br/> Public Function Test (): void {<br/> request = new URLRequest (uploadfile); <br/> navigatetoURL (request, "_ blank "); <br/>}</P> <p> // when a file is selected we upload the file to the PHP file upload script on the server <br/> Public Function selecthandler (event: event): void {</P> <p> try {<br/> // Upload File <br/> alert. show ("Upload" + fileref. name + "(total" + math. round (filere F. Size) + "bytes )? ", <Br/>" Confirm upload ", <br/> alert. yes | alert. no, <br/> null, <br/> proceedwithupload); <br/> // fileref. upload (request); <br/> textarea1.text = "uploading" + fileref. name + "... "; <br/>}< br/> catch (error: Error) {<br/> // vague <br/> trace (" unable to upload file. "); <br/> textarea1.text + ="/nunable to upload file. "; <br/>}< br/> private function proceedwithupload (E: closeevent): void {<br/> If (E. detail = alert. yes) {<br/> request = new URLRequest (uploadfile); <br/> try {<br/> fileref. upload (request); <br/>}catch (error: Error) {<br/> alert. show ("Upload Failed"); <br/>}< br/> // dispatched during file open. <br/> Public Function openhandler (Event: Event): void {<br/> trace ("file opened"); <br/> textarea1.text + = "/nfile opened "; <br/>}</P> <p> // dispatched during File Upload <br/> Public Function progresshandler (Event: progressevent ): void {<br/> trace ("File Upload in progress (" + event. bytesloaded + "of" + event. bytestotal + ")"); <br/> textarea1.text + = "/nfile upload in progress (" + event. bytesloaded + "of" + event. bytestotal + ")"; <br/> lbprogress. TEXT = "uploaded" + event. bytesloaded <br/> + "bytes, total" + event. bytestotal + "Byte"; <br/> var proc: uint = event. bytesloaded/event. bytestotal * 100; <br/> bar. setprogress (Proc, 100); <br/> bar. label = "current progress:" + "" + proc + "% "; <br/>}< br/> // dispatched when the file has been given to the server script <br/> // This event does not receive a response from the server <br //> // use dataevent. upload_complete_data event as shown in uploadcompletehandler <br/> Public Function completehandler (Event: Event): void {<br/> trace ("File Uploaded "); <br/> textarea1.text + = "/nfile uploaded"; <br/> alert. show ("congratulations, upload successful "); <br/>}< br/> // dispatched when a file upload has completed <br/> // This event can contain in a response from the server as opposed to the event. complete Event <br/> // The PHP Upload file can send back information if we want it to <br/> // the event. data and event. text properties wowould contain a response if any <br/> Public Function uploadcompletehandler (Event: dataevent): void {<br/> trace ("information about upload: /n "+ String (event. text); <br/> textarea1.text + = "/ninformation about upload/N" + event. text as string; <br/>}</P> <p> // dispatched when an HTTP Error occurs <br/> Public Function httperrorhandler (Event: httpstatusevent ): void {<br/> trace ("HTTP Error occured" + event. status); <br/> textarea1.text + = "/nhttp error occured-" + event. status; <br/>}</P> <p> // dispatched when an HTTP Io error occurs <br/> Public Function httpioerrorhandler (Event: ioerrorevent ): void {<br/> trace ("HTTP Io error occured-" + event. text); <br/> textarea1.text + = "/nhttp Io error occured-" + event. text; <br/>}</P> <p> // dispatched when an HTTP Io error occurs <br/> Public Function httpsecurityerrorhandler (Event: securityerrorevent ): void {<br/> trace ("HTTP security error occured-" + event. text); <br/> textarea1.text + = "/nhttp security error occured-" + event. text; <br/>}</P> <p>] --> <br/> </MX: SCRIPT> </P> <p> <mx: textarea x = "10" Y = "100" width = "90%" Height = "200" id = "textarea1"/> <br/> <mx: label id = "lbprogress" text = "Upload" x = "10" Y = "321" fontsize = "12"/> <br/> <mx: progressbar id = "bar" labelplacement = "bottom" themecolor = "# f20d7a" <br/> Minimum = "0" visible = "true" Maximum = "100" label = "Current progress: 0% "<br/> direction =" right "mode =" Manual "width =" 200 "x =" 10 "Y =" 349 "fontsize =" 12 "/> </ p> <p> </MX: application> <br/>