I'm writing a webservices for an IphoneAPP using cakephp as the backend for the Iphone App. Everthing works fine with updating / deleting /editing data from the database, in other words iphone app is running perfectly with the database interactivity.
Now after some modifications in the App , I had to save image when the post from the iphone. The URL that is generated when we post is something like this http://somedomainname.com/customer/add/?name=abc&number=165adf&image=89504e470d0a1a0a0000000d494844520000004e0000005f08060000004620f1230000001974455874536f6674
Above URL contains image value in binary , which is apng image. I would like to decode the binary code and upload it in the folder. I have used the code to upload the file
$folder= "img/Invoicecustomer";
$formdata = $data;
$itemId = null;
// setup dir names absolute and relative
$folder_url = WWW_ROOT.$folder;
$rel_url = $folder;
// create the folder if it does not exist
if(!is_dir($folder_url)) {
mkdir($folder_url, 0777, true);
chmod($folder_url, 0777);
}
// if itemId is set create an item folder
if($itemId) {
// set new absolute folder
$folder_url = WWW_ROOT.$folder.'/'.$itemId;
// set new relative folder
$rel_url = $folder.'/'.$itemId;
// create directory
if(!is_dir($folder_url)) {
mkdir($folder_url);
}
}
// list of permitted file types, this is only images but documents can be added
$permitted = array('image/gif','image/jpeg','image/pjpeg','image/png');
// loop through and deal with the files
foreach($formdata as $file) {
// replace spaces with underscores
$filename = str_replace(' ', '_', $file['signature']);
// assume filetype is false
$typeOK = false;
// check filetype is ok
foreach($permitted as $type) {
if($type == $file['type']) {
$typeOK = true;
break;
}
}
// if file type ok upload the file
if($typeOK) {
// switch based on error code
switch($file['error']) {
case 0:
// check filename already exists
if(!file_exists($folder_url.'/'.$filename)) {
// create full filename
$full_url = $folder_url.'/'.$filename;
$url = $rel_url.'/'.$filename;
// upload the file
$success = move_uploaded_file($file['tmp_name'], $url);
} else {
// create unique filename and upload file
ini_set('date.timezone', 'Europe/London');
$now = date('Y-m-d-His');
$full_url = $folder_url.'/'.$now.$filename;
$url = $rel_url.'/'.$now.$filename;
$success = move_uploaded_file($file['tmp_name'], $url);
}
// if upload was successful
if($success) {
// save the url of the file
$result['urls'][] = $url;
} else {
$result['errors'][] = "Error uploaded $filename. Please try again.";
}
break;
case 3:
// an error occured
$result['errors'][] = "Error uploading $filename. Please try again.";
break;
default:
// an error occured
$result['errors'][] = "System error uploading $filename. Contact webmaster.";
break;
}
} elseif($file['error'] == 4) {
// no file was selected for upload
$result['nofiles'][] = "No file Selected";
} else {
// unacceptable file type
$result['errors'][] = "$filename cannot be uploaded. Acceptable file types: gif, jpg, png.";
}
}
i'm getting an error as it is not recognizing image from the url, which is in binary form. How can i decode the image and upload it in folder.? Also the image sent from the iphone is encoded, i have check in Xcode there is no code to decode the image.