0

I know many people have been asking similar questions here but somehow I still don't get it working. To make it simpler I'm just trying to store a previously loaded UIImage in objectiveC (using C4 framework) like this:

-(void)setup {
    UIImage *myImage=[UIImage imageNamed:@"image.jpg"];

    NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0);
    // setting up the URL to post to
    NSString *urlString = @"my full server address/imageupload.php";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [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:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"dr.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];

    C4Log(@"returnString: %@", returnString);

}

And then my upload.php looks like this:

<?php
      $uploaddir = './'; //Uploading to same directory as PHP file
      $file = basename($_FILES['userfile']['name']);
      $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";
   }
   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 "http://mlab.taik.fi/UrbanAlphabets/{$file}" . "\r\nsize:" .  $_FILES['userfile']['size'] . "\r\ntype:" . $_FILES['userfile']['type']. "\r\npostsize:" . $postsize."\r\ncanupload:" . $canupload ."\r\ntempdir:" .$tempdir."\r\nmaxsize:".$maxsize;

?>

It looks as if 'upload_tmp_dir' is empty for some reason but I have no idea how to fix it... Here is the echo:

Temp file uploaded. 

 MY SERVER/dr.jpg

size:31476

type:application/octet-stream

postsize:100M

canupload:1

tempdir:

maxsize:100M

Any tipps?

suMi
  • 1,536
  • 1
  • 17
  • 30

2 Answers2

0

Use the full path in your script to the upload directory, also only move the temp file WHEN a temp file is uploaded. Example:

<?php
      $uploaddir = '/Volumes/Web/mlab/mlabwww/UrbanAlphabets/'; //Uploading to same directory as PHP file
      $file = basename($_FILES['userfile']['name']);
      $uploadFile = $file;
      $randomNumber = rand(0, 99999); 

      $newName = $uploadDir . $uploadFile;

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

         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 "http://mlab.taik.fi/UrbanAlphabets/{$file}" . "\r\nsize:" .  $_FILES['userfile']['size'] . "\r\ntype:" . $_FILES['userfile']['type']. "\r\npostsize:" . $postsize."\r\ncanupload:" . $canupload ."\r\ntempdir:" .$tempdir."\r\nmaxsize:".$maxsize;

    } else {
         echo "Temp file not uploaded. \r\n";
   }

?>
Joran Den Houting
  • 3,149
  • 3
  • 21
  • 51
0

temp directory will always be empty after the script has finished it's execution. so you have to move your file within the script..

and don't look for file it temp directory because it won't be there look for it where you moved..

this will help http://us2.php.net/move_uploaded_file.

Ronak K
  • 1,567
  • 13
  • 23
  • for more detailed explanation use [this](http://www.w3schools.com/php/php_file_upload.asp) example which is for php. – Ronak K Nov 15 '13 at 12:40
  • obviously I'm looking in where I'm trying to move the file but I cannot find it there. So it doesn't seem to get written.... – suMi Nov 15 '13 at 13:02
  • you need to send data in bytes i.e. multipart at the time of sending, it will always show application/octet-stream when it is received. – Ronak K Nov 15 '13 at 13:15
  • ah, i think i understand you didnt created the directory where you are moving file i think. first create the directory on server where you want to move file. – Ronak K Nov 15 '13 at 13:17
  • I tested the "image/jpg" and that didn't help. Also all examples online use "application/octet-stream". About the image directory... Yes, it does exist. I'm not putting it into any folder first. just plain where the php file is. – suMi Nov 15 '13 at 13:19