2009-09-30 7 views
0

미묘한 캡처 파일을 MySQL 데이터베이스에 업로드해야합니다. 문제는 html 양식을 통해 수동으로 수행되지 않는다는 것입니다. 따라서 enctype="multipart/form-data"$_FILES을 사용하여 파일을 업로드 할 수 없습니다.우리는 어떻게 PHP를 사용하여 MySQL 데이터베이스에 파일을 업로드합니까

MySQL 데이터베이스에 PHP를 사용하여 파일을 업로드하는 방법이 있습니까?

감사합니다, Mithun

+0

html 양식을 사용하지 않았습니까? 그렇다면 업로드 방법은 무엇입니까? – Graviton

+0

양식 업로드가 중단되는 이유는 무엇입니까? – nduplessis

+0

아니요 자동으로 업로드해야하는 내 응용 프로그램에서 동적으로 생성 된 파일이 있습니다. 따라서 나는 HTML 양식을 사용할 수 없습니다. – Vidya

답변

0

당신은 POST 요청을 시뮬레이션 할 것이다

"아니, 난 내가 자동으로 업로드해야 내 응용 프로그램에서 동적으로 생성 된 파일이"- 사용 시도를 다음 예제와 같은 소켓을 사용하십시오.

<?php  
$filename = 'C:/tmp/myphoto.jpg'; 
$handler = 'http://www.example.com/upload.php'; 
$field = 'image'; 
$res = send_file($filename, $handler, $field); 


if ($res) { 
echo 'done.'; 
} else { 
echo 'something went wrong.'; 
} 
?> 



function send_file($fname, $handler, $field) 
{ 
/* check if file exists */ 
if (!file_exists($fname)) { 
    echo 'file not found.'; 
    return false; 
} 

/* get file's extension */ 
preg_match("/\.([^\.]+)$/", $fname, $matches); 
$ext = $matches[1]; 

/* guess mimetype from file's extension 
    please add some more mimetypes here */ 
switch(strtolower($ext)) { 
    case "doc": 
     $mime = "application/msword"; 
     break; 
    case "jpeg": 
    case "jpg":  
    case "jpe": 
     $mime = "image/jpeg"; 
     break; 
    case "gif": 
     $mime = "image/gif"; 
     break; 
    case "pdf": 
     $mime = "application/pdf"; 
     break; 
    case "png": 
     $mime = "image/png"; 
     break; 
    case "txt": 
    default: 
     $mime = "text/plain"; 
     break; 
}  

/* get hostname and path of remote script */ 
$host = parse_url($handler, PHP_URL_HOST); 
$path = parse_url($handler, PHP_URL_PATH); 

/* setup request header and body */ 
$boundary = "---------" . str_replace(".", "", microtime()); 
$reqbody = "--$boundary\r\n" 
      . "Content-Disposition: form-data; name=\"$field\"; filename=\"$fname\"\r\n" 
      . "Content-Type: $mime\r\n\r\n" 
      . file_get_contents($fname) . "\r\n" 
      . "--$boundary--\r\n"; 
$bodylen = strlen($reqbody); 
$reqhead = "POST $path HTTP/1.1\r\n" 
      . "Host: localhost\r\n" 
      . "Content-Type: multipart/form-data; boundary=$boundary\r\n" 
      . "Content-Length: $bodylen\r\n" 
      . "Connection: Close\r\n\r\n"; 

/* open socket connection to remote host on port 80 */ 
$fp = fsockopen($host, 80, $errno, $errmsg, 30); 

/* check the connection */ 
if (!$fp) { 
    print "Cannot connect to $host!\n"; 
    return false; 
} 

/* send request */ 
fwrite($fp, $reqhead); 
fwrite($fp, $reqbody); 

/* read response */ 
$res = ""; 
while(!feof($fp)) { 
    $res .= fgets($fp, 4096); 
}  
fclose($fp); 

/* separate header and body */ 
$neck = strpos($res, "\r\n\r\n"); 
$head = substr($res, 0, $neck); 
$body = substr($res, $neck+4); 

/* check HTTP status */ 
$lines = explode("\r\n", $head); 
preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $lines[0], $m); 
$status = $m[2]; 

if ($status == 200) { 
    return(true); 
} else { 
    return(false); 
} 
} 
0

파일을 어떻게 작성하고 있습니까? ? 나는 fopen ('yourfilename.txt', 'w')을 사용하면 파일이 하나도 없다면 파일을 만들 것이라고 확신한다. 그런 다음 mysql 데이터베이스에 파일 이름을 삽입하면됩니다.

0

어쩌면 뭔가 쉽습니다.

$data = file_get_contents("yourFile.dat"); 

그런 다음 '데이터'변수를 데이터베이스에 삽입하십시오.

관련 문제