2012-08-30 18 views
0

file.html이라는 파일이있는 경우 PHP를 통해이 파일의 복제본을 어떻게 만드나요? file1 .... file10로 바꿉니 까?PHP 대용량 파일 복사

$filename = 'file.html' 
$copyname = 'file2.html' 
if ($file = @fopen($copyname, 'x')) { 
    // We've successfully created a file, so it's ours. We'll close 
    // our handle. 
    if ([email protected]($file)) { 
     // There was some problem with our file handle. 
     return false; 
    } 

    // Now we copy over the file we created. 
    if ([email protected]($filename, $copyname)) { 
     // The copy failed, even though we own the file, so we'll clean 
     // up by itrying to remove the file and report failure. 
     unlink($copyname); 
     return false; 
    } 

    return true; 
} 
+1

프로그램을 디버그하는 데 도움이되는 오류 메시지를 숨기는 @ 기호를 제거하십시오. – tomsv

답변

2

작은 파일 접근이 당신이 그것을 저장하기 전에 파일의 내용으로 뭔가를 할 수 있습니다 : 이것은 당신이 파일의 내용에 접근하는 것을 허용하지 않습니다 접근 방식은

$text = file_get_contents('file.html'); 
for($i = 0; $i < 100; $i++) { 
    file_put_contents('file'.$i.'.html', $data); 
} 

큰 파일

for($i = 0; $i < 100; $i++) { 
    copy('file.html', 'file'.$i.'.html'); 
} 
+0

@file_put_contents ('file'. $ i. '.html'); // 표시 할 오류를 방지하려면 – Parallelis

+1

작은 파일에는 문제가 없지만 큰 파일에는 문제가 발생할 수 있습니다. 파일 크기가 300MB라면 어떻게 될까요? 나는'copy'를 사용하는 것이 더 나은 접근 방법이라고 생각합니다. –

+0

내 M.O. 오류를 해결하는 것입니다. 긴 파일을 숨기면 장기적으로 상황이 악화 될 수 있으므로 일반적으로 해당 폴더에 쓰기 권한이 있는지 확인합니다. –

0

그냥 루프에서 코드를 실행합니다 : 저장하기 전에, 그것은 단지 (cp file.html file1.html의 리눅스 bash는 명령에 해당) 복사본을 만드는 기본 OS를 알려줍니다

$filename = 'file.html' 
for($i=1; $i<=10; $i++) { 
    $copyname = "file$i.html"; 
    copy($filename, $copyname); 
} 

언제든지 오류 검사 및 처리를 추가하십시오.