我想使用ASIFormDataRequest將文件上傳到服務器(從自定義mac os應用程序)。在我的可可/ OBJ-C代碼,我有:ASIFormDataRequest + PHP問題
- (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代碼如下所示:
$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,但它似乎仍然無法工作。有沒有人有建議,或看到我接近這個錯誤?
謝謝!
+1用於在創建請求時刪除無關的alloc + init + autorelease。我已經完美的成功上傳文件到一個像喬的代碼的PHP頁面。 – 2010-12-06 09:24:55