ASIFormDataRequest + PHP问题 [英] ASIFormDataRequest + PHP Issues

查看:92
本文介绍了ASIFormDataRequest + PHP问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to upload a file to a server (from a custom mac os application) using ASIFormDataRequest. In my cocoa/obj-c code, I have:

- (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代码如下:

The PHP code on the server looks like this:

$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!";
 }

当我尝试使用此代码上传文件时,成功响应,但文件从未显示在我的服务器上。我有CHMODed文件文件夹到777,以防万一,但它仍然没有工作。有没有人有建议,或看到我接近这个错误?

When I try to upload a file using this code, I get a "Post Success" response on the client end, but the file never shows up on my server. I have CHMODed the files folder to 777 just in case, but it still didn't seem to work. Does anyone have a suggestion, or see an error in the way I'm approaching this?

谢谢!

推荐答案

更新未来读者 。这里的根本问题是你必须像下面这样创建ASIFormDataRequest项:

Update for future readers. The fundamental problem here was that you must create ASIFormDataRequest items exactly like this:

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., etc etc! That's all it is.

嗯。我不完全确定autorelease有正确。你必须小心不要在ASIHttpRequest中丢失resuest指针。

Hmm. I'm not entirely sure the autorelease there is correct. You have to be careful about not losing resuest pointers in ASIHttpRequest.

设置一下,它是值得尝试它作为一个简单的(非排队,异步)请求,使用已被测试过几十亿次的这种模式:

Setting that aside, would it be worth trying it first as a simple (non-queued, asynchronous) request, using exactly this pattern which has been tested billions of times:

-(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];
    }

只需粘贴到您的网址,您可以立即尝试。这将消除很多问题,让我们更接近的问题。在done例程中,获取并显示结果像这样...

Just paste in your URL and you can try that immediately. That will eliminate a lot of problems and get us closer to the problem. In the done routine, get and show the result like this...

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

最后在您的PHP。我发现添加一行代码,EMAILS ME从PHP,说明什么是发生有用的。或者,使用wall命令,并且只有在您使用客户端时在您的服务器上打开一个shell。

Finally in your PHP. I find it useful to add a line of code that EMAILS ME from the php stating what the heck is going on. Alternately, use a "wall" command, and just have a shell open on your server as you work with the client. It's the only way to see what the heck is going on as you work.

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

(In fact I use old-fashioned perl rather than futuristic php, so I can't offer any code on that end! :) )

这篇关于ASIFormDataRequest + PHP问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆