php上载图片无法上载
我正在尝试将图像和标题从iOS上传到我的服务器。我有一个php正在成功地接收图像和标题,但随后我得到了错误:上传文件时出错,请重试。php出了什么问题php上载图片无法上载,php,mysql,ios,image,upload,Php,Mysql,Ios,Image,Upload,我正在尝试将图像和标题从iOS上传到我的服务器。我有一个php正在成功地接收图像和标题,但随后我得到了错误:上传文件时出错,请重试。php出了什么问题 <?php $myparam = $_POST['userfile']; //getting image Here $mytextLabel= $_POST['filenames']; //getting textLabe Here echo $myparam; echo $mytextLabel; $target_path
<?php
$myparam = $_POST['userfile']; //getting image Here
$mytextLabel= $_POST['filenames']; //getting textLabe Here
echo $myparam;
echo $mytextLabel;
$target_path = "upload/";
$target_path = $target_path . basename( $_FILES['myfile']['name']);
if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['myfile']['name']) . " has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}
?>
因此,经过大量的工作(大约连续48小时),我设法让它工作。
iOS:
NSData *imageData =UIImageJPEGRepresentation(imageView.image, 90);
NSString *imgPath= @"http://www.myserver.com/uploads.php";
NSString *filenames = labelb.text; //name of picture excluding .jpg
NSString *urlString = imgPath;
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[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 *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filenames] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[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];
if([returnString isEqualToString:@"yes"])
{
[self finitop];
}
else {
[spinner stopAnimating];
label.text = @"Posted Empty Because Upload Failed";
}
<?php
$uploaddir = './upload/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
echo "yes";
}
}
else
{
echo "NO ";
}
?>
PHP:
NSData *imageData =UIImageJPEGRepresentation(imageView.image, 90);
NSString *imgPath= @"http://www.myserver.com/uploads.php";
NSString *filenames = labelb.text; //name of picture excluding .jpg
NSString *urlString = imgPath;
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[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 *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filenames] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[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];
if([returnString isEqualToString:@"yes"])
{
[self finitop];
}
else {
[spinner stopAnimating];
label.text = @"Posted Empty Because Upload Failed";
}
<?php
$uploaddir = './upload/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
echo "yes";
}
}
else
{
echo "NO ";
}
?>
向我们展示您的HTML表单。没有HTML…它直接从iOS上传到此页面。如果你的文件输入是“userfile”,你必须使用$\u文件['userfile']['tmp\u name']
。还要确保您的表单enctype=multipart/formdata
刚刚用我所有的代码编辑了我的问题