Php implements the function of uploading files without refreshing

Source: Internet
Author: User
Tags custom name
Some time ago I made an ajax application, which involves an operation to upload a file and then read the relevant content from the file. It is not difficult for php to upload files, but it is difficult to refresh the page. if you want to upload files, you can find a method under baidu. I sorted out my ideas and shared them with my friends. I also liked SyntaxHighlighter. all (); an ajax application was created some time ago, involving an operation to upload a file and then read the relevant content from the file. It is not difficult for php to upload files, but it is difficult to refresh the page. if you want to upload files, you can find a method under baidu. I have sorted out my ideas and shared them with my friends. I also welcome to give some criticism and guidance.
Tip: Use the target attribute and iframe of the form.
If you do not like reading large sections of code, you can think for yourself. The test program is provided at the bottom of the article.

1. a php method for uploading files.
This method accepts a $ file parameter, which is the $ _ FILES variable obtained from the client. the renamed file name is returned. if the upload fails, an empty string is returned.
Php code

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;
}

II. client HTML code
Here is the trick. add another iframe for implementation. The form tag form defines a target attribute, which is interpreted as follows:
[Pre] target attributes:
_ Blank ---------- new window
_ Self ----------- self
_ Top ------------ Main framework
_ Parent --------- parent framework
The custom name ----- appears in the framework structure, and the link is opened in the framework of the name.

In this example, the iframe name is used, so the link is opened in the iframe when the form is submitted
Feel refreshed)
Call the startUpload method when submitting a form. of course, this is defined by JS.
[/Pre] [pre] In addition, we also define a span to display the prompt information. The code is as follows:
[/Pre] xhtml code

Import file:
Iframe> <br> & nbsp; form> span> <br> 3. JS part <br> This part is relatively simple, only display the prompt message <br> javascript code <br> & nbsp; function startUpload () {<br> & nbsp; & nbsp; var spanObj = document. getElementById ("info"); & nbsp; spanObj. innerHTML = "start upload"; <br> & nbsp; & nbsp ;}< br> & nbsp; function stopUpload (responseText) {& nbsp; var spa NObj = document. getElementById ("info"); <br> & nbsp; spanObj. innerHTML = "uploaded successfully; & nbsp; spanObj. innerHTML = responseText; <br> & nbsp; & nbsp ;}< br> The server must handle the issue. <Br> <br style = "font-weight: bold;"> 4. server segment processing <br> php code <br> & nbsp; & nbsp; $ file =$ _ FILES [myfile]; & nbsp; $ fileName = uploadFile ($ file); <br> & nbsp; & nbsp; $ result = readFromFile (". /upload /". $ fileName); <br> a js code should be added later to call the stopUpload method. <Br> javascript code <br> & nbsp; window. top. window. stopUpload (""); <br> The final result is to add the readFromFile method in php. <Br> php code <br> & nbsp; function readFromFile ($ target_path) {<br> & nbsp; & nbsp; // read file content & nbsp; $ file = fopen ($ target_path, r) or die ("unable to open file"); <br> & nbsp; $ fileContent =; & nbsp; while (! Feof ($ file) <br> & nbsp; & nbsp ;{& nbsp; & nbsp; & nbsp; $ str = fgets ($ file); <br> & nbsp; & nbsp; $ fileContent. = $ str; & nbsp ;}< br> & nbsp; & nbsp; fclose ($ file); & nbsp; return $ fileContent; <br> & nbsp; & nbsp ;}< br> </li>

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.