使用php和html将文件上传到服务器

时间:2015-12-24 13:07:38

标签: php html

我有使用php将文件上传到服务器的问题 我的代码工作正常,并确保它的快速,因为我上传到localhost但我怎么能创建一个进度条的东西,将更新文件正在上传的用户。谢谢。香港专业教育学院找到了从其他网站上传的代码

                $file = rand(1000,100000)."-".$_FILES['file']['name'];
                $file_loc = $_FILES['file']['tmp_name'];
                $file_size = $_FILES['file']['size'];
                $file_type = $_FILES['file']['type'];
                $folder="uploads/";
                $new_size = $file_size/1024;  
                $new_file_name = strtolower($file);
                $final_file=str_replace(' ','-',$new_file_name);
                if(move_uploaded_file($file_loc,$folder.$final_file))
                {
                    require_once 'dbconfig.php';
                    try {
                    $conn = new PDO("mysql:host=$host;dbname=$dbname",$username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
                    $stmt = $conn->prepare("CALL sp_insertdocument (?,?,?,?,?,?,?,?,?)");

                    $stmt->bindParam(1, $doctitle, PDO::PARAM_STR, 30);
                    $stmt->bindParam(2, $doctype, PDO::PARAM_STR, 30);
                    $stmt->bindParam(3, $doccontent, PDO::PARAM_STR, 30);
                    $stmt->bindParam(4, $datefilled, PDO::PARAM_STR, 30);
                    $stmt->bindParam(5, $request, PDO::PARAM_STR, 30); 
                    $stmt->bindParam(6, $staffid, PDO::PARAM_STR, 30); 
                    $stmt->bindParam(7, $final_file, PDO::PARAM_STR, 30); 
                    $stmt->bindParam(8, $file_type, PDO::PARAM_STR, 30); 
                    $stmt->bindParam(9, $new_size, PDO::PARAM_STR, 30); 
                    $stmt->execute();

                }

2 个答案:

答案 0 :(得分:0)

我在大多数项目中都使用了以下内容。请尝试以下方法:

ajax = new XMLHttpRequest();
ajax.onreadystatechange = function ()
{

    if (ajax.status) {

    if (ajax.status == 200 && (ajax.readyState == 4))
    {
        //To do tasks if any if upload is completed
    }
    }
}
ajax.upload.addEventListener("progress", function (event) {

    var percent = (event.loaded / event.total) * 100;
    //percent variable can be used for modifying the length of your progress bar.
    console.log(percent);

});

ajax.open("POST", 'your file upload link', true);
ajax.send(formData);
//ajax.send id for sending upload form data.

答案 1 :(得分:0)

您已使用JavaScript / Jquery实现fileupload,其进度低于解决方案的最佳示例。

Click here to download script with demo

谢谢!