【问题标题】:Uploading files using php very slow in xampp在xampp中使用php上传文件非常慢
【发布时间】:2015-07-08 06:27:05
【问题描述】:

我是新开发的。我选择了php来学习编码。所以我自己学习可能会出错,请澄清我的疑问。 我在使用 php 将文件上传到文件夹时遇到问题。我真正要做的是,我上传一个文件,该文件保存在一个文件夹中,并将文件名单独插入数据库中。在上传文件时,我确实将文件复制到另一个文件夹中,该文件夹将用于编辑目的,这样原始文件就不会受到干扰。我遇到的问题是,文件上传成功,名称也插入了数据库。但是即使文件的大小很小,上传也需要很长时间。当我使用本地测试时效果很好,但是当我实时进入这个问题(上传缓慢)时,我会遇到这个问题。上传负责人做的是,上传一个文件,打开一个新的浏览器,再上传另一个文件。当新浏览器打开时,文件被上传,但在以前的浏览器中它仍在处理中。我为将文件复制到另一个文件夹而编写的代码未执行,因为打开新浏览器以上传另一组文件。我正在使用 xamp cp v3.2.1。为了最大限度地减少执行时间,我将默认的最大执行时间设置为 30。但无法快速上传文件。

下面是我的php编码:

 <?php 





 // connect to the database
 include('connect-db.php');

 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $udate = mysql_real_escape_string(htmlspecialchars($_POST['udate']));
 $file_array=($_FILES['file_array']['name']);



 // check to make sure both fields are entered
 if ($udate == '' || $file_array=='')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($udate, $file_array, $error);
 }
 else
 {
     $udate = mysql_real_escape_string(htmlspecialchars($_POST['udate']));
if(isset($_FILES['file_array']))
{
    $name_arrray=$_FILES['file_array']['name'];
    $tmp_name_arrray=$_FILES['file_array']['tmp_name'];
    for($i=0;$i <count($tmp_name_arrray); $i++)
    {
        if(move_uploaded_file($tmp_name_arrray[$i],"test_uploads/".str_replace(' ','',$name_arrray[$i])))

        {

                       // save the data to the database
$j=str_replace(' ','',$name_arrray[$i]);
echo $j;
 $udate = mysql_real_escape_string(htmlspecialchars($_POST['udate']));
  $provider = mysql_real_escape_string(htmlspecialchars($_POST['provider']));
  $existfile=mysql_query("select ubatch_file from batches");
  while($existing = mysql_fetch_array( $existfile)) {
      if($j==$existing['ubatch_file'])
    echo'  <script>
function myFunction() {
    alert("file already exists");
}
</script>';

      }

 mysql_query("INSERT IGNORE batches SET udate='$udate', ubatch_file='$j',provider='$provider',privilege='$_SESSION[PRIVILEGE]'")
 or die(mysql_error()); 
        echo $name_arrray[$i]."uploaded completed"."<br>";
        $src = 'test_uploads';
$dst = 'copy_test_uploads';
$files = glob("test_uploads/*.*");
      foreach($files as $file){
      $file_to_go = str_replace($src,$dst,$file);
      copy($file, $file_to_go);

       /* echo "<script type=\"text/javascript\">
                        alert(\"CSV File has been successfully Uploaded.\");
                        window.location = \"uploadbatches1.php\"
                    </script>";*/
      }
        } else
        {
            echo "move_uploaded_file function failed for".$name_array[$i]."<br>";
        }

    }
}

 // once saved, redirect back to the view page
 header("Location:uploadbatches1.php"); 
 }
 }



?>

【问题讨论】:

  • 上传的文件有多大?
  • 为什么你的 php 脚本以 } 开头?
  • 对不起,它的 php 代码结束了。
  • max_execution_time=30 max_input_time=60 post_max_size=128M upload_max_filesize=32M max_file_uploads=20
  • 你忘记在页面顶部开始会话了!!

标签: php mysql file-upload upload


【解决方案1】:

这需要很长时间,因为每次都将所有文件复制到新文件夹中。这超出了执行时间。仅复制上传的文件可以使上传和复制文件快速。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 2016-11-26
    • 2012-01-02
    • 2023-02-13
    • 2016-05-21
    • 2012-01-15
    • 2021-07-07
    • 2013-02-01
    相关资源
    最近更新 更多