user652792
user652792

Reputation:

Renaming of uploaded image not working

I'm trying to change the file name at the point of uploading. This image name is what I want to be inserted into the database 4f9e9118b753cIMAGE.jpeg, but only IMAGE.jpeg (the original image name) is still retained in the file system. Below is the portion of code I am working with:

if (isset($_FILES['userupload']['name'])) {
   $uploaddir = "uploads/";

     switch($_FILES['userupload']['type']) {
                case "image/gif":
                case "image/jpeg":
                case "image/jpg":
                case "image/png":
          $uploaddir .= 'pictures/';
          break;
     }         

     foreach($_FILES as $userupload){   
          $t = uniqid();
          $type = $userupload['type'];
          $name = $userupload['name'];
          $size = $userupload['size']; 
          $image_name = $t."$name";


          if(move_uploaded_file($_FILES['userupload']['tmp_name'], $uploaddir . $file)) {
              $query = "INSERT INTO users ( image_name ) VALUES ( '$image_name' )";  
              mysql_query($query) or die (mysql_error());

Upvotes: 0

Views: 109

Answers (1)

Oras
Oras

Reputation: 1096

You need to insert this line

move_uploaded_file($_FILES["userupload"]["tmp_name"],$uploaddir.$image_name);

before the query

Upvotes: 1

Related Questions