2012-04-02 46 views
0

我不知道問題出在我的PHP代碼還是在objective-c方面。我不明白這一點,所以將不勝感激任何幫助獲取文件從我創建到MySQL數據庫的iPad應用程序上傳。現在,只有文件的描述被髮布到數據庫中,但blob /文件不是。任何幫助將非常感激!爲什麼我的文件沒有從iPad上傳到MySQL DB?

這是我的PHP代碼:

<?php 
$username = "person"; 
$password = "xxxxxxx"; 
$database = "database"; 

mysql_connect(localhost,$username,$password); 
mysql_select_db($database) or die("Unable to select database"); 

$file = $_FILES['file']; 
$name = $file['tmp_name']; 
$testpage = file_get_contents($name); 
$testpage = mysql_real_escape_string($testpage); 

mysql_query("INSERT INTO tbldocs(Title,Document) VALUES('some title','$testpage')");    
mysql_close(); 
?> 

這是我的目標c代碼,包裝成一個HTTP分組

NSMutableDictionary* post_dict = [[NSMutableDictionary alloc] initWithCapacity:2]; 
[post_dict setObject:@"test_value" forKey:@"test_key"]; 
[post_dict setObject:[NSURL fileURLWithPath:[pdfUrl absoluteString]] forKey:@"file"]; 
NSData* regData = [self generateFormData:post_dict]; 
[post_dict release]; 

NSMutableURLRequest* post = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:@"http://myserver/upload.php"]]; 
[post addValue: @"multipart/form-data; boundary=_insert_some_boundary_here_" forHTTPHeaderField: @"Content-Type"]; 
[post setHTTPMethod: @"POST"]; 
[post setHTTPBody:regData]; 
NSURLResponse* response; 
NSError* error; 
NSData* result = [NSURLConnection sendSynchronousRequest:post returningResponse:&response error:&error]; 
NSLog(@"%@", [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease]); 

謝謝, 羅西

回答

1

可變$file['name']是在上傳中發佈的文件的名稱(例如本地系統的文件名)。

變量$file['tmp_name']是服務器上文件的名稱。這是包含要放入數據庫的內容的文件的名稱。

這應該解決php方面。 Objective-C代碼稍微複雜一些。您需要手動構建上傳中使用的mime編碼內容。有上CocoaDev一個simple example在generateFormData方法,在線路例如:

[post_dict setObject:[NSURL fileURLWithPath:@"/Butterfly.tif"] forKey:@"file1"]; 

他在這裏設置PHP相當於$_FILE['file1']可變的,你的情況,你可能想選擇forKey:@"file"代替。

編輯自包含的同步變體。我有以下參數:DestUrl - 包含目標的NSURL,fileName - 包含文件名稱的NSString,最後是包含文件內容的NSData(包含文件內容的NSData)FileData

NSMutableData *data = [[NSMutableData alloc] initWithCapacity:100]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:DestUrl]; 
[request setHTTPMethod:@"POST"]; 
// Define the boundary 
NSString *boundary = [NSString stringWithFormat:@"weasel_grapple_%ld_foo", (long)time(NULL)]; 
// Tell MIME that content type 
[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] 
    forHTTPHeaderField:@"Content-Type"]; 
// First piece of data 
[data appendData:[[NSString stringWithFormat:@"--%@\n", boundary] dataUsingEncoding:NSASCIIStringEncoding]]; 
// Add in the form field name (name), filename 
[data appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\n", 
        @"file", fileName] dataUsingEncoding:NSASCIIStringEncoding]]; 
// And it's binary data here 
[data appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\n\n"] dataUsingEncoding:NSASCIIStringEncoding]]; 
// This is content of the file 
[data appendData:FileData]; 
// Need a blank link as a separator of form-data items 
[data appendData:[[NSString stringWithString:@"\n"] dataUsingEncoding:NSASCIIStringEncoding]]; 
// Mark the end of the upload message 
[data appendData:[[NSString stringWithFormat:@"--%@--\n", boundary] dataUsingEncoding:NSASCIIStringEncoding]]; 

[request setHTTPBody:data]; 

NSURLResponse *response; 
NSError *error; 
NSData *retData = [NSURLConnection sendSynchronousRequest:request 
              returningResponse:&response 
               error:&error]; 

如果你想利用一個文件的URL,並把它變成的FileData然後執行: 的NSData *的FileData = [NSData的dataWithContentsOfFile:fileURL]。

在PHP方面,首先用一個簡單的上傳表單進行測試,確保上傳的數據正在進入數據庫並從那裏開始工作。

我的php文件上傳到數據庫的片段看起來像。這是非常可怕的,但它得到跨線:

foreach ($_FILES as $key => $value) { 
    $file_tag = $key; 
    $fname = basename($value['name']); 
    $name = $value['tmp_name']; 

    $testpage = file_get_contents($name); 
$testpage = addslashes($testpage); 

    mysql_query("INSERT INTO tbldocs(Title,Document) VALUES('some title','$testpage')"); 
} 

還要注意上傳:如果您上傳的文件大於64K,那麼你必須指定Document作爲一個字段類型的MEDIUMBLOB。常規的BLOB數據類型最多隻支持64K數據。

+0

謝謝,我給了一個鏡頭 - 看到我的編輯,但仍然沒有骰子...我得到的是數據庫中的文件的名稱。 – Rossi 2012-04-02 21:23:27

+0

我使用他的generateFromData方法,並按照您的建議修改了我的代碼。上面顯示的編輯。它仍然不起作用,但我真的很感謝你的幫助!你碰巧看到其他可能錯誤的東西嗎? – Rossi 2012-04-02 22:01:43

+0

你嚴重地是我的英雄。認真對待。感謝您的幫助!我真的很感激它!它的工作原理真的很棒。 – Rossi 2012-04-04 05:05:45

相關問題