2011-06-28 56 views
1

我在互聯網上發現了很多關於此事的建議,但沒有任何我嘗試似乎工作。這是我的Objective-C代碼的基礎上,這裏的教程:http://iphone.zcentric.com/2008/08/29/post-a-uiimage-to-the-web/如何從iPad應用程序上傳文件到PHP到MYSQL?

NSString *importFilePath = [openedUrl path]; 
NSString *filename = [importFilePath lastPathComponent]; 

//I changed this from initwithcontentsoffile because the program gets handed a url 
//so I thought it made the most sense 
NSData *data = [[NSData alloc] initWithContentsOfURL:openedUrl]; 


NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:@"http://mysite.php?secret=%@&secret2=%@"]]; 
NSLog(@"the url: %@", [lastView uploadData:openedUrl]); 
[request setHTTPMethod:@"POST"]; 

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\";filename=\"%@\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:data]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
// setting the body of the post to the reqeust 
[request setHTTPBody:body]; 

// now lets make the connection to the web 
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

我改變了文件名的新文件的文件名和URL是我自己。除此之外,它與教程非常相似。

我改變了我的php文件。當我在一個URL中作爲文件硬編碼時,它效果很好。但是,當我嘗試從我的應用程序傳遞一個文件時,只有名稱被傳遞,沒有文件內容。我不確定不工作的部分是來自我的php文件還是來自我的objective-c代碼。這是新的和改進的PHP文件:

<? 
$username=$_GET['secret']; 
$password=$_GET['secret2']; 
$database="somedb"; 

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

$fileName = $_FILES['userfile']['name']; 
$tmpName = $_FILES['userfile']['tmp_name']; 

$testpage = file_get_contents('$tmpName'); 
$testpage = mysql_real_escape_string($testpage); 
mysql_query("INSERT INTO tbldocuments(Description,Document) VALUES('$fileName','$testpage')"); 
mysql_close; 
?> 

如果你能看到什麼不好的代碼,通過片段,請讓我知道 - 或者如果在它們之間相互作用或有任何建議的方式一定的差距。

謝謝! R

+0

這是實際的代碼嗎?你初始化一個'Filename',但是在Objective-C中取消引用一個'filename'變量。 – sarnold

+0

這個PHP例子必須來自一個'不好'的網站或一個處理過時的PHP版本的網站。 addslashes是爲sql語句使用準備數據的一個主要被棄用的方法。 –

+0

對於大部分(文件名部分),這是實際的代碼...我瞭解了很多Objective-C,但我必須承認,我不明白這裏發生了什麼 - 我以爲我正在把FILE中的相關文件名,而不是示例中的ipod.png。而PHP是從這個網站:http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx我應該拋棄它,並重新開始與PHP?或與此工作? – Rossi

回答

相關問題