title: Image upload/receive API
body: <p>I'd like to offer a simple API on my website that allows people to upload images to it (and receive the URL to access it). </p>
  
  <p>But I have several questions:  </p>
  
  <ul>
  <li><p>Would it be better if the user would have to send the image in binary code or would it be better if the user would have to send it in idk ASCII or so? What is the conventional way? (I'm asking that because I can imagine that some languages only have functions to read files as textfiles.)  </p></li>
  <li><p>Where do I store the images on the server and how? Can I put them in a MySQL database and would that perform well? Or should I put them all in one folder?</p></li>
  <li><p>How should the user be able to specify the file type? In the header or in the body?  </p></li>
  <li><p>How do I receive and the data save it as a file?  </p></li>
  </ul>
  
  <p>I found this code somewhere else:  </p>
  
  <pre><code>&lt;?php
  
  /* PUT data comes in on the stdin stream */
  $putdata = fopen("php://input", "r");
  
  /* Open a file for writing */
  $fp = fopen("myputfile.ext", "w");
  
  /* Read the data 1 KB at a time
     and write to the file */
  while ($data = fread($putdata, 1024))
    fwrite($fp, $data);
  
  /* Close the streams */
  fclose($fp);
  fclose($putdata);
  
  ?&gt;
  </code></pre>
  
  <p>Does this work for binary files like images?<br>
  Is it a good idea to "Read the data 1 KB at a time"?</p>