Renaming the uploaded file using ImageMagick
<p>In my current project I am using the plupload plugin with PHP and ImageMagick and its working fine - now my next task is too rename uploaded files in lowercase.</p>

<p>To rename these files I first store the uploaded image in a temporary folder with a ".temp" extension, than I move the file to a main folder and there I attempt to do a rename. The first time the function is executed, if there are no matching image then it will store it perfectly, but the second time through it doesn't work as well. The function should check for the existing file name and should store repeats with an incrementing index, i.e. as image_1.jpg, image_2.jpg, etc, but istead of doing that it just overwrites the image i.e. image.jpg.</p>

<p>Here is a snippet of my code:</p>

<pre><code>  $filename = $this-&gt;input-&gt;post('filename');
  $root  = $_SERVER["DOCUMENT_ROOT"]."/uploads/";
  $source_image  = $root.'temp/'.$filename;

  $image_name = explode(".",$filename);
  rename($source_image, $source_image. '.temp');
  copy($source_image.'.temp', $root.$filename.'.temp');
  unlink($source_image.'.temp');

  $exp_temp_img_slash = explode(".",$filename);
  $rename_img_a = strtolower($exp_temp_img_slash[0]);
  $rename_img_b = $exp_temp_img_slash[1];

  if (file_exists($root.$rename_img_a .".".$rename_img_b)) {
      $count=1;

      while (file_exists($root. $rename_img_a ."_".$count. ".".$rename_img_b)) {
          $count++;
           $rename_img = strtolower($rename_img_a . '_' . $count . ".".$rename_img_b);
      }

      $renamed_image = $_SERVER["DOCUMENT_ROOT"]."/uploads/".$rename_img;
      rename($root.$filename.".temp",$renamed_image);
   } else {
       $rename_img = strtolower($rename_img_a .".".$rename_img_b);
       $renamed_image = $_SERVER["DOCUMENT_ROOT"]."/uploads/".$rename_img;
       rename($root.$filename.".temp",$renamed_image);
   }
</code></pre>

<p>Any help will be appreciated, thanks.</p>
