php 伪ajax 上传文件 实现无刷新上传文件

php技巧

发现一个php 伪ajax 上传文件示例,写得比较拖沓,我只把其中有用的部分提取出来,实现无刷新上传文件

 先看代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
 
$upload_dir = 'savedir';  //文件存储的路径
 
 
//处理上传的文件
if (isset($_POST['fileframe']))
{
	$result='ERROR';
	$result_msg = 'No FILE field found';
	if(isset($_FILES['file']))
	{
		if ($_FILES['file']['error'] == UPLOAD_ERR_OK)
{$filename = $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $upload_dir.'/'.$filename); 
$result = 'OK';
}
elseif ($_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE)
$result_msg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
else 
$result_msg = 'Unknown error';
	}
 
echo '<html><head><title>-</title></head><body>';
echo '<script language="JavaScript" type="text/javascript">'."\n";
echo 'var parDoc = window.parent.document;';
 
if ($result == 'OK')
{
 echo 'parDoc.getElementById("upload_status").innerHTML = "文件[ '.$filename.' ]上传完成";';
 echo 'parDoc.getElementById("file").form.reset();';
echo 'parDoc.getElementById("file").disabled = false;';
 }
 else
 {
 echo 'parDoc.getElementById("upload_status").innerHTML = "ERROR: '.$result_msg.'";';
 }
 
 echo "\n".'</script></body></html>';
 exit();
}
?>
<!-- Beginning of main page -->
<html><head>
<title>IFRAME 无刷新上传文件示例</title>
</head>
<body>
 
<h1>上传文件:</h1>
<p>文件在选择完成后即刻进行上传 </p>
 
<form action="<?=$PHP_SELF?>" target="upload_iframe" method="post" enctype="multipart/form-data">
 <input type="hidden" name="fileframe" value="true">
 <!-- Target of the form is set to hidden iframe -->
 <!-- From will send its post data to fileframe section of this PHP script (see above) -->
 
 <label for="file">text file uploader:</label><br>
 <!-- JavaScript is called by OnChange attribute -->
 <input type="file" name="file" id="file" onChange="jsUpload(this)">
</form>
<script type="text/javascript">
/* This function is called when user selects file in file dialog */
function jsUpload(upload_field)
{
 // this is just an example of checking file extensions
 // if you do not need extension checking, remove 
 // everything down to line
 // upload_field.form.submit();
 
 var re_text = /\.txt|\.xml|\.zip/i;
 var filename = upload_field.value;
 
 /* Checking file type */
 if (filename.search(re_text) == -1)
 {
 alert("File does not have text(txt, xml, zip) extension");
 upload_field.form.reset();
 return false;
 }
 
 upload_field.form.submit();
upload_status.innerHTML = "文件上传中...";
 upload_field.disabled = true;
 return true;
}
</script>
<iframe name="upload_iframe" style="width: 400px; height: 100px; display: none;">
</iframe>
<!-- For debugging purposes, it's often useful to remove
"display: none" from style="" attribute -->
 
<br>
上传状态:<br>
<span id="upload_status">请选择文件</span>
<br><br>
 
Example by <a href="http://blog.asdjkl.net/">jason's blog</a>
</body>
</html>

我只是给出一个php ajax 文件无刷新上传的例子,这里面还有其他很多工作要补充的
比如:文件存储的目录,文件的类别判断,文件上传成功后做好记录,等等这些工作,根据你的实际需要进行调整。

Leave a Reply

Icons by N.Design Studio. Designed By Ben Swift. Powered by WordPress and Free WordPress Themes
Entries RSS Comments RSS Log in