开发者

zlib/libz.dylib decompression

I'm totally new to php and need some help with a script. can't figure out why the write doesn't work when the open works just fine. The file I receive is zipped using the - (NSData *)compress:(NSData *) from this example which use the libz.dylib framework.

code:

<?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 f开发者_运维知识库ile ($file)";
    }
    else{
        echo "NO - folder ($dir) not writable";
    }
}
else {
    echo "NO - unable to move_uploaded_file";
}
?>

the decompression doesn't work. the post request is also the same as in the example above. does anyone know what's wrong? what compression does the compress-method in the example use? (deflate or compress or something else)

the code for the post looks like this:

- (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;
}

basically the same as in the example from cocoadev but i've added

[postData appendData: [@"Content-Type: application/gzip\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData: [@"Content-Encoding: gzip\r\n\r\n"dataUsingEncoding:NSUTF8StringEncoding]]; 

because i though this would help the gfile($file) but when i try to gunzip the .gz in terminal i still get the error gzip: upload/test1.gz: not in gzip format

I also read on this site that if I add Content-Type = gzip (as I do above) to the HTTP Post request the Integration Appliance will decompress the HTTP post body but I can't get this to work either. (I've tried this with the very simple php-script that is in the example from cocoadev, since I don't need the gzfile()-stuff in my php-scrip)


try to enable warnings and notices outputs. it may help you to get more detailed information. just put

error_reporting(E_ALL);

at the first line of your code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜