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 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
<Form id = "upForm" action = "upload. php "method =" post "enctype =" multipart/form-data "target =" upload_target "onsubmit =" startUpload () "> Import file: <input type = "file" name = "myfile" id = "myfile"/>
<Input type = "submit" name = "submitBtn" value = "import"/> <iframe id = "upload_target" name = "upload_target" src = "#" style = "width: 0; height: 0; border: 0px solid # fff; "> iframe>
Form> <span id = "info"> span>
Iii. JS part
This part is relatively simple, just display the prompt information
Javascript code
Function startUpload (){
Var spanObj = document. getElementById ("info"); spanObj. innerHTML = "Start upload ";
}
Function stopUpload (responseText) {var spanObj = document. getElementById ("info ");
SpanObj. innerHTML = "Upload succeeded; spanObj. innerHTML = responseText;
}
Next, we need to handle the issue on the server.
Iv. Server segment Processing
Php code
$ File = $ _ FILES [myfile]; $ fileName = uploadFile ($ file );
$ Result = readFromFile ("./upload/". $ fileName );
In addition, you should add a JS Code later to call the stopUpload method.
Javascript code
Window. top. window. stopUpload ("");
Finally, the php readFromFile method is completed.
Php code
Function readFromFile ($ target_path ){
// Read the file content $ file = fopen ($ target_path, r) or die ("unable to open file ");
$ FileContent =; while (! Feof ($ file ))
{$ Str = fgets ($ file );
$ FileContent. = $ str ;}
Fclose ($ file); return $ fileContent;
}