Blade
Blade

Reputation: 1437

Uploading images to server doesn't work

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)

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];

and my php file:

<?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'] ;
}

 ?>

Upvotes: 0

Views: 3952

Answers (2)

Adeel Mughal
Adeel Mughal

Reputation: 736

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

Upvotes: 1

Electronick
Electronick

Reputation: 1122

$_FILES['userfile']['type'] The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.

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

If you want to check mime-type you should follow another way such as mime_content_type()

Upvotes: 1

Related Questions