将图像上传到服务器不起作用 [英] Uploading images to server doesn't work

查看:116
本文介绍了将图像上传到服务器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过xcode中的php请求将图片上传到我的服务器。我尝试了很多可能性,但到目前为止似乎没有任何效果。我不熟悉php,所以也许存在错误。现在它只是给我错误不正确的文件类型,但我肯定使用jpg图像。 (不止一次尝试过)

I'm trying to upload pictures to my server via php request in xcode. I tried many possibilities but as of now nothing seems to work. I'm not that well acquainted with php, so maybe there lies the error. Right now it is just giving me the errors "Incorrect file type", but I'm definitely using a jpg image. (Tried it with more than once)

这是我在xcode中所拥有的:

Here is what I have in xcode:

NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
NSString *urlString = @"http://myserver/upload.php";

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------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 stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"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]];
[request setHTTPBody:body];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

和我的php文件:

<?php
$uploaddir = '/images/';      //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;

if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
echo "Temp file uploaded. \r\n";
} else {
echo "Temp file not uploaded. \r\n";
}

if ((!($_FILES['userfile']['type'] == "image/jpeg")) && (!($_FILES['userfile']['type']    == "image/png")))           
{
exit("Incorrect file type.  " . $_FILES['userfile']['type'] . " is the file type you     uploaded."); 
}


if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    $postsize = ini_get('post_max_size');   
    $canupload = ini_get('file_uploads');    
    $tempdir = ini_get('upload_tmp_dir');   
    $maxsize = ini_get('upload_max_filesize');
    echo "http://www.mycompanyname.com/myappname/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
}

 ?>


推荐答案

检查文件类型并检查目录路径$ uploaddir = '/图片/';这是对的吗?这段代码对我来说很好。

Check file types and also check directory path $uploaddir = '/images/'; is it correct? this code is working fine for me.

这篇关于将图像上传到服务器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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