1

こんにちは、サーバーにデータをアップロードしようとしていますが、アップロードされたコンテンツが大きなファイルであり、接続が切断された場合は、停止した場所から同じファイルをアップロードし、再度開始しないようにする必要があります。ファイルをサーバーに正常にアップロードできますが、アップロードを再開できません.これを行う他の方法はありますか?? 以下は私のコードです。

RESUME.PHP

    <?php

    $host = "localhost";
    $db_uname = "root";
    $db_pass="";
    $db_name = "upload";
    $cont=mysql_pconnect($host,$db_uname,$db_pass) or die("Too many connection, please try later ");
mysql_select_db($db_name);
 
    ob_clean();
    if($_POST['submit'])
    {

     define('CHUNK_SIZE', 1024*1024); // Size (in bytes) of tiles chunk

    // Read a file and display its content chunk by chunk
      function readfile_chunked($filename, $retbytes = TRUE) {
    	mysql_query("INSERT INTO uploads(chunk) VALUES('')") or die('insert query: ' . mysql_error());

	$last = mysql_insert_id();
	$file = '/tmp/'.$_FILES['file']['name']; // file where it is saved after chunking
	$buffer = '';
    $cnt =0;
   
    $handle = fopen($filename, 'rb');
	$a=file_get_contents($filename);
	
	$sql=mysql_fetch_array(mysql_query("select chunk from uploads where id='26'"));
	$b= $sql['chunk'];
	
	if(strcmp($a,$b)==0) // to check if file chunked in folder                               and database data are same
	{
	echo "success";
	
	}
	else
	{
	echo "failure";
	
	}
	//exit;
    if ($handle === false) {
      return false;
    }
    while (!feof($handle)) {
      $buffer = fread($handle, CHUNK_SIZE);
   
    // Open the file to get existing content
    $current = file_get_contents($file);
    // Append a new person to the file
     $current .= $buffer;
    // Write the contents back to the file
    file_put_contents($file, $current);
    mysql_query("update uploads set chunk =concat(chunk,'".mysql_real_escape_string($buffer)."') where id='$last'") or die('update query: ' . mysql_error());
        ob_flush();
      flush();
	 
      if ($retbytes) {
        $cnt += strlen($buffer);
		//echo $cnt;
      }
	 
    }
	
    $status = fclose($handle);
    if ($retbytes && $status) {
	//echo $cnt;
      return $cnt; // return num. bytes delivered like                               readfile() does.
    }
    return $status;
  }


  
    $filename = $_FILES['file']['tmp_name'];
    readfile_chunked($filename);
 }
?>

<form action="resume.php" method="post"
                        enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit"  name="submit" value="Upload File" />
</form>

コードを localhost で動作させることはできますが、データがチャンク化されておらず、サーバーに保存されていないため、サーバーで使用する際に問題があります。

4

2 に答える 2

0

この「resumablejs」 http://resumablejs.com/を試すことができます。データのチャンクを作成してサーバー側に送信し、サーバー側から受信したチャンクから元のファイルを作成できます。

于 2021-07-29T12:46:42.407 に答える