title: Php image upload : Changing image names
body: <p>I have an image upload form that uploads images to the database.</p>
  
  <p>Here is the code I use, it generates a random value for each image, thanks to the trick below.</p>
  
  <pre><code>$name = "cover".rand(1,1000000).".jpg"; // generates random image name
  </code></pre>
  
  <p>But what if the uploaded file isn't a .jpg? It can be a png, gif or any other image file. How can I dynamically add the file extension to the end of the image?</p>
  
  <p>I also want to use a stronger randomizer function, what kind of function should I use to give each image a unique value?</p>
  
  <p>Form</p>
  
  <pre><code>&lt;form action="upload.php" method="post" enctype="multipart/form-data" name="uploadform"&gt;
      &lt;input type="hidden" name="MAX_FILE_SIZE" value="350000"&gt;
      &lt;input name="picture" type="file" id="picture" size="50"&gt;
      &lt;input name="upload" type="submit" id="upload" value="Upload Picture!"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>upload.php</p>
  
  <pre><code>&lt;?php
  // if something was posted, start the process... 
  if (isset($_POST['upload'])) {
  
  // define the posted file into variables 
      $name = $_FILES['picture']['name'];
      $tmp_name = $_FILES['picture']['tmp_name'];
      $type = $_FILES['picture']['type'];
      $size = $_FILES['picture']['size'];
  
  // if your server has magic quotes turned off, add slashes manually 
      if (!get_magic_quotes_gpc()) {
          $name = addslashes($name);
      }
  
  // open up the file and extract the data/content from it 
      $extract = fopen($tmp_name, 'r');
      $content = fread($extract, $size);
      $content = addslashes($content);
      fclose($extract);
  
  // connect to the database 
      include "../cn/connect.php";
  
     $name = "cover".rand(1,1000000).".jpg"; // generates random image name
  
  // the query that will add this to the database 
      $stmt = $sqli-&gt;prepare("INSERT INTO image1(name, type, size) VALUES (?,?,?)");
      $stmt-&gt;bind_param("sss", $name, $_FILES['picture']['type'], $_FILES['picture']['size']);
      $stmt-&gt;execute();
      $stmt-&gt;close();
      echo "Mission has been completed, sir!";
  
      if (!empty($_FILES)) {
          $target = "upload/";
          $target = $target . basename($_FILES['picture']['name']);
          $ok = 1;
          $picture_size = $_FILES['picture']['size'];
          $picture_type = $_FILES['picture']['type'];
          //This is our size condition 
          if ($picture_size &gt; 5000000) {
              echo "Your file is too large.&lt;br&gt;";
              $ok = 0;
          }
  
  
          //This is our limit file type condition 
          if ($picture_type == "text/php") {
              echo "No PHP files&lt;br&gt;";
              $ok = 0;
          }
  
          //Here we check that $ok was not set to 0 by an error 
          if ($ok == 0) {
              Echo "Sorry your file was not uploaded";
          } //If everything is ok we try to upload it
          else {
              if (move_uploaded_file($_FILES['picture']['tmp_name'], $target)) {
                  echo "The file " . basename($_FILES['picture']['name']) . " has been uploaded &lt;br/&gt;";
              } else {
                  echo "Sorry, there was a problem uploading your file.";
              }
          }
      }
  
      echo "Successfully uploaded your picture!";
  } else {
      die("No uploaded file present");
  }
  
  ?&gt;
  </code></pre>