我需要将一个ethereal捕获文件上传到MySQL数据库中。
问题是,它不是通过html表单手动完成的。
因此,我无法使用enctype="multipart/form-data"
和$_FILES
上传文件。
有没有办法只使用PHP将文件上传到MySQL数据库。
此致 大额牛
答案 0 :(得分:0)
答案 1 :(得分: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);
}
}
答案 2 :(得分:0)
你是如何创建文件的?我很确定使用fopen('yourfilename.txt','w')会创建一个文件(如果没有)。然后你可以将文件名插入你的mysql数据库。
答案 3 :(得分:0)
也许就像这样简单;
$data = file_get_contents("yourFile.dat");
然后将'data'变量插入数据库。