title: iphone SDK : Upload image from iphone to a php Server send empty file ?(sample code link inside)
body: <p>I try to send a photo and GPS location to server via PHP</p>
  
  <p>here is the PHP part:<a href="http://www.w3schools.com/PHP/php_file_upload.asp" rel="nofollow noreferrer">Copy from here</a>
  Saving the Uploaded File</p>
  
  <p>The examples above create a temporary copy of the uploaded files in the PHP temp folder on the server.</p>
  
  <p>The temporary copied files disappears when the script ends. To store the uploaded file we need to copy it to a different location:</p>
  
  <pre><code>&lt;?php
  if ((($_FILES["file"]["type"] == "image/gif")
  || ($_FILES["file"]["type"] == "image/jpeg")
  || ($_FILES["file"]["type"] == "image/pjpeg"))
  &amp;&amp; ($_FILES["file"]["size"] &lt; 20000))
    {
    if ($_FILES["file"]["error"] &gt; 0)
      {
      echo "Return Code: " . $_FILES["file"]["error"] . "&lt;br /&gt;";
      }
    else
      {
      echo "Upload: " . $_FILES["file"]["name"] . "&lt;br /&gt;";
      echo "Type: " . $_FILES["file"]["type"] . "&lt;br /&gt;";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb&lt;br /&gt;";
      echo "Temp file: " . $_FILES["file"]["tmp_name"] . "&lt;br /&gt;";
  
      if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
        echo $_FILES["file"]["name"] . " already exists. ";
        }
      else
        {
        move_uploaded_file($_FILES["file"]["tmp_name"],
        "upload/" . $_FILES["file"]["name"]);
        echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        }
      }
    }
  else
    {
    echo "Invalid file";
    }
  ?&gt;
  </code></pre>
  
  <p>The script above checks if the file already exists, if it does not, it copies the file to the specified folder.</p>
  
  <p>Note: This example saves the file to a new folder called "upload"</p>
  
  <p>now back to iOS part ,I only can send GPS location to server ,it's easy because just a string.</p>
  
  <p>and you can <a href="http://www.sendspace.com/file/cnaeh1" rel="nofollow noreferrer">download sample code here</a></p>
  
  <p>But an image is not,I do some search and found this <a href="https://stackoverflow.com/questions/1939335/upload-image-from-iphone-to-the-server-folder">discussion</a></p>
  
  <p>I directly put the code in my send GPS&amp;image button</p>
  
  <p>the best thing is it can feedback I send the file success or failed</p>
  
  <p>this is the most message I got after I send photo to server</p>
  
  <p><strong>1.Invalid file</strong></p>
  
  <p><strong>2.Return Code: <br />Upload: <br />Type: <br />Size: 0 Kb<br />Temp file: <br />Stored in: upload/</strong></p>
  
  <p>I follow some guide to save image to NSData </p>
  
  <pre><code>NSData *photoData= UIImageJPEGRepresentation(self.theimageView.image, 1.0);
  </code></pre>
  
  <p>and I also check there is data inside or not</p>
  
  <p>so simply add this code into Button action</p>
  
  <pre><code>if (photoData == nil) {
      NSLog(@"The photo is nothing !!!");
  }
  else {
      NSLog(@"Photo inside !!!!");
  </code></pre>
  
  <p>It works fine.......</p>
  
  <p><strong>But How To Upload An Image To Server ???</strong></p>
  
  <p><strong>maybe I need to give a file name then upload it ???</strong></p>
  
  <p>but  how to...</p>
  
  <p>I never use camera function and upload data before</p>
  
  <p>Hope someone can figure out this problem</p>
  
  <p><strong>Many Great Thanks !!!</strong></p>