problems with php multiple image upload
<p>I try to upload multiple photos on the sever using php, the following is the code I used for upload:</p>

<pre><code> private function handle_file_upload($uploaded_file, $name, $size, $type, $error) {
    $file = new stdClass();
    // Remove path information and dots around the filename, to prevent uploading
    // into different directories or replacing hidden system files.
    // Also remove control characters and spaces (\x00..\x20) around the filename:
    $file-&gt;name = trim(basename(stripslashes($name)), ".\x00..\x20");
    $file-&gt;size = intval($size);
    $file-&gt;type = $type;
    $error = $this-&gt;has_error($uploaded_file, $file, $error);
    if (!$error &amp;&amp; $file-&gt;name) {
        $file_path = $this-&gt;options['upload_dir'].$file-&gt;name;
        $append_file = is_file($file_path) &amp;&amp; $file-&gt;size &gt; filesize($file_path);
        clearstatcache();
        if ($uploaded_file &amp;&amp; is_uploaded_file($uploaded_file)) {
            // multipart/formdata uploads (POST method uploads)
            if ($append_file) {
                file_put_contents(
                    $file_path,
                    fopen($uploaded_file, 'r'),
                    FILE_APPEND
                );
            } else {
                move_uploaded_file($uploaded_file, $file_path);
            }
        } else {
            // Non-multipart uploads (PUT method support)
            file_put_contents(
                $file_path,
                fopen('php://input', 'r'),
                $append_file ? FILE_APPEND : 0
            );
        }
        $file_size = filesize($file_path);
        if ($file_size === $file-&gt;size) {
            $file-&gt;url = $this-&gt;options['upload_url'].rawurlencode($file-&gt;name);
            foreach($this-&gt;options['image_versions'] as $version =&gt; $options) {
                if ($this-&gt;create_scaled_image($file-&gt;name, $options)) {
                    $file-&gt;{$version.'_url'} = $options['upload_url']
                        .rawurlencode($file-&gt;name);
                }
            }
        } else if ($this-&gt;options['discard_aborted_uploads']) {
            unlink($file_path);
            $file-&gt;error = 'abort';
        }
        $file-&gt;size = $file_size;
        $file-&gt;delete_url = $this-&gt;options['script_url']
            .'?file='.rawurlencode($file-&gt;name);
        $file-&gt;delete_type = 'DELETE';
    } else {
        $file-&gt;error = $error;
    }
    return $file;
}



 public function post() {
    $upload = isset($_FILES[$this-&gt;options['param_name']]) ?
        $_FILES[$this-&gt;options['param_name']] : array(
            'tmp_name' =&gt; null,
            'name' =&gt; null,
            'size' =&gt; null,
            'type' =&gt; null,
            'error' =&gt; null
        );
    $info = array();
    if (is_array($upload['tmp_name'])) {
        foreach ($upload['tmp_name'] as $index =&gt; $value) {
            $info[] = $this-&gt;handle_file_upload(
                $upload['tmp_name'][$index],
                isset($_SERVER['HTTP_X_FILE_NAME']) ?
                    $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index],
                isset($_SERVER['HTTP_X_FILE_SIZE']) ?
                    $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'][$index],
                isset($_SERVER['HTTP_X_FILE_TYPE']) ?
                    $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'][$index],
                $upload['error'][$index]
            );
        }
    } else {
        $info[] = $this-&gt;handle_file_upload(
            $upload['tmp_name'],
            isset($_SERVER['HTTP_X_FILE_NAME']) ?
                $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'],
            isset($_SERVER['HTTP_X_FILE_SIZE']) ?
                $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'],
            isset($_SERVER['HTTP_X_FILE_TYPE']) ?
                $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'],
            $upload['error']
        );
    }
    header('Vary: Accept');
    if (isset($_SERVER['HTTP_ACCEPT']) &amp;&amp;
        (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {
        header('Content-type: application/json');
    } else {
        header('Content-type: text/plain');
    }
    echo json_encode($info);
}
</code></pre>

<p>The current code works fine for uploading images, but they store the original image names into the server and if I upload the same image again, it won't allow me to store the duplicated image in the server,  and some images are in different type like png or gif, what I want to do is I want to assign a unique id to all the images one they get uploaded and save all the images into jpg files in the server, how can I acheive that, if you can illustrate based on my code will be great, thanks!</p>
