如何使用进度条创建php上传文件

时间:2014-01-03 10:13:57

标签: php file-upload upload

我想知道如何在我的php上传文件中添加进度条。 这是我的代码:

form.html http://pastebin.com/embed_iframe.php?i=mukfyVSz

<form id="myForm"  action="action.php" method="post">
  <input type="file" name="upfile">
    <input type="hidden" name="MAX_FILE_SIZE" value="10000">
    <input type="submit" value="Upload">
</form>
<div style="margin-top:15px;" id="htmlExampleTarget"></div>

    <script>
    // prepare the form when the DOM is ready
$(document).ready(function() {
    // bind form using ajaxForm
    $('#myForm').ajaxForm({
        // target identifies the element(s) to update with the server response
        target: '#htmlExampleTarget',

        // success identifies the function to invoke when the server response
        // has been received; here we apply a fade-in effect to the new content
       // success: function(data) {
        //    $('.data').html(data);
        //}
    });
});
    </script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>

action.php的 http://pastebin.com/vhcH7rTT

<?php

$upload_dir = "./uploads"; // upload folder
$file_name = $_FILES["upfile"]["name"];
 // MIME TYPES LIST: http://reference.sitepoint.com/html/mime-types-full
 $allowed_types = array("image/gif","image/jpeg","text/html","image/png");
if(!in_array($_FILES["upfile"]["type"],$allowed_types)) {
    die("Upload non consentito per questo tipo di file. ");
}

if(trim($_FILES["upfile"]["name"]) == "") {
die("Error1");
}

if(@is_uploaded_file($_FILES["upfile"]["tmp_name"])) {
    @move_uploaded_file($_FILES["upfile"]["tmp_name"], "$upload_dir/$file_name")
    or die("Error2");
} else {
    die("Problems with " . $_FILES["upfile"]["name"]);
}

echo "<div [class='alert alert-success'>Upload with success " . $_FILES["upfile"]"name"] . "</div>";
?>

1 个答案:

答案 0 :(得分:0)

jquery文件上传更符合您的需求,

只需浏览插件页面:

http://blueimp.github.io/jQuery-File-Upload/