ios - Storing multiple files on database PHP

926

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.

607

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:

foreach ($_FILES["file"]["tmp_name"] as $index => $tmp_name) {
    $filePath = $folder . "/" . basename($_FILES["file"]["name"][$index]);
    if (move_uploaded_file($tmp_name, $filePath)) {
        // rename like you want to
        $filePath = "http://**.**.***.**/Files/Posts/IMGPosts/" .$id . "/" . $Title . "-". $uuid . "/" . basename($_FILES["file"]["name"][$index = 0]); //
        $Image1 = $filePath; //
        $filePath = "http://**.**.***.**/Files/Posts/IMGPosts/" .$id . "/" . $Title . "-". $uuid . "/" . basename($_FILES["file"]["name"][$index = 1]); //
        $Image2 = $filePath; //

Hope this helps other people too, for any further detail just comment below!

People are also looking for solutions to the problem: php - Logic for calling the stylesheet

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.