Swift 2,将base64图像上传到PHP

时间:2015-09-05 08:04:52

标签: php swift base64 xcode7

我在尝试将图片上传到php时遇到了一个大问题。

我的脚本工作正常,但当我在线查看图像时,它显示图像已损坏,如果我下载并尝试在photoshop中打开,则表示图像已损坏。

Swift文件上传脚本

func percentEscapeString(string: String) -> String {
    return CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
        string,
        nil,
        ":/?@!$&'()*+,;=",
        CFStringBuiltInEncodings.UTF8.rawValue) as String;
}

func imagePost(params : NSMutableDictionary, image: UIImage, url: String, postCompleted: (succeeded: Bool, msg: AnyObject) -> ()){
    let request = NSMutableURLRequest(URL: NSURL(string: url)!)
    let session = NSURLSession.sharedSession()
    request.HTTPMethod = "POST"


    let imageData = UIImageJPEGRepresentation(image, 0.9)
    var base64String = self.percentEscapeString(imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))) // encode the image
    print(base64String)
    params["image"] = [ "content_type": "image/jpeg", "filename":"test.jpg", "file_data": base64String]
    do{
        request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions(rawValue: 0))
    }catch{
        print(error)
    }

    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        NSOperationQueue.mainQueue().addOperationWithBlock {
            var err: NSError?
            var json:NSDictionary?
            do{
                json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary
            }catch{
                print(error)
                err = error as NSError
            }

            // Did the JSONObjectWithData constructor return an error? If so, log the error to the console
            if(err != nil) {
                print("Response: \(response)")
                let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
                print("Body: \(strData!)")
                print(err!.localizedDescription)
                let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
                print("Error could not parse JSON: '\(jsonStr)'")
                postCompleted(succeeded: false, msg: "Error")
            }else {

                // The JSONObjectWithData constructor didn't return an error. But, we should still
                // check and make sure that json has a value using optional binding.
                if let parseJSON = json {
                    // Okay, the parsedJSON is here, let's get the value for 'success' out of it
                    if let success = parseJSON["success"] as? Bool {
                        //print("Success: \(success)")
                        postCompleted(succeeded: success, msg: parseJSON["message"]!)
                    }
                    return
                }
                else {
                    // Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
                    let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
                    print("Error could not parse JSON: \(jsonStr)")
                    postCompleted(succeeded: false, msg: "Unable to connect")
                }
            }
        }
    })

    task.resume()
}

PHP脚本

$json = file_get_contents('php://input');
$obj = json_decode($json);

if($obj->image->content_type == "image/jpeg"){
     $filename = $obj->id . time() . ".jpg";
     $target_file = "userImages/$filename";
     if(file_put_contents($target_file, $obj->image->file_data)){
        $return_data = ["success"=>true, "message"=>"The photo has been uploaded."];
    } else {
        $return_data = ["success"=>false, "message"=>"Sorry, there was an error uploading your photo."];
    }
}else{
    $return_data = ["success"=>false,"message"=>"Not a JPEG image"];
}
在上传之前

部分base64。

  

%2F9j%2F4AAQSkZJRgABAQAASABIAAD%2F4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAlqADAAQAAAABAAAAyAAAAAD%2F7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs%2BEJ%2B%2F8AAEQgAyACWAwEiAAIRAQMRAf

上传后的base64的一部分。

  

%2F9j%2F4AAQSkZJRgABAQAASABIAAD%2F4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAlqADAAQAAAABAAAAyAAAAAD%2F7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs%2BEJ%2B%2F8AAEQgAyACWAwEiAAIRAQMRAf

任何人都可以看到任何问题吗?

2 个答案:

答案 0 :(得分:3)

似乎这个用于替换base64中不需要的字符的代码对php不起作用。

func percentEscapeString(string: String) -> String {
    return CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
        string,
        nil,
        ":/?@!$&'()*+,;=",
        CFStringBuiltInEncodings.UTF8.rawValue) as String;
}

我拿出来并改变了我的PHP代码以替换不需要的字符。

if($obj->image->content_type == "image/jpeg"){
    $filename = $obj->id . time() . ".jpg";
    $target_file = "userImages/$filename";
    $data = str_replace(' ', '+', $obj->image->file_data);
    $data = base64_decode($data);
    if(file_put_contents($target_file, $data)){
        $return_data = ["success"=>true, "message"=>"The photo has been uploaded."];
    } else {
        $return_data = ["success"=>false, "message"=>"Sorry, there was an error uploading your photo."];
    }
}else{
    $return_data = ["success"=>false,"message"=>"Not a JPEG image"];
}

这为我解决了。

答案 1 :(得分:0)

| * | Swift2代码:将图像转换为字符串以便发送到Php服务器

{{1}}

| * | Php代码:将字符串数据转换回图像并保存在服务器中

{{1}}