[英]upload data from an iphone app to a web server with php
with almost no knowledge in PHP whatsoever, I am trying to send some data (NSData) from my iphone app to my web server (locally for now) but without success (The PHP uploader script is being called since I am getting its error message). 几乎没有PHP的知识,我试图从我的iPhone应用程序发送一些数据(NSData)到我的网络服务器(现在在本地)但没有成功(因为我收到错误消息,所以正在调用PHP上传程序脚本) 。 could anyone please help me understand what am I doing wrong here?
谁能帮助我理解我在这里做错了什么?
My iphone app at this point includes only the code below in the ViewDidLoad of my (one and only) viewController, which I have combined from some of the conversations around here: 我的iphone应用程序此时仅包含我的(一个且唯一的)viewController的ViewDidLoad中的代码,我从这里的一些对话中加入了它:
- (void)viewDidLoad
{
[super viewDidLoad];
NSData *data;
const char *bytestring = "black dog";
data = [NSMutableData dataWithBytes:bytestring length:strlen(bytestring)];
NSString *strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSString *urlString = @"http://localhost/uploader1.php";
NSString *filename = @"filename";
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}
I have also tried the following PHP uploader script: 我还尝试了以下PHP上传器脚本:
<?php
$target = "./upload/";
$target = $target.basename( $_FILES['userfile']['name']) ;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "SUCCESS";
}
?>
What am I doing wrong and how come I cant post this data? 我做错了什么,为什么我不能发布这些数据? Again, the PHP script is getting called cause I get the error message: Wrong file type...
再次,PHP脚本被调用因为我收到错误消息:错误的文件类型...
Any help would be much appreciated. 任何帮助将非常感激。
Thanks! 谢谢!
In your Objective-C code you are sending the file like this: 在您的Objective-C代码中,您发送的文件如下:
name=\"userfile\"; filename=\"%@.jpg\"\r\n",
so, in the PHP side, you should access $_FILES["userfile"]
, instead of $_FILE["file"]
or $_FILE["uploaded"]
, to get the correct information about your upload. 因此,在PHP方面,您应该访问
$_FILES["userfile"]
,而不是$_FILE["file"]
或$_FILE["uploaded"]
,以获取有关上传的正确信息。
If you are still experiencing problems, first suggestion is putting some traces server-side: 如果您仍然遇到问题,首先建议在服务器端添加一些跟踪:
print_r($_REQUEST);
will give you a list of all the $_POST/$_GET/cookies input parameters, so you can better know what is happening. 将为您提供所有$ _POST / $ _ GET / cookies输入参数的列表,以便您更好地了解正在发生的事情。 Furthermore, I would suggest also tracing:
此外,我建议还跟踪:
print_r($_FILES);
it could be that the ObjC side is sending wrong data, or that wrong data is read on the server side. 可能是ObjC端发送错误数据,或者在服务器端读取错误数据。
EDIT: the error you are getting: 编辑:你得到的错误:
"Wrong file type..."
depends on the fact that you are trying to calculate the size of the uploaded file as if it were an image: 取决于您尝试计算上传文件的大小,就好像它是一个图像:
$check = getimagesize($_FILES['userfile']['tmp_name']);
if($check[0]>0 && $check[1]>0 && $check['mime']=='image/jpg'){
while in fact you are sending a simple string as file content: 实际上你发送一个简单的字符串作为文件内容:
const char *bytestring = "black dog";
either you remove the check, or you upload a real file. 要么删除支票,要么上传真实文件。
EDIT: 编辑:
Use these lines: 使用这些行:
error_reporting(E_ALL);
ini_set("display_errors", 1);
to make error reporting more verbose, so you possibly know why it is failing... 使错误报告更详细的,所以你可能知道它为什么失败?
Did you try ASIHTTPRequest? 你尝试过ASIHTTPRequest吗? http://allseeing-i.com/ASIHTTPRequest/
http://allseeing-i.com/ASIHTTPRequest/
For target path dont use HTTP, use the method below. 对于目标路径,请勿使用HTTP,请使用以下方法。
Instead of doing file_put_contents(***WebSiteURL***...)
you need to use the server path to /cache/lang/file.php
(eg /home/content/site/folders/filename.php). 而不是使用
file_put_contents(***WebSiteURL***...)
您需要使用/cache/lang/file.php
的服务器路径(例如/home/content/site/folders/filename.php)。
You cannot open a file over HTTP and expect it to be written. 您无法通过HTTP打开文件并期望它被写入。 Instead you need to open it using the local path.
相反,您需要使用本地路径打开它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.