在数据库PHP上存储多个文件

时间:2016-10-24 17:15:20

标签: php ios swift

您好我正在从ios swift app上传多张图片。

但我的问题是我无法将它们存储在我的数据库中,而且我非常确定这与我的PHP代码有关...它适用于1个图像,但无法使其工作2张图片......我尝试过使用循环,但没有任何效果,可能是我无法实现它们的错误。

这是我的PHP代码,如果有人可以帮助它,非常感谢!!

// 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
}

如果有人需要查看更多代码,请告诉我们。

1 个答案:

答案 0 :(得分:0)

我通过更改PHP文件中的当前代码来修复我的问题,以便将图像添加到此,以便在我的IOS应用程序中添加来自UIImage数组的多个图像:

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; //

希望这也有助于其他人,任何进一步的细节只需在下面评论!