Nasıl php kullanarak mysql veritabanı için bir dosya yüklerim

4 Cevap

I need to upload a file of ethereal capture into the MySQL database. The problem is , it is not done manually through a html form . Hence I can't use enctype="multipart/form-data" and $_FILES to upload a file.

Sadece MySQL veritabanı için PHP kullanarak bir dosya yüklemek için bir yolu var mı.

Regards, Mithun

4 Cevap

"Hayır ben otomatik olarak yüklemek gerekiyor benim uygulamadan dinamik olarak oluşturulan dosyaları var"

Bir POST isteği taklit etmek zorunda olacak - Aşağıdaki örnek yok gibi yuvalarını kullanarak deneyin:

<?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);
}
}

Nasıl dosyaları oluştururken? Ben bir tane yoksa fopen ('yourfilename.txt', 'w') kullanarak bir dosya yaratacaktır eminim. Sonra sadece MySQL veritabanı içine dosya adı eklemek olabilir.

belki kadar kolay bir şey;

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

Daha sonra veritabanına 'veri' değişken eklemek.