Post multiple files with QNetworkRequest in Qt 6.4.2 C++ and php
-
wrote on 2 Oct 2024, 01:29 last edited by Kaguro 10 Feb 2024, 01:31
Hi Guys!
I have a problem with uplode multiple files to my server. When I try to upload just one file its working, but in this way not, where is my mistake?Qt upload part:
void FileUploader::uploadFile() { QList<QFile *> *file = new QList<QFile *>; QStringList list ={"F:/Download/Heti hetes/2 evad/HH 2x42 2001-06-16e1.avi","F:/Download/Heti hetes/1 evad/bestofhh0.avi","F:/Download/Heti hetes/1 evad/h7_1999-11-27 (1).avi"}; QStringList listname ={"video_name1","video_name2","video_name3"}; for (int i = 0; i < list.size(); i++) { QFile *tmp= new QFile(list.at(i)); file->append(tmp); } QUrl url("http://xyz/upfiles.php"); QNetworkRequest request(url); QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); for (int i = 0; i < list.size(); i++) { QHttpPart filePart; filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\""+ listname.at(i) +".mp4\"")); file->at(i)->open(QIODevice::ReadOnly); filePart.setBodyDevice(file->at(i)); file->at(i)->setParent(multiPart); multiPart->append(filePart); } QHttpPart typePart; typePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"type\"")); typePart.setBody("video"); multiPart->append(typePart); reply = networkManager->post(request, multiPart); multiPart->setParent(reply); connect(reply, &QNetworkReply::uploadProgress, this, &FileUploader::uploadProgress); connect(reply, &QNetworkReply::finished, this, &FileUploader::onUploadFinished); connect(reply, &QNetworkReply::errorOccurred, this, &FileUploader::onError); }
php part:
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { foreach ($_FILES as $fileKey => $fileValue) { if(isset($_POST['type']) && $_POST['type'] == "video") { $target_dir = "videos/"; } else { $target_dir = "thumbnail/"; } $target_file = $target_dir . basename($fileValue["name"]); if (move_uploaded_file($fileValue["tmp_name"], $target_file)) { echo htmlspecialchars(basename($fileValue["name"])) . "Upload Success"; } else { echo htmlspecialchars(basename($fileValue["name"])) . "Upload Fail"; } } } ?>
Thanks for your help!
-
What have you done to debug it?
Have you sniffed the network traffic to see what went on the wire (both directions)?
When it worked with one file was that one file in a multi-part upload, or some other mechanism?
Is the first file about 6% of the total? What would that suggest?
Does it work with many small files?
What are the file upload limits at the PHP end (upload_max_filesize and max_file_uploads)?
Is there enough temporary space on the server side (upload_tmp_dir)?
What is recorded in logs on the server end?wrote on 2 Oct 2024, 14:17 last edited by@ChrisW67 Now its working perfectly! I thought that the file size only had to be set in php.ini, but no, it had to be set in apache.conf as well:
LimitRequestBody 2147483648 (for example this is 2gb limit).
So many thank you for your help and questions!:) -
wrote on 2 Oct 2024, 03:13 last edited by
As you do not describe what "not working" means it will be difficult to get specific help. What does and does not work? What have you done to debug it? Have you sniffed the network traffic to see what went on the wire?
At a glance you are not setting Content-Type on the binary parts.
-
wrote on 2 Oct 2024, 05:10 last edited by
When I upload only 1 file in a similar way (I use post in the cycle), then it works what it means It uploads the files to the right place. (All files) If I want to send it to a post as I wrote in the question (All files in one post), it goes very slowly to 6%, and says "Upload error: QNetworkReply::UnknownNetworkError" in debug and the upload stops. But it didn't upload any of the files.
-
wrote on 2 Oct 2024, 07:18 last edited by ChrisW67 10 Feb 2024, 07:19
What have you done to debug it?
Have you sniffed the network traffic to see what went on the wire (both directions)?
When it worked with one file was that one file in a multi-part upload, or some other mechanism?
Is the first file about 6% of the total? What would that suggest?
Does it work with many small files?
What are the file upload limits at the PHP end (upload_max_filesize and max_file_uploads)?
Is there enough temporary space on the server side (upload_tmp_dir)?
What is recorded in logs on the server end? -
wrote on 2 Oct 2024, 12:48 last edited by Kaguro 10 Feb 2024, 13:45
Okay, thanks for your questions it makes clear what the main problem is. I tried upload 50 mini files in this way, and it works. When I try to upload one 4,7 Gb file I got the same QNetworkReply::UnknownNetworkError and nothing happened (upload nothing). I thought the time is the problem, but no the size of ONE file! If its more then 1Gb the upload dies. I set everything on my server in php.ini (max_input_time = 600, memory_limit = 4G, post_max_size = 8G, max_file_uploads = 70) but the maximum 1gb file size never change.. no more no less than 1gb and the upload dies. Tmp dir size is okay( Size 98G Used 39G Avail 55G Use 41% Mounted on / ). Now I can't see the network traffic and this kind of logs, i am so beginner in ubuntu servers.
So I think the problem is the size not the 30 sec timeout, but I have no idea where I have to change it. -
What have you done to debug it?
Have you sniffed the network traffic to see what went on the wire (both directions)?
When it worked with one file was that one file in a multi-part upload, or some other mechanism?
Is the first file about 6% of the total? What would that suggest?
Does it work with many small files?
What are the file upload limits at the PHP end (upload_max_filesize and max_file_uploads)?
Is there enough temporary space on the server side (upload_tmp_dir)?
What is recorded in logs on the server end?wrote on 2 Oct 2024, 14:17 last edited by@ChrisW67 Now its working perfectly! I thought that the file size only had to be set in php.ini, but no, it had to be set in apache.conf as well:
LimitRequestBody 2147483648 (for example this is 2gb limit).
So many thank you for your help and questions!:) -
-
wrote on 2 Oct 2024, 22:00 last edited by
I love it when a plan comes together
1/7