Al Martin Al Martin - 5 months ago 85
PHP Question

Swift 2, uploading base64 images to PHP

I'm having a huge issue trying to upload my images to php.

my script works fine but when I go to view the image online it shows a broken image and if I download it and try to open in photoshop it says the image is corrupt.

Swift file upload script

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 script

$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"];
}


part of the base64 before uploading.


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


part of the base64 after uploading.


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


Can anyone see any problems?

Answer

It seems that this code for replacing unwanted characters in the base64 doesn't work for php.

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

I took it out and altered my php code to replace the unwanted characters.

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"];
}

This fixed it for me.

Comments