ios - Storing multiple files on database PHP
Hello I am uploading multiple images from my ios swift app.
But my problem is I cannot manage to store them in my DB, and I'm pretty sure It is to do with my PHP code... It works fine for 1 image, but cannot get it to work for 2 images...I have tried using loops, but nothing worked, probably my fault for implementing them incorectly.
Here is my php code, if anyone can help it's much appreciated!!
// STEP 2.3 Move uploaded file
$folder = $folder . "/" . basename($_FILES["file"]["name"]);
if (move_uploaded_file($_FILES["file"]["tmp_name"], $folder)) {
$returnArray["message"] = "Post has been made with picture";
$blogImage1 = "http://**********/Files/Posts/ImgPosts/" .$id . "/" . $Title . "-". $uuid . "/" . basename($_FILES["file"]["name"]); //
} else {
//$returnArray["message"] = "Post has been made without picture";
$Image1 = "";
}
Swift Code
func createBodyWithParameters(parameters: NSMutableDictionary?,boundary: String) -> NSData {
let body = NSMutableData()
if parameters != nil {
for (key, value) in parameters! {
if(value is String || value is NSString){
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
else if(value is [UIImage]){
var i = 0;
for image in value as! [UIImage]{
let filename = "image\(i).jpg"
let data = UIImageJPEGRepresentation(image,1);
let mimetype = "image/jpg"
//"\(key)\(i)"
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\(i)\"; filename=\"\(filename)\"\r\n")
body.appendString("Content-Type: \(mimetype)\r\n\r\n")
body.append(data!)
body.appendString("\r\n")
i += 1;
}
}
}
}
body.appendString("--\(boundary)--\r\n")
// NSLog("data %@",NSString(data: body, encoding: NSUTF8StringEncoding)!);
return body
}
If anyone needs to see more code, please let me know.
Answer
Solution:
I fixed my issue by changing the current code in my PHP file for adding the image to this, to add multiple images from an UIImage array in my IOS app:
Hope this helps other people too, for any further detail just comment below!