简体   繁体   中英

How do we upload a file to mysql database using php

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.

Is there a way to upload a file just using PHP in to the MySQL database.

Regards, Mithun

"No I have files generated dynamically from my application ,which I need to upload automatically"

You will have to simulate a POST Request - try using sockets like the following example does:

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

How are you creating the files? I'm pretty sure that using fopen('yourfilename.txt', 'w') will create a file if there isn't one. Then you could just insert the file name into your mysql database.

maybe something as easy as;

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

then insert the 'data' variable into the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM