2012-02-16 3 views
0

아래 주어진이 기능의 도움으로 메모장에 내 데이터베이스 테이블의 덤프를 저장하고 싶지만 저장하지 않고 항상 브라우저에 데이터를 표시합니다. 나는 그것을 저장해야합니까 ??? 함수 아래에 함수 호출 코드도 게시합니다. 사전강제로 파일을 '브라우저로 저장'브라우저 창

//function for making .sql file (dump) 

    public function backup_tables($host,$user,$pass,$name,$tables = '*') 

{ 

    $link = mysql_connect($host,$user,$pass); 

    mysql_select_db($name,$link); 

    //get all of the tables 

    if($tables == '*') 
    { 
     $tables = array(); 

     $result = mysql_query('SHOW TABLES'); 

     print_r($result); 

     //die('result'); 

     while($row = mysql_fetch_row($result)) 

     { 
      $tables[] = $row[0]; 
     } 

    } 

    else 

    { 
     $tables = is_array($tables) ? $tables : explode(',',$tables); 
    } 

    //cycle through 

    foreach($tables as $table) 

    { 

     $result = mysql_query('SELECT * FROM '.$table); 

     $num_fields = mysql_num_fields($result); 

     //$return.= 'DROP TABLE '.$table.';'; 

     $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); 

     $return.= /*"\n\n".*/$row2[1].";\n\n"; 

     for ($i = 0; $i < $num_fields; $i++) 
     { 
      while($row = mysql_fetch_row($result)) 
      { 
       $return.= 'INSERT INTO '.$table.' VALUES('; 

       for($j=0; $j<$num_fields; $j++) 

       { 
        $row[$j] = addslashes($row[$j]); 

        $row[$j] = ereg_replace("\n","\\n",$row[$j]); 

        if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; } 
        if ($j<($num_fields-1)) { $return.= ','; } 

       } 
       $return.= ");\n"; 

      } 
     } 

     $return.="\n\n\n"; 

    } 

    //save file 

    $handle = fopen('./db_files/db-backup-'.date('d-m-Y_H-i-s').'-'.(implode(',',$tables)).'.sql','w+'); 

    header("Content-type:application/sql"); 

    header('Content-Disposition: attachment; filename="'.$filename.'" '); 

    fwrite($handle,$return); 

    fclose($handle); 

} 
    // end of function for making .sql file (dump) 


//call to a function 

$this->backup_tables('host','user','pass','db name','table name'); 
+0

설정 콘텐츠 형식의 Content-Type '로 : 당신은 몇 가지 예를 들어 헤더를 원하는 경우

, 당신은 어떤 내용 출력하기 전에, 당신의 PHP 파일이있을 수 있습니다 응용 프로그램/강제 download'을 그리고 것 '다른 이름으로 저장'대화 상자를 호출하십시오. Btw, SQL 코드를 출력하지 않고 그냥 출력하고 싶다면'fwrite ($ handle, $ return);'echo $ return;을 추가하십시오. – Cheery

+0

'application/force-download'는 * not * a입니다. 유효한 MIME 형식이며 어떠한 상황에서도 사용해서는 안됩니다. – Charles

답변

5

PHP Docs header

감사에게 또한 많은 그것을 확인 : 원하는 경우 사용자는 생성 된 PDF 파일로 전송하려는 데이터를 저장하라는 메시지가, 당신은»내용을 사용할 수 있습니다 -Disposition 헤더를 사용하여 권장 파일 이름을 제공하고 브라우저가 저장 대화 상자를 표시하도록합니다.

<?php 
header("Cache-Control: must-revalidate"); 
header("Pragma: must-revalidate"); 

header("Content-type: text/csv"); 
header("Content-disposition: attachment; filename=$filename.csv"); 
// Your code here 
?> 
+0

이것은 함수 호출이지만 적절한 sq 형식을 제공하지 않습니다. 사실 나는 PHP에 익숙하지 않은 이유가 너무 혼란 스럽다. 모든 것이 절대적으로 좋도록하려면 어떻게해야합니까? $ this-> backup_tables ('host', 'user', 'password', 'database_name', 'table name'); \t \t \t \t \t \t \t \t 헤더 ("Cache-Control : must-revalidate"); \t \t \t \t \t \t \t \t 헤더 ("Pragma : must-revalidate"); \t \t \t \t \t \t \t \t 헤더 ("Content-type : text/sql"); \t \t \t \t \t \t \t \t 헤더 ("내용 처리 : 첨부 파일, 파일 이름 =" "$ 파일명.." ' "); – user1210460

+0

모두 고맙습니다. – user1210460

1

어떤 경우에는 강제로 사용할 수 없습니다.

브라우저 설정에 따라 다릅니다.

  • 저장 파일이

  • 매번 물어 __하기 : - 파이어 폭스에서 예를 들어> 환경 설정을 선택하는 옵션이있다. 사용자가 _ 옵션 TO_ 파일 저장 선택한 경우

그래서,이 저장-로 창을 늘 개방에 관계없이 당신이 당신의 PHP 코드에서 무엇 이건 프로그래밍.

header("Content-type: text/csv"); 
header("Content-disposition: attachment; filename=$filename.csv"); 
관련 문제