Post image from linux (using c without curl) to php server - php

I am using v4l2 library on linux, take a picture and want to send it to a php server via c program.
I want to using a socket to do it. But i don't know how to pass the image to request .
This is my sample code:
int portno = 80;
struct sockaddr_in serv_addr;
int sockfd, bytes, sent, received, total;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
char message[1024],response[4096];
if (sockfd < 0){
printf("ERROR opening socket");
}
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
if(inet_pton(AF_INET, CONST_DOMAIN, &serv_addr.sin_addr)<=0){
printf("\n inet_pton error occured\n");
return 1;
}
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) {
printf("ERROR connecting");
}
char content[1024];
char *contentTemp="image_name=%s";
sprintf(content,contentTemp,imageName);
char *headerTemp="POST %supload.php HTTP/1.0\r\nHost: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-length: %d\r\n\r\n%s";
sprintf(message,headerTemp,SERVICE_PATH,SERVICE_HOST,strlen(content),content);
write(sockfd,message,strlen(message));
Can i using this way to post an image to server (include its name) ?
Any suggest for me ?
Thanks
PS: sorry about my english skill.

You are including only file name. You have to include the whole image file contents into post data stream. Forms submitting binary data with POST request should use multipart/form-data content type. You can't use application/x-www-form-urlencoded type.
From HTML 4.01 specification:
The content type "application/x-www-form-urlencoded" is inefficient
for sending large quantities of binary data or text containing
non-ASCII characters. The content type "multipart/form-data" should be
used for submitting forms that contain files, non-ASCII data, and
binary data.
You could adjust your code like this:
char *filename="file.jpg"; // this example uses jpeg
// optionally load file from filesystem
// though I think you have it in a buffer, don't you?
FILE *file = fopen(filename, "rb");
char binary[1024]; // adjust buffer size to your needs
size_t filesize = fread(binary, 1, sizeof(binary), file);
// check for error here to make sure read succeeded
fclose(file);
// multipart/form-data POST header
const char *headerTemp = "POST %supload.php HTTP/1.0\r\n"
"Host: %s\r\n"
"Content-Type: multipart/form-data; boundary=BoUnDaRy\r\n"
"Content-Length: %lu\r\n"
"\r\n";
// first and only part beginning
const char *bodyTemp =
"--BoUnDaRy\r\n"
"Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n"
"Content-Type: image/jpeg\r\n"
"Content-Transfer-Encoding: binary\r\n"
"\r\n";
// and ending
const char body2[] = "\r\n"
"--BoUnDaRy--\r\n";
char body1[1024]; // adjust buffer size to your needs
// calculate body size, will be included in Content-Length header
size_t body_size = strlen(body1) + strlen(body2) + filesize;
snprintf(header, 1024, headerTemp, SERVICE_PATH, SERVICE_HOST, body_size);
snprintf(body1, 1024, bodyTemp, filename);
// you should add checking for each write return value
write(sockfd, header, strlen(header));
write(sockfd, body1, strlen(body1));
write(sockfd, binary, filesize);
write(sockfd, body2, strlen(body2));
After sending data you should read server response, for example:
while (1) {
ssize_t result = recv(sockfd, response, sizeof(response), 0);
if (result == 0) {
break;
} else if (result < 0) {
perror("reading socket failed");
break;
}
printf("%s\n", response);
}
close(sockfd);
If you just close socket without waiting for the response server may complain and return error. You should also check if the response confirms valid request.

Related

Download files from the download server with the help of php and mvc

I have a download server for files.
And I want my users to get the files from the download server.
My download server is Linux.
I want when the user clicks on the download button.
Get the file directly from the download server.
I do not want to use the stream to download ...
I want to connect to the download server via the link and then download it using PHP
My site is with mvc
Thank you, step by step to help me
thank you
Stream stream = null;
//This controls how many bytes to read at a time and send to the client
int bytesToRead = 10000;
// Buffer to read bytes in chunk size specified above
byte[] buffer = new Byte[bytesToRead];
// The number of bytes read
try
{
//Create a WebRequest to get the file
HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(Global.UrlVideoPrice + IdCourse + "//" + IdTopic+".rar");
//Create a response for this request
HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();
if (fileReq.ContentLength > 0)
fileResp.ContentLength = fileReq.ContentLength;
//Get the Stream returned from the response
stream = fileResp.GetResponseStream();
// prepare the response to the client. resp is the client Response
var resp = System.Web.HttpContext.Current.Response;
//Indicate the type of data being sent
resp.ContentType = "application/octet-stream";
//Name the file
resp.AddHeader("Content-Disposition", "attachment; filename=\"" + Topic.fldName + ".rar\"");
resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());
int length;
do
{
// Verify that the client is connected.
if (resp.IsClientConnected)
{
// Read data into the buffer.
length = stream.Read(buffer, 0, bytesToRead);
// and write it out to the response's output stream
resp.OutputStream.Write(buffer, 0, length);
// Flush the data
resp.Flush();
//Clear the buffer
buffer = new Byte[bytesToRead];
}
else
{
// cancel the download if client has disconnected
length = -1;
}
} while (length > 0); //Repeat until no data is read
}
finally
{
if (stream != null)
{
//Close the input stream
stream.Close();
}
}
This is my download code.
Now I want the user to get the files directly from the download server.
Do not use site traffic to download the file.
And use server download traffic

Send Raw Data from PHP Server API to Swift client API

My goal is to store and retrieve raw data from a PHP script and client Swift code using HTTP POST. I am using MySQL database storing MEDIUMBLOB data.
I successfully sent the data from Swift and stored it into database.
Here is my working code used to store the data (I removed the WHERE clause to simplify), I can see the stored raw data using PHPMyAdmin (e.g. [BLOB - 345.6 KiB] which is the same size as client code data size) :
<?php
//...
$file = $_FILES['file']['tmp_name'];
$file_data = file_get_contents($file);
$file_info = finfo_open(FILEINFO_MIME_TYPE);
$file_type = finfo_file($file_info, $file);
$file_size = filesize($file);
$worldMap1 = $file_data;
$query = $query = "INSERT INTO " . $this->table_name . " SET worldMap1=:worldMap1";
//...
?>
Client Swift code (request structure) :
let boundary = generateBoundaryString()
var request = URLRequest(url: requestedUrl)
request.httpMethod = POSTMETHOD
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.httpBody = createBodyWithParametersForData(
parameters: dataBody,
filePathKey: "file",
imageDataKey: data,
boundary: boundary
)
private func createBodyWithParametersForData(parameters: [String: Any]?, filePathKey: String?, imageDataKey: Data, boundary: String) -> Data {
var body = Data();
if parameters != nil {
for (key, value) in parameters! {
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString(string: "\(value)\r\n")
}
}
let filename = "file"
let mimetype = "application/octet-stream"
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
body.append(imageDataKey)
body.appendString(string: "\r\n")
body.appendString(string: "--\(boundary)--\r\n")
return body
}
Now I simply want to send the data back to client Swift code (non working code) :
<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// Fetch the record:
$query = "SELECT worldMap1 FROM " . $this->table_name . "";
// prepare query
$stmt = $db->prepare($query);
//...
// execute query
$stmt->execute();
if ($stmt->rowCount() == 1) {
// Fetch the record into a variable:
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$file_size = filesize($row["worldMap1"]);
// Send the content information:
header("Content-Type: application/octet-stream\n");
header("Content-Disposition: attachment; filename=\"file\"\n");
header("Content-Length: {$file_size}\n");
// Send the file:
echo $row["worldMap1"];
}
// Omit the closing PHP tag to avoid tainting the downloaded file
On client side, I either receive 0 byte data or timeout error codes.
I also tried to hardcode the Content-Length value e.g. 1000.
I do not see any clear documentation on how to send raw data from PHP code.
Here is the only useful information I found and used to store the data : http://www.peachpit.com/articles/article.aspx?p=1967015&seqNum=4
Generally, files are stored in a directory and not in a database.
It looks like the problem is most likely with your Content-Type: application/octet-stream\n header. I don't think it's enough to just tell the browser hey, I'm expecting an octet-stream.
In order for your browser to recognize this as an attachment properly I would change the header to indicate the actual type of file you are dealing with so it knows how to interpret the file.
Like so:
header('Content-Type: ' . $type);
Here are a list of accepted MIME-TYPES
You are also over-writing your your content type header from the "required" application/json to the octet-stream. I would probably not send out the aplication/json header. Even though I believe it would be over-written by the latter.
Make sure you send your headers out before ANYTHING is outputted by the script. Even an unknown white space can cause you problems.
I also do not see the need for the \n in you header.
I would also consider using single quotes instead of double quotes as your likely to make mistakes escaping your double quotes for your file name.
Hope that helps.
Again, if it were me I would not do this. I would upload all the files to a directory not a db.

How do I read a string sent from PHP through a socket to a Qt server application?

I am having a really hard time reading character input that is sent through a socket connection to a Qt server application. The data is sent from PHP.
I understand the principles of reading streamdata because I already asked this on stack. I also got it working using a server and client written both in Qt.
The method I use is to append the bytesize of the data i want to send before the actual data. Then when the data comes in, I first read the length parth so that I know exactly how much bytes I have to read in order to have correctly formed data.
it looks like this:
send function:
void Client::sendNewMessage(){
qDebug() << "sendNewMessage()";
QString string(messageLineEdit->text());
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << quint16(0);
out << string;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket->write(block);
}
receive function:
QDataStream in(tcpServerConnection);
in.setVersion(QDataStream::Qt_4_0);
qDebug() << "bytes available = " << tcpServerConnection->bytesAvailable();
if (blockSize == 0) {
int size = (int) sizeof(quint16);
qDebug() << "size = " << size;
if (tcpServerConnection->bytesAvailable() < (int)sizeof(quint16)){
qDebug() << "less bytes than size...";
return;
}
qDebug() << "bytes available=" << tcpServerConnection->bytesAvailable();
in >> blockSize;
}
if (tcpServerConnection->bytesAvailable() < blockSize){
qDebug() << "less bytes available than blocksize, bytes="
<< tcpServerConnection->bytesAvailable();
return;
}
QString data;
in >> data;
qDebug() << "data = " << data;
Okay, this all works so I tried doing it with PHP but it failed
this is one of my attempts:
<?php
$addr = gethostbyname("127.0.0.1");
$client = stream_socket_client("tcp://$addr:*****", $errno, $errorMessage);
if ($client === false) {
throw new UnexpectedValueException("Failed to connect: $errorMessage");
}
$data = 'a';
$datatopost = serialize($data);
fwrite($client, strlen($data));
fwrite($client, base64_encode($data));
echo stream_get_contents($client);
fclose($client);
In Qt I have tried various combinations of quint8, 16, 32, 64, sizeof(char), sizeof(int).
in PHP I have tried serializing the data, encoding it, and also sending it without all that stuff. But i can not get it to work. I must be very close though because the data is actually sent as there are bytes available but I have no idea how to encode/decode correctly for it to work.
After asking various question concerning this topic I do feel that my understanding has gone up a lot but an important piece of information on how to actually do things is still missing for me.
So my question: What is going wrong here and what steps need to be taken to be able to read data from PHP to Qt/C++?
Details are highly apreciated as I really like to know how things work from the inside out.
side-note after sending data from the PHP script, the server sends data back aswel and that works. So the connection is made succesfuly
UPDATE
this is the working php script that actually also receives a reply back:
<?php
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
perror("Could not create socket");
}
echo "Socket created n";
//Connect socket to remote server
if(!socket_connect($sock , '127.0.0.1' , *****))
{
perror("Could not connect");
}
echo "Connection established n";
$message = "aa";
//Send the message to the server
if( ! socket_send ( $sock , $message , strlen($message) , 0))
{
perror("Could not send data");
}
echo "Message send successfully n";
//Now receive reply from server
if(socket_recv ( $sock , $buf , 500 , MSG_WAITALL ) === FALSE)
{
perror("Could not receive data");
}
echo $buf;
///Function to print socket error message
function perror($msg)
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("$msg: [$errorcode] $errormsg n");
}
The script reply when executed from browser url:
Socket created nConnection established nMessage send successfully n hello
It's not that surprising the PHP code does not integrate. As mentioned you have to be aware that QDataStream implements a custom serialization. And as also mentioned you probably want to use (read|write)RawData, or (read|write)Bytes, if your reading something not previously serialized with QDataStream in general. However, the general idea of the way your trying to write string data from PHP should be compatible with the way Qt encodes strings (length then a series of characters. That is what the manual says anyway..). But there some issues.
QString is 2Byte Unicode.
PHP Strings are byte arrays of an arbitrary kind of ASCII compatible data - PHP String details.
There is a few things wrong with this bit:
fwrite($client, strlen($data));
fwrite($client, base64_encode($data));
strlen() returns the number of bytes in the underlying storage (which is the actual byte length for a ASCII string). base64_encode() changes the number of bytes in the string. And your assuming fwrite() is writing a four byte integer. Its type casting and writing a string.
We are still guessing at how
QString data;
in >> data;
really works.
General advice is, you've got to carefully define external binary APIs.
Do you need data serialization for this task at all? Your PHP client and Qt server are probably using different formats for it.
Try to send and receive raw data.
Here is a simple QTcpServer exmaple:
class DataReceiver : public QObject
{
Q_OBJECT
public:
explicit DataReceiver(QObject *parent = 0);
public slots:
void start(quint16 port = 9090);
private slots:
void newTcpConnection();
private:
QTcpServer server;
};
DataReceiver::DataReceiver(QObject *parent) :
QObject(parent)
{
connect(&server, SIGNAL(newConnection()), this, SLOT(newTcpConnection()));
}
void DataReceiver::start(quint16 port)
{
bool isOk = server.listen(QHostAddress::Any, port);
if (isOk && server.isListening())
{
qDebug() << "QTcpServer started on port" << port;
}
else
{
qCritical() << "Failed to start QTcpServer";
}
}
void DataReceiver::newTcpConnection()
{
qDebug() << "New incoming connection";
QTcpSocket *socket = server.nextPendingConnection();
QByteArray data;
while (true)
{
QByteArray tmp = socket->readAll();
data += tmp;
if (tmp.isEmpty() && !socket->waitForReadyRead())
{
break;
}
}
socket->deleteLater();
qDebug("Data received: %s (len = %d)", data.constData(), data.length());
}
Launching server:
#include <QCoreApplication>
#include "data_receiver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
DataReceiver d;
d.start();
return a.exec();
}
You can use a PHP client to send data to it:
<?php
$addr = gethostbyname("127.0.0.1");
$port = 9090;
$data = 'hello from php';
$client = stream_socket_client("tcp://$addr:$port", $errno, $errorMessage);
if ($client === false) {
throw new UnexpectedValueException("Failed to connect: $errorMessage");
}
fwrite($client, $data);
fclose($client);
Or you can use the nc utility:
echo -n "hello from nc" | nc 127.0.0.1 9090
Here is server output for both cases:
QTcpServer started on port 9090
New incoming connection
Data received: hello from php (len = 14)
New incoming connection
Data received: hello from nc (len = 13)

Read html file and replace string in c

I need function to read html file (email template) and replace some string in that file. I already have some function for that but i think that function make html template look ugly when i send email. here is my function
int send_formated_mail(char *to, struct mail_struct *mail, char *string)
{
int retval;
/* Open pipe to sendmail */
sendmail_pipe=popen(sendmail_path, "w");
if(sendmail_pipe == NULL)
{
fprintf(stderr, "No Pipe to \"sendmail\"\n");
return 1;
}
FILE *fd = fopen(mail->mail_layout_path, "r");
if(fd != NULL)
{
fprintf(sendmail_pipe, "To: %s\r\n"
"From: %s\r\n"
"Reply-to: %s\r\n"
"Subject : %s\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"Mime-Version: 1.0\r\n\n",
to, from, reply_to, mail->subject);
char buffer[255];
memset(buffer,'\0',sizeof(buffer));
while(!feof(fd))
{
fread(buffer, sizeof(buffer), 1, fd);
fprintf(sendmail_pipe, "%s", replace_str(buffer, LINK, string));
memset(buffer,'\0',sizeof(buffer));
}
fflush(sendmail_pipe);
retval = pclose(sendmail_pipe);
fclose(fd);
return retval;
}
return 1;
}
char *replace_str(char *str, char *orig, char *rep)
{
static char buffer[4096];
char *p;
if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
return str;
strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
buffer[p-str] = '\0';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
return buffer;
}
Template is utf-8 charset and i need to send utf-8. Right now string is replaced but there are some errors with template, like charset is different. I know how to do this is php and in php it works well but i dont know in c...
any help is welcome!
p.s can i execute php file from this c function? so i dont need this replace and charset would be ok.

Visual Basic - Upload file to PHP webpage

I'm trying to upload an image file to a PHP file on a web server.
On VB.NET ->
My.Computer.Network.UploadFile(tempImageLocation, "website.com/upload.php")
tempImageLocation is a location on the harddrive where the image is located. The image is located on the harddrive where I specify it.
On PHP ->
$image = $_FILES['uploads']['name'];
I don't understand, because it is loading the page - but PHP can't find the file under 'uploads'
Google brought me here while I was searching for the same question. Thanks people it gave me the idea, and with a little knowledge of PHP, I've achieved it. I know its an old question but still I'm going to share my code so it could help people in future..
VB:
My.Computer.Network.UploadFile("e:\file1.jpg", "http://www.mysite.com/upl/upl.php")
PHP:
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
and don't forget to give the upload folder the appropriate permissions.
I know, is old.. but here is solution work to me:
Private Sub HttpUploadFile(
ByVal uri As String,
ByVal filePath As String,
ByVal fileParameterName As String,
ByVal contentType As String)
Dim myFile As New FileInfo(filePath)
Dim sizeInBytes As Long = myFile.Length
Dim boundary As String = "---------------------------" & DateTime.Now.Ticks.ToString("x")
Dim newLine As String = System.Environment.NewLine
Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes(newLine & "--" & boundary & newLine)
Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri)
request.ContentType = "multipart/form-data; boundary=" & boundary
request.Method = "POST"
request.KeepAlive = True
'request.Credentials = Net.CredentialCache.DefaultCredentials
Using requestStream As IO.Stream = request.GetRequestStream()
Dim formDataTemplate As String = "Content-Disposition: form-data; name=""{0}""{1}{1}{2}"
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""{2}Content-Type: {3};"
Dim header As String = String.Format(headerTemplate, fileParameterName, filePath, newLine, contentType)
header = header & vbNewLine & "Content-Length: " & sizeInBytes.ToString & vbNewLine
header = header & "Expect: 100-continue" & vbNewLine & vbNewLine
'MsgBox(header)
Debug.Print(header)
Dim headerBytes As Byte() = Encoding.UTF8.GetBytes(header)
requestStream.Write(headerBytes, 0, header.Length)
Using fileStream As New IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read)
Dim buffer(4096) As Byte
Dim bytesRead As Int32 = fileStream.Read(buffer, 0, buffer.Length)
Do While (bytesRead > 0)
requestStream.Write(buffer, 0, bytesRead)
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
Loop
End Using
Dim trailer As Byte() = Encoding.ASCII.GetBytes(newLine & "--" + boundary + "--" & newLine)
requestStream.Write(trailer, 0, trailer.Length)
requestStream.Close()
End Using
Dim response As Net.WebResponse = Nothing
Try
response = request.GetResponse()
Using responseStream As IO.Stream = response.GetResponseStream()
Using responseReader As New IO.StreamReader(responseStream)
Dim responseText = responseReader.ReadToEnd()
Debug.Print(responseText)
End Using
End Using
Catch exception As Net.WebException
response = exception.Response
If (response IsNot Nothing) Then
Using reader As New IO.StreamReader(response.GetResponseStream())
Dim responseText = reader.ReadToEnd()
Diagnostics.Debug.Write(responseText)
End Using
response.Close()
End If
Finally
request = Nothing
End Try
End Sub
Using:
HttpUploadFile("https://www.yousite.com/ws/upload.php?option1=sss&options2=12121", FULL_FILE_NAME_PATH_IN_YOUR_PC, "files", "multipart/form-data")
I copy somen code in a website i dont remember.
I only put this 2 lines of code to work:
header = header & vbNewLine & "Content-Length: " & sizeInBytes.ToString & vbNewLine
header = header & vbNewLine & "Expect: 100-continue" & vbNewLine
hope help.
Here is the complete example for uploading file using Visual Basic and on Server Side PHP (Rest API) GitHub Link
Here is quick and dirty tutorial for you: PHP File Upload
'uploads' is just name attribute value of element of a form:
<input type="file" name="uploads" />
or in other words, this is POST variable name that is accessed over $_FILES global.
If you don't set the field name, you can save the uploaded file with this
$file = array_shift($_FILES);
move_uploaded_file($file['tmp_name'], '/path/to/new/location/'.$file['name']);
Take a look at some of these other answers. PHP requires files uploaded with the POST method to use certain headers which are normally set by the browser when uploading from a web form but which can be set in VB with the HttpWebRequest Class.
As for the PHP side, you aren't going to be able to locate the file immediately after uploading with $image = $_FILES['uploads']['name'];. PHP stores uploads with a temporary filename accessible with the $_FILES['uploads']['tmp_name'] variable, and using move_uploaded_file() is the standard way of shifting uploads from temporary storage into a permanent uploads directory. The PHP manual provides a good overview of that.
Here's my sample server php file:
<?php
// write to a log file so you know it's working
$msg = $_POST['w'];
$logfile= 'data.txt';
$fp = fopen($logfile, "a");
fwrite($fp, $msg);
fclose($fp);
$file = array_shift($_FILES);
move_uploaded_file($file['tmp_name'], '/MAMP/htdocs/test/'.$file['name']);
?>
Here's the code to call #Rodrigo's code:
HttpUploadFile("http://localhost/test/test.php?w=hello&options2=12121", "C:\temp\bahamas.mp3", "files", "multipart/form-data")

Categories