首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASIFormDataRequest + PHP问题

ASIFormDataRequest + PHP问题
EN

Stack Overflow用户
提问于 2010-12-06 08:48:22
回答 2查看 2K关注 0票数 0

我正在尝试使用ASIFormDataRequest将文件上传到服务器(从自定义的mac os应用程序)。在我的cocoa/obj-c代码中,我有:

代码语言:javascript
复制
- (IBAction)uploadFile:(id)sender 
{ 
 [networkQueue reset];
 [networkQueue setShowAccurateProgress:YES];
 [networkQueue setUploadProgressDelegate:progressIndicator];
 [networkQueue setRequestDidFailSelector:@selector(postFailed:)];
 [networkQueue setRequestDidFinishSelector:@selector(postFinished:)];
 [networkQueue setDelegate:self];

 ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:@"http://domain.com/to/upload.php"]] autorelease];

 [request setFile:@"/path/to/file.jpg" forKey:@"file"];

 [networkQueue addOperation:request];
 [networkQueue go];

}

- (void)postFinished:(ASIHTTPRequest *)request
{
 NSLog(@"Post Success");
}
- (void)postFailed:(ASIHTTPRequest *)request
{
 NSLog(@"Post Failed");
}

服务器上的PHP代码如下所示:

代码语言:javascript
复制
$target_path = "files/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
  echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
 } else{
  echo "There was an error uploading the file, please try again!";
 }

当我尝试使用此代码上传文件时,在客户端收到"Post Success“响应,但文件从未出现在我的服务器上。为了以防万一,我使用CHMODed将文件文件夹设置为777,但它似乎仍然不起作用。有没有人有什么建议,或者在我处理这个问题的方式上发现了错误?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-06 17:15:59

面向未来读者的更新。这里的基本问题是,您必须完全像这样创建ASIFormDataRequest项:

代码语言:javascript
复制
NSURL *url = [NSURL URLWithString:@"https://www.you.com/cgi-bin/you.cgi?stuff"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

Do not add in more release,retain,autorelease,self.等!这就是全部。

嗯。我不能完全确定那里的自动释放是正确的。在ASIHttpRequest中,您必须小心不要丢失最新的指针。

先把它作为一个简单的(非排队的,异步的)请求,使用这个经过数十亿次测试的模式,是否值得先尝试一下:

代码语言:javascript
复制
-(void) sendSomethingAnything
    {
    NSURL *url = [NSURL URLWithString:@"https://www.you.com/cgi-bin/you.cgi?stuff"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

    [request setPostValue:@"fakeIDstring" forKey:@"id"];
    [request setPostValue:@"anotherCalue" forKey:@"username"];
    // your file here ..

    [request setDelegate:self];
    [request setDidFinishSelector:@selector(postFinished:)];
    [request setDidFailSelector:@selector(postFailed:)];

    [request startAsynchronous];
    }

只需粘贴您的URL,您可以立即尝试。这将消除许多问题,并使我们更接近问题。在完成的例程中,获取并显示结果,如下所示...

代码语言:javascript
复制
NSData *newStringData = [request responseData];
NSString *x = [[[NSString alloc] initWithData:newStringData
                  encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"result text from server is %@", x);

最后在你的PHP中。我发现添加一行代码从php向我发送电子邮件来说明到底发生了什么是很有用的。或者,使用"wall“命令,在使用客户端时只在服务器上打开一个shell。这是唯一能在你工作的时候看到到底发生了什么的方法。

(事实上,我使用的是老式的perl,而不是未来派的php,所以我不能提供任何代码!:)

票数 4
EN

Stack Overflow用户

发布于 2010-12-08 14:51:39

我这样做了,而且它肯定会这样导入# -> "ASIFormDataRequest.h“

NSURL *url = NSURL:@“XXXXXX”;ASIFormDataRequest *request = ASIFormDataRequest requestWithURL:url;

请求setPostValue:@"fakeIDstring“forKey:@"id";请求setPostValue:@"anotherCalue”forKey:@“用户名”;//您的文件在这里..请求setFile:@“路径/文件名.png”forKey:@“上传的文件”;请求setDelegate:self;请求setDidFinishSelector:@ setDidFailSelector:@selector(requestWentWrong:);(requestDone:);请求requestDone请求startAsynchronous;

然后用php代码

$target_path =“文件/”;

$target_path = $target_path。basename( $_FILES‘’uploadedfile‘);

if(move_uploaded_file($_FILES‘’uploadedfile‘,$target_path)) {回显“该文件”。基本名称($_FILES‘’uploadedfile‘)。“已上传”;}否则{ echo“上传文件时出错,请重试!”;}

请注意,在处理这个上传文件的php文件旁边应该有一个"files“文件夹。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4362303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档