1

我正在使用 cakephp 作为 Iphone 应用程序的后端为 IphoneAPP 编写网络服务。Everthing 可以很好地更新/删除/编辑数据库中的数据,换句话说,iphone 应用程序与数据库交互性完美运行。

现在在应用程序中进行了一些修改后,我不得不在从 iphone 发布时保存图像。我们发布时生成的 URL 是这样的http://somedomainname.com/customer/add/?name=abc&number=165adf&image=89504e470d0a1a0a0000000d494844520000004e0000005f08060000004620f1230000001974455874536f

上面的 URL 包含二进制的图像值,即 apng 图像。我想解码二进制代码并将其上传到文件夹中。我已经使用代码上传文件

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

我收到一个错误,因为它无法从 url 识别图像,它是二进制形式的。如何解码图像并将其上传到文件夹中。?从 iphone 发送的图像也被编码,我在 Xcode 中检查了没有代码来解码图像。

4

2 回答 2

1

问题可能是由于最大长度而截断 URL(数据)引起的?见12

于 2013-08-02T12:27:26.057 回答
0

我认为可以通过使用简单的 PHP 函数来完成。通常我使用 base64 编码/解码功能来使用 web 服务上传图像。

下面是一个代码片段。

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

这里 $path_to_folder 将是您到目的地的相对路径。$file_name 将是任何动态名称并且可以具有任何扩展名(jpg,png),例如:- $file_name = 'xyz.png'。

"$image" 将是我们从移动设备获得的 base64 编码字符串。最后一行是设置上传文件的777权限。

我希望这对您的任务有所帮助..

于 2013-08-03T09:45:44.180 回答