2

Its not a duplicate because the issue was NOT the undefined index.

I originally had the issue from my iOS app but I created an html form. It works now from html but not from iOS.

The form code is:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <form enctype="multipart/form-data" action="uploadPhoto.php" method="POST">
        Choose a file to upload: <input name="userfile" type="file" /><br />
        <input type="submit" value="Upload File" />
    </form>
</body>
</html>

and the php code for uploadPhoto.php is:

<?php

////////////////
echo 'file count=', count($_FILES),"\n";
var_dump($_FILES);
echo "\n";
////////////////
      $uploaddir = './photos/';
      if(isset($_FILES['userfile']['name'])){
        $file = basename($_FILES['userfile']['name']);
        echo "file is set";
        }
      $uploadFile = $file;
      $randomNumber = rand(0, 99999); 

      $newName = $uploaddir . $uploadFile;

    if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
   echo "Temp file uploaded. \r\n";
    } else {
    echo "Temp file not uploaded. \r\n";
   }
   if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
         $postsize = ini_get('post_max_size');
         $canupload = ini_get('file_uploads'); 
         $tempdir = ini_get('upload_tmp_dir');
         $maxsize = ini_get('upload_max_filesize');
         echo "\r\n" .  $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
    }

?>

And I get this from a browser upload:

file count=1 array(1) { ["uploaded file"]=> array(5) { ["name"]=> string(9) "Koko2.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/phpH6pnPi" ["error"]=> int(0) ["size"]=> int(72727) } } Temp file not uploaded.

I do have 777 for that photos folder. I echoed and get: 1 for file_uploads 8M for post_max_size 128M for upload_max_filesize

The server error log says: User undefined index in uploadPhoto.php lines 18, 24 & 29 which refer to each mention of $_FILES['userfile']['name'].

Here is the iOS code:

NSData *imageData = UIImageJPEGRepresentation(image, 0.6);

// 0 Define URL
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myservers.com/app/photos/uploadPhoto.php"]];

// 1 Create session configuration
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPMaximumConnectionsPerHost = 1;

// 2 Create session & variables
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

// 3 Create request & body & parameters
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];

// 4 Create object to put content into...
NSMutableData *body = [NSMutableData data];

// 5 Create body of the request to put into the object
NSString *boundary = @"0xKhTmLbOuNdArY";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *filename = [NSString stringWithFormat:@"someName.jpg"];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\" filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\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];

// 3
self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:body];
// 4
self.uploadView.hidden = NO;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
// 5
[_uploadTask resume];

It only fails from my ios code. I was unable to find a similar issue in the 11 other answers mentioned in SO.

What could be the problem?

marciokoko
  • 4,988
  • 8
  • 51
  • 91
  • 3
    Your HTML named it "uploadedfile" yet in your PHP code you called it as "userfile" - what's going on? – Bailey Herbert Aug 02 '14 at 18:18
  • @BaileyHerbert that is the problem OP has. That is the undefined index. – putvande Aug 02 '14 at 18:19
  • Indeed, and I just pointed it out for him, with a rhetorical question. :) – Bailey Herbert Aug 02 '14 at 18:20
  • `name="uploadedfile"` <= yep, that'll do it. – Funk Forty Niner Aug 02 '14 at 18:24
  • 1
    Guys, thanks for the enthusiasm, but its not the user file vs uploaded file mismatch. I changed it to userfile on both and it still fails. I posted that because I had been testing with different code. Also, id like to talk to whoever marked my question down. Ive looked at the posted questions/answers and took many of the suggestions mentioned there but its not my issue because it hasn't worked. – marciokoko Aug 02 '14 at 18:30
  • 1
    I edited the code with the ISSET change I made. It seems that value is NOT set. Why would that happen? – marciokoko Aug 02 '14 at 18:51
  • @BaileyHerbert Can you please read the edited question – marciokoko Aug 03 '14 at 18:45
  • You know, you guys should really help by coming back and answering posts. its seems you were all quick to down-vote and mark my question as duplicate when it clearly isn't. I concede it wasn't clearly written, but I took the time to make it better. I believe if you are meant to help, you should also take the time to follow up on questions once they are cleared up. It seems to be a very valid issue which many other SO'ers are having problems with. – marciokoko Aug 04 '14 at 20:16
  • This question should be re-opened because the suggested "duplicate" is interesting, but is not the root issue here. Notably, the problem is that the Objective-C code is missing a semicolon between `name` and `filename` entries in the `Content-Disposition` line. Thus, the malformed `multipart` entry means the server will not recognize `$_FILES`, but it is remedied if the semicolon is added. – Rob Aug 08 '14 at 11:32
  • @Rob Thanks so much! If you send me your paypal, I will send you a tip :-) Oh and post it as an answer so I can accept it – marciokoko Aug 08 '14 at 20:55
  • Lol. I cannot answer this question because someone marked this as a duplicate of that other question (which it isn't). Until it gets sufficient votes to reopen the question and eliminate that erroneous "duplicate", I can't answer it. But, you've got your answer, which is the important thing! – Rob Aug 09 '14 at 04:53

0 Answers0