首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php上传图片上传失败

php上传图片上传失败
EN

Stack Overflow用户
提问于 2014-08-31 18:06:47
回答 1查看 103关注 0票数 0

我正在尝试上传一个图片和标题从iOS到我的服务器。我有一个php正在成功地接收图像和标题,但是我得到了错误:上传文件时出错了,请再试一次。php出了什么问题?

代码语言:javascript
复制
<?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!";
}
?>

编辑:下面是iOS侧代码,但它可以运行

代码语言:javascript
复制
NSString *filenames = @"title name";      //set name here
        NSLog(@"%@", filenames);
        NSString *urlString = @"http://www.myserver.com/uploads.php";

        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:[@"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];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-01 03:34:47

因此,经过大量的工作(大约48小时),我设法使它发挥作用。iOS:

代码语言:javascript
复制
 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:

代码语言:javascript
复制
<?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 ";
}


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

https://stackoverflow.com/questions/25594796

复制
相关文章

相似问题

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