【问题标题】:uploading file to server using php and html [duplicate]使用php和html将文件上传到服务器[重复]
【发布时间】:2016-03-30 22:16:37
【问题描述】:

我在使用 php 将文件上传到服务器时遇到问题 我的代码工作正常并且确保它很快,因为我将它上传到本地主机但是我如何创建一个进度条来更新文件正在上传的用户。谢谢。我找到了从其他网站上传的代码

                $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();

                }

【问题讨论】:

  • 为了做到这一点,你必须使用一些 javascript/jquery...查看theseexamples...

标签: php html


【解决方案1】:

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

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.

【讨论】:

    【解决方案2】:

    您必须使用 JavaScript/Jquery 来实现文件上传,进度低于您解决方案的最佳示例。

    Click here to download script with demo

    谢谢!

    【讨论】:

      猜你喜欢
      • 2019-06-30
      • 1970-01-01
      • 2016-03-01
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 2021-02-03
      相关资源
      最近更新 更多