2011-08-03 107 views
0

我完全不熟悉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)

回答

0

嘗試啓用警告和通知輸出。它可能會幫助你獲得更詳細的信息。 只是把

error_reporting(E_ALL); 

在你的代碼的第一行

+0

是錯誤呼應@heximal?或我會在哪裏看到它? –

+0

取決於您在哪裏運行腳本。如果在命令行界面中啓動它,則會在控制檯中看到警告。如果你通過瀏覽器運行腳本 - 警告將出現在生成的html代碼中 – heximal

+0

好吧,我在瀏覽器中運行它,並且我只能得到「NO2」:/ –