简体   繁体   English

无法将图片上传到服务器

[英]Uploading images to server doesn't work

I'm trying to upload pictures to my server via php request in xcode. 我正在尝试通过xcode中的php请求将图片上传到我的服务器。 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. 我对php不太了解,因此可能存在错误。 Right now it is just giving me the errors "Incorrect file type", but I'm definitely using a jpg image. 现在,它只是给我错误“文件类型不正确”,但我绝对使用的是jpg图片。 (Tried it with more than once) (尝试了不止一次)

Here is what I have in xcode: 这是我在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];

and my php file: 和我的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'] ;
}

 ?>

$_FILES['userfile']['type'] The mime type of the file, if the browser provided this information. $ _FILES ['userfile'] ['type']文件的mime类型(如果浏览器提供了此信息)。 An example would be "image/gif". 一个例子是“ image / gif”。 This mime type is however not checked on the PHP side and therefore don't take its value for granted. 但是,不会在PHP端检查此mime类型,因此不要将其值视为理所当然。

http://php.net/manual/en/features.file-upload.post-method.php http://php.net/manual/zh/features.file-upload.post-method.php

If you want to check mime-type you should follow another way such as mime_content_type() 如果要检查mime-type,则应遵循另一种方法,例如mime_content_type()

Check file types and also check directory path $uploaddir = '/images/'; 检查文件类型并检查目录路径$ uploaddir ='/ images /'; is it correct? 这是正确的吗? this code is working fine for me. 这段代码对我来说很好用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM