Issues of Parse Email and Save Attachments into directory
<p>I am using this script(<a href="http://stuporglue.org/mailreader-php-parse-e-mail-and-save-attachments-php-version-2/" rel="nofollow">http://stuporglue.org/mailreader-php-parse-e-mail-and-save-attachments-php-version-2/</a>) to save email attachment on my server. You can also view the complete script on browser here: <a href="http://stuporglue.org/downloads/mailReader.txt" rel="nofollow">http://stuporglue.org/downloads/mailReader.txt</a></p>

<p>Everything works fine but there are 2 problems here. </p>

<p>1) The file name of the image that i saved into the directory is not an image: 1360341823_test_jpg </p>

<pre><code>How to convert the file name from 1360341823_test_jpg to 1360341823_test.jpg 
in the script?
</code></pre>

<p>2) The permission of the file that saved in the directory is 600. </p>

<pre><code>How to make it default 755 or 775?
</code></pre>

<p>I believe this is the function to convert the image in the script.:</p>

<pre><code>function saveFile($filename,$contents,$mimeType){
global $save_directory,$saved_files,$debug;
$filename = preg_replace('/[^a-zA-Z0-9_-]/','_',$filename);

$unlocked_and_unique = FALSE;
  while(!$unlocked_and_unique){
    // Find unique
    $name = time()."_".$filename;
      while(file_exists($save_directory.$name)) {
        $name = time()."_".$filename;
      }

    // Attempt to lock
    $outfile = fopen($save_directory.$name,'w');
      if(flock($outfile,LOCK_EX)){
        $unlocked_and_unique = TRUE;
      } else {
        flock($outfile,LOCK_UN);
        fclose($outfile);
      }
  }

fwrite($outfile,$contents);
fclose($outfile);

  // This is for readability for the return e-mail and in the DB
  $saved_files[$name] = Array(
    'size' =&gt; formatBytes(filesize($save_directory.$name)), 
    'mime' =&gt; $mimeType
  );
}    
</code></pre>

<p>Any help?</p>
