首页 / AJAX / ajax php上传文件,上传百分比
ajax php上传文件,上传百分比
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ajax php上传文件,上传百分比,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3249字,纯文字阅读大概需要5分钟。
内容图文
我使用iframe进行简单的ajax上传表单.我想要的是,加载消息显示上传百分比在< div id =“message”>< / div>
这是我的javascript
function start_Uploading(){
document.getElementById('message').innerHTML = 'Uploading...';
return true;
}
function stopUpload(success)
{
var result = '';
if (success == 1)
document.getElementById('message').innerHTML = 'Success';
else
document.getElementById('message').innerHTML = 'Failed';
}
这是形式
< div id="message"><br/></div>
< form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="start_Uploading();" >
File:< input name="myfile" type="file" size="30" />
< input type="submit" name="submitBtn" value="Upload" />
< br/>< iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
服务器端upload.php文件是
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$result = 0;
$target_path = $destination_path . basename( $_FILES['myfile']['name']);
if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path))
$result = 1;
sleep(1);
echo "<script language=\"javascript\" type=\"text/javascript\">window.top.window.stopUpload($result);</script>";
解决方法:
两件事情:
>使用Javascript
>使用APC
使用Javascript:
You can use plupload (http://www.plupload.com/)
- pretty good multiple file uploader
Pretty decent JS plugin for uploading files in PHP in two methods,
1. normal upload
2. chunk based uploading.
限制/假设:
1. Flash Plugin - in the browser
2. Session problem - since it's flash, a different session's are created for
each file upload.
APC / PHP
使用APC – 您可以自己创建一个进度条.
限制/假设:
1. Install APC on the server - using PECL (pecl install APC)
2. Write a separate code to get the status of upload.
码:
getprogress.php
<?php
if(isset($_GET['progress_key'])) {
$status = apc_fetch('upload_'.$_GET['progress_key']);
echo $status['current']/$status['total']*100;
}
?>
upload.php的
<?php
$id = uniqid("");
?>
<html>
<head><title>Upload Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
function getProgress(){
$.get("getprogress.php?progress_key=<?php echo($id)?>",
function(percent) {
document.getElementById("progressinner").style.width = percent+"%";
if (percent < 100){
setTimeout("getProgress()", 100);
}
});
}
function startProgress(){
document.getElementById("progressouter").style.display="block";
setTimeout("getProgress()", 1000);
}
</script>
<iframe id="theframe" name="theframe"
src="fileupload.php?id=<?php echo($id) ?>"
style="border: none; height: 100px; width: 400px;" >
</iframe>
<br/><br/>
<div id="progressouter" style="width: 500px; height: 20px; border: 6px solid red; display:none;">
<div id="progressinner" style="position: relative; height: 20px; background-color: purple; width: 0%; ">
</div>
</div>
</body>
</html>
fileupload.php
<?php
$id = $_GET['id'];
?>
<form enctype="multipart/form-data" id="upload_form"
action="target.php" method="POST">
<input type="hidden" name="APC_UPLOAD_PROGRESS"
id="progress_key" value="<?php echo $id?>"/>
<input type="file" id="test_file" name="test_file"/><br/>
<input onclick="window.parent.startProgress(); return true;"
type="submit" value="Upload!"/>
</form>
内容总结
以上是互联网集市为您收集整理的ajax php上传文件,上传百分比全部内容,希望文章能够帮你解决ajax php上传文件,上传百分比所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。