C ++ Qt sends a POST file stream to the PHP interface,
Qt calls the PHP-written interface, transmits the image file to it, and stores the file on the server.
Binary files cannot be directly transmitted. Qt uses Base64 for encoding, and PHP decoding is saved as files.
Note: After receiving the data, PHP will replace the plus sign (+) in the POST data with a space, resulting in incomplete received data and failure to restore the data normally, in this example, all spaces are replaced with the + sign before PHP decoding.
Qt file:
// Qt file QFile file ("1.jpg"); if (! File. open (QIODevice: ReadOnly) {qDebug () <"file open failed. "; return;} QByteArray data = file. readAll (); file. close (); QNetworkRequest req (QUrl ("http: // localhost: 88/index. php "); networkMgr-> post (req," fileData = "+ data. toBase64 () + "");
PHP file:
<? Php $ recContent = $ _ POST ['filedata']; $ data = base64_decode (str_replace ("", "+", ($ recContent); file_put_contents ("1.jpg ", $ data);?>