1

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Leroy Mikenzi
  • 792
  • 6
  • 22
  • 46

2 Answers2

1

The problem might be caused by truncation of the URL (data) due to a max length? See 1 and 2.

Community
  • 1
  • 1
tersmitten
  • 1,310
  • 1
  • 9
  • 23
0

I think it can be done by using simple PHP functions. Generally i use base64 encoding/decoding functions to upload images using webservices.

Below is a code snipt.

$upload_file = $path_to_folder . '/' . $file_name;
file_put_contents($upload_file, base64_decode($image, true));
//$filesize = filesize($upload_file);
chmod($upload_file, 0777);

Here $path_to_folder will be Your relative path to destination. $file_name will be any dynamic name and can have any extension(jpg,png), for example :- $file_name = 'xyz.png'.

"$image" will be a base64 encoded string that we get from mobile device. Last one line is to set 777 permission of that uploaded file.

I hope this will help you in your task..

Yashrajsinh Jadeja
  • 1,699
  • 1
  • 16
  • 21