上传多个文件需要很长时间在ftp

时间:2017-06-03 14:24:53

标签: php ftp

我想在php中使用ftp 上传多个文件。只是想确保我正确地做到这一点或者做得很好。我可以在我的localhost上传文件,但在将项目传输到服务器之后,上传需要时间。

有时,如果我上传的文件超过1个,它也会给出错误unable to connect to ftp。以下是我所做的代码。

for($i=0; $i < count($_FILES['uploaded']['name']); $i++){
    $array = array($_FILES['uploaded']['name']);
    $tmpFilePath = $_FILES['uploaded']['tmp_name'][$i];
    $filesize = $_FILES['uploaded']['size'][$i];

    if($filesize > 10485760){
      $msgError[] = "Exceed maximum file size!";    
    }   

    if($tmpFilePath != ""){
      $shortname = $_FILES['uploaded']['name'][$i];

      $filePath = $date.'-'.$_FILES['uploaded']['name'][$i];

      // Start FTP Connection 
      require "../ftp-conn.php";

      $file = $tmpFilePath;

      $remote_file = "/public_html/procurement/uploads/" . $filePath;

      // turn passive mode on
      ftp_pasv($conn_id, true);

      // upload a file
      if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
        echo "successfully uploaded $file\n";
      } else {
        die ("There was a problem while uploading $file\n");
      }

      // close the connection
        ftp_close($conn_id);
      }

}

对此有更好的解决方案吗?

  

更新代码:

// Start FTP Connection 
$ftp_server = "ftp.myweb.com";
$ftp_user_name = 'user';
$ftp_user_pass = 'password';

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or

die("Couldn't connect to $ftp_server"); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// turn passive mode on
ftp_pasv($conn_id, true);

//Function to check file size and upload file if success
for($i=0; $i < count($_FILES['uploaded']['name']); $i++){
    $tmpFilePath = $_FILES['uploaded']['tmp_name'][$i];
    $filesize = $_FILES['uploaded']['size'][$i];

    if($filesize > 10485760){
        $msgError[] = "Exceed maximum file size!";
    }

    if($tmpFilePath != ""){
        $shortname = $_FILES['uploaded']['name'][$i];
        $filePath = $date.'-'.$_FILES['uploaded']['name'][$i];

        $file = $tmpFilePath;
        $remote_file = "/public_html/procurement/uploads/" . $filePath;

        if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
            echo "successfully uploaded $file\n";
        } else {
            die ("There was a problem while uploading $file\n");
        }

    }

}

// close the connection
ftp_close($conn_id);

0 个答案:

没有答案