title: Using PHP to upload images to a folder while saving descriptions to a database
body: <p>The user decides to upload multiple images for their classified listing. What I need to have happen is to:</p>
  <ol>
  <li>Upload the images with descriptions for each of the images - SOLVED</li>
  <li>Save the images to a folder specified</li>
  <li>Save the image location to my MySQL database with descriptions - SOLVED</li>
  <li>Be able to call the images and descriptions in their classified listing in kind of a gallery setting</li>
  </ol>
  <p>My table schema is set up like this (simplified):</p>
  <pre><code>ad_id | member_id | category | subcategory | ... | photo_0_href | photo_0_desc ... | etc.
  </code></pre>
  <p>Could someone walk me through the process? Thanks.</p>
  <h3>step3.php</h3>
  <pre><code>&lt;form action=&quot;upload.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
  &lt;p&gt;
  &lt;label for=&quot;file0&quot;&gt;Filename: &lt;/label&gt;
  &lt;input name=&quot;file[]&quot; type=&quot;file&quot; id=&quot;file0&quot; size=&quot;20&quot; /&gt;
  &lt;/p&gt;
  &lt;p&gt;
  &lt;label for=&quot;file0desc&quot;&gt;Description: &lt;/label&gt;
  &lt;textarea rows=&quot;10&quot; cols=&quot;30&quot; id=&quot;file0desc&quot; class=&quot;textarea&quot;&gt;&lt;/textarea&gt;
  &lt;/p&gt;
  &lt;p&gt;
  &lt;label for=&quot;file1&quot;&gt;Filename: &lt;/label&gt;
  &lt;input name=&quot;file[]&quot; type=&quot;file&quot; id=&quot;file1&quot; size=&quot;20&quot; /&gt;
  &lt;/p&gt;
  &lt;p&gt;
  &lt;label for=&quot;file1desc&quot;&gt;Description: &lt;/label&gt;
  &lt;textarea rows=&quot;10&quot; cols=&quot;30&quot; id=&quot;file1desc&quot; class=&quot;textarea&quot;&gt;&lt;/textarea&gt;
  &lt;/p&gt;
  &lt;p&gt;
  &lt;input id=&quot;submit&quot; type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Continue to Step 4&quot; /&gt;
  &lt;/p&gt;
  &lt;/form&gt;
  </code></pre>
  <h3>upload.php</h3>
  <pre><code>&lt;?php
  
  if ((($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/gif&quot;)
  || ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/jpeg&quot;)
  || ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/pjpeg&quot;)
  || ($_FILES[&quot;file&quot;][&quot;type&quot;] == &quot;image/png&quot;))
  &amp;&amp; ($_FILES[&quot;file&quot;][&quot;size&quot;] &lt; 1048600)) // less than 1MB
    {
    if ($_FILES[&quot;file&quot;][&quot;error&quot;] &gt; 0)
      {
      echo &quot;Return Code: &quot; . $_FILES[&quot;file&quot;][&quot;error&quot;] . &quot;&lt;br /&gt;&quot;;
      }
    else
      {
      header(&quot;Location: step4.php&quot;);
  /*
      echo &quot;Upload: &quot; . $_FILES[&quot;file&quot;][&quot;name&quot;] . &quot;&lt;br /&gt;&quot;;
      echo &quot;Type: &quot; . $_FILES[&quot;file&quot;][&quot;type&quot;] . &quot;&lt;br /&gt;&quot;;
      echo &quot;Size: &quot; . ($_FILES[&quot;file&quot;][&quot;size&quot;] / 1024) . &quot; Kb&lt;br /&gt;&quot;;
      echo &quot;Temp file: &quot; . $_FILES[&quot;file&quot;][&quot;tmp_name&quot;] . &quot;&lt;br /&gt;&quot;;
  
  */
      if (file_exists(&quot;upload/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;]))
        {
        echo $_FILES[&quot;file&quot;][&quot;name&quot;] . &quot; already exists. &quot;;
        }
      else
        {
        move_uploaded_file($_FILES[&quot;file&quot;][&quot;tmp_name&quot;],
        &quot;upload/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;]);
        echo &quot;Stored in: &quot; . &quot;upload/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;];
        }
      }
    }
  else
    {
    echo &quot;Invalid file&quot;;
    }
  ?&gt;
  </code></pre>
  <p>I know my upload.php isn't configured for multiple image files yet, but it's a start.</p>
  <p>I worked out saving the descriptions to the database using <code>$_SESSION['file_n_desc']</code>. I just need to figure out how to upload the darn images to the folder and then save the locations into the database.</p>
  <p>I also need to have the image renamed to a random string (to prevent images being overwritten). I know I can do this with the <code>rand()</code> function.</p>