2014-11-27 5 views
1

안녕하세요, 내 서버에 데이터를 업로드하려고하지만 내 요구 사항은 업로드 된 콘텐츠가 큰 파일이고 연결이 끊어지면 같은 파일을 업로드해야합니다. 멈춰서 다시 시작하지 마십시오. 내 파일을 정상적으로 서버에 업로드 할 수는 있지만 업로드를 재개 할 수 없습니다.이 작업을 수행 할 수있는 다른 방법이 있습니까 ?? 다음은 내 코드입니다.각 데이터 덩어리를 모두 저장 PHP

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) { 
 
    \t mysql_query("INSERT INTO uploads(chunk) VALUES('')") or die('insert query: ' . mysql_error()); 
 

 
\t $last = mysql_insert_id(); 
 
\t $file = '/tmp/'.$_FILES['file']['name']; // file where it is saved after chunking 
 
\t $buffer = ''; 
 
    $cnt =0; 
 
    
 
    $handle = fopen($filename, 'rb'); 
 
\t $a=file_get_contents($filename); 
 
\t 
 
\t $sql=mysql_fetch_array(mysql_query("select chunk from uploads where id='26'")); 
 
\t $b= $sql['chunk']; 
 
\t 
 
\t if(strcmp($a,$b)==0) // to check if file chunked in folder        and database data are same 
 
\t { 
 
\t echo "success"; 
 
\t 
 
\t } 
 
\t else 
 
\t { 
 
\t echo "failure"; 
 
\t 
 
\t } 
 
\t //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(); 
 
\t 
 
     if ($retbytes) { 
 
     $cnt += strlen($buffer); 
 
\t \t //echo $cnt; 
 
     } 
 
\t 
 
    } 
 
\t 
 
    $status = fclose($handle); 
 
    if ($retbytes && $status) { 
 
\t //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>

나는 로컬 호스트에있는 코드가 작동 할 수 있지만 내 데이터 청크와 서버에 저장되지 않기 때문에 내가 서버를 사용하여 문제가

+0

업로드 할 파일이 악의적 인 PHP 스크립트가 아닌지 유효성을 검사하는 것을 잊지 마세요. – Paradoxis

+0

@ 패러독스 : 이것은 단지 테스트 파일 일뿐입니다. 주 프로젝트를 구현하는 데 사용됩니다. – Rebecca

+1

원하는 프로젝트가 구현되지 않은 것 같습니다. 당신이 찾아야 할 것은 파일 청킹을 지원하는 프로젝트입니다. –

답변

0

jQuery를 사용하여 파일을 문자열로 읽은 다음 해당 문자열을 조각으로 잘라서 각 조각을 별도로 업로드해야합니다.

+0

오디오 및 비디오에도 적용 할 수 있습니까 ?? – Rebecca

+0

오디오 및 비디오도 파일이므로 프로그래밍 관점과 차이가 없습니다. –