我正在使用 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 中检查了没有代码来解码图像。