【问题标题】:How do I upload a file from my iPad app to PHP to MYSQL?如何将文件从 iPad 应用程序上传到 PHP 到 MYSQL?
【发布时间】:2011-06-28 00:49:19
【问题描述】:

我在互联网上找到了很多关于这件事的建议,但我尝试的任何方法似乎都不起作用。这是我的 Objective-c 代码,基于这里的教程:http://iphone.zcentric.com/2008/08/29/post-a-uiimage-to-the-web/

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;   
?> 

如果您发现任何 sn-p 代码有任何问题,请告诉我 - 或者它们的交互方式是否存在差距或有任何建议。

谢谢! 回复

【问题讨论】:

  • 这是实际代码吗?您在 Objective-C 中初始化了一个 Filename,但取消引用了一个 filename 变量。
  • 此 PHP 示例必须来自“不良”站点或处理过时 PHP 版本的站点。 addlashes 是一种主要的弃用方法,用于为 sql 语句准备数据。
  • 在大多数情况下(文件名部分),这是实际的代码......我了解很多objective-c,但我不得不承认,我不明白发生了很多事情这里 - 我以为我将相关文件名放在 FILE 而不是示例中的 ipod.png 中。 PHP 来自这个站点:php-mysql-tutorial.com/wikis/mysql-tutorials/… 我应该放弃它并重新开始使用 PHP 吗?或使用这个?
  • 另外,@sarnold,真正的代码在两个地方都有小写的 f-> 文件名。不过很好,谢谢,我现在已经编辑了。

标签: php mysql ipad upload nsdata


【解决方案1】:

看起来这个包装器可以解决你在objective-c方面的大部分问题。

http://allseeing-i.com/ASIHTTPRequest/How-to-use

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多