zlib/libz.dylib解压缩
问题描述:
我完全不熟悉php,需要一些脚本帮助。当打开工作正常时,无法弄清为什么写入不起作用。 我收到的文件使用- (NSData *)compress:(NSData *)
from this example使用libz.dylib框架进行压缩。zlib/libz.dylib解压缩
代码:
<?php
error_reporting(E_ALL);
$dir = "upload/";
if(!file_exists($dir)){
mkdir($dir);
}
$target = $dir . basename($_FILES['uploaded']['name']);
$zip = $target . ".gz";
$file = $target . ".jpeg";
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $zip))
{
$arr = gzfile($zip);
if(is_writable($dir)){
if(! $fh = fopen($file, 'w')){
echo "NO - cannot open file ($file)";
exit;
}
foreach($arr as $value){
if(fwrite($fh, $value) === FALSE){
echo "NO - failed to write to file ($file)";
exit;
}
}
fclose($fh);
echo "YES - successfully written to file ($file)";
}
else{
echo "NO - folder ($dir) not writable";
}
}
else {
echo "NO - unable to move_uploaded_file";
}
?>
减压不起作用。该帖子的要求也与上面的example相同。 有没有人知道什么是错的?示例中的compress-method使用了什么压缩? (放气或压缩或别的东西)
这一职位的代码如下所示:
- (NSURLRequest *)postRequestWithURL: (NSURL *)url
data: (NSData *)data // IN
boundry: (NSString *)boundry // IN
{
// from http://www.cocoadev.com/index.pl?HTTPFileUpload
NSMutableURLRequest *urlRequest =
[NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:
[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundry]
forHTTPHeaderField:@"Content-Type"];
NSMutableData *postData =
[NSMutableData dataWithCapacity:[data length] + 512];
[postData appendData:
[[NSString stringWithFormat:@"--%@\r\n", boundry] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:
[[NSString stringWithFormat:
@"Content-Disposition: form-data; name=\"%@\"; filename=\"test%d\"\r\n", FORM_FLE_INPUT, counter++]
dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:
[@"Content-Type: application/gzip\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:
[@"Content-Encoding: gzip\r\n\r\n"dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:data];
[postData appendData:
[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundry] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:postData];
return urlRequest;
}
基本相同,从cocoadev的例子,但我已经添加了
[postData appendData: [@"Content-Type: application/gzip\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData: [@"Content-Encoding: gzip\r\n\r\n"dataUsingEncoding:NSUTF8StringEncoding]];
因为我虽然这将有助于gfile($file)
,但是当我尝试在终端中gunzip .gz时,我仍然收到错误gzip:upload/test1.gz:not in gzip format
我还阅读了this site,如果我将Content-Type = gzip(如上所述)添加到HTTP Post请求,Integration Appliance将解压缩HTTP POST主体,但我无法使其工作。 (我已经用cocoadev中的非常简单的php脚本试过了,因为我不需要我的php-scrip中的gzfile()
-stuff)
答
尝试启用警告和通知输出。它可能会帮助你获得更详细的信息。 只是把
error_reporting(E_ALL);
在你的代码的第一行
是错误呼应@heximal?或我会在哪里看到它? –
取决于您在哪里运行脚本。如果在命令行界面中启动它,则会在控制台中看到警告。如果你通过浏览器运行脚本 - 警告将出现在生成的html代码中 – heximal
好吧,我在浏览器中运行它,并且我只能得到“NO2”:/ –