Home  >  Article  >  Backend Development  >  A PHP program code for uploading files without refreshing_PHP tutorial

A PHP program code for uploading files without refreshing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:07:41935browse

To achieve non-refresh file upload, we have an imaginary way to use the iframe framework to achieve file upload, and the other is to use ajax to achieve file upload. Both methods can achieve the non-refresh file upload function.

The most original and simple iframe upload example:

The front-end upload page index.html is mainly a form and a js callback function. When uploading files, the method and enctype attributes of the form must be the same as the following code. Then set the target value to the name of the iframe, so that files can be uploaded without refreshing.

The code is as follows
 代码如下 复制代码

 
 
 
 
上传文件 
 
 
 
<script>  <br> function CallbackFunction(str){  <br> alert("上传成功");  <br> }  <br> </script> 

 
  
  
  
 
 

Copy code

 代码如下 复制代码

set_time_limit(0); 
if($_SERVER['REQUEST_METHOD']=='POST') { 
  move_uploaded_file($_FILES["test_file"]["tmp_name"], 
  dirname($_SERVER['SCRIPT_FILENAME'])."/UploadTemp/" . $_FILES["test_file"]["name"]); 
  echo "<script>window.parent.CallbackFunction();</script>"; 

?>




Upload file



<script> <br> function CallbackFunction(str){ <br> alert("Upload successful"); <br> } <br> </script>






The background upload processing page uploadfile.php, this code is a simple upload code without error and exception handling. After the upload code is executed, you need to tell the parent page that the upload has been completed. Therefore, the callback function CallbackFunction of the parent page is called in this page. This function can have parameters and the form can be defined by yourself.
The code is as follows

Copy code
set_time_limit(0);
if($_SERVER['REQUEST_METHOD']=='POST') {
move_uploaded_file($_FILES["test_file"]["tmp_name"],
dirname($_SERVER['SCRIPT_FILENAME'])."/UploadTemp/" . $_FILES["test_file"]["name"]);
echo "<script>window.parent.CallbackFunction();</script>";
}
?> The above is a simple method to use iframe to upload files without refreshing. If you want a robust program, you need to refine it http://www.bkjia.com/PHPjc/629900.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629900.htmlTechArticleTo achieve file upload without refreshing, we have an imaginary way to use the iframe framework to upload files. Another way Just use ajax to upload files. Both methods can achieve...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn