I'm having a little struggle getting a PHP file to upload 2 files
simultaneously.
The code follows. The first image always gets uploaded but the second one
sometimes does but when it does it is a duplicate of the first image but
named as the second.

There's probably a real simple solution but obviously I'm not seeing it.

Here's the code:

if (($userfile=="") OR ($userfile=="none")) {
$userfile="";
}
// $Description=nl2br($Description);
if ($userfile!="") { // can only check all aspects of the upload it if it is
used
        // prep the image
        $newname = $userfile_name;
        $Image=$userfile_name;  // the name that will be saved to the database
        // get the size of the image
        $TheImage=$userfile;
        $imagehw = GetImageSize($TheImage);
        $ImageWidth = $imagehw[0];
        $ImageHeight = $imagehw[1];

function do_upload($filename,$newname) {
  $file = basename($filename);
  $tmp_upload_path = "/tmp/";
  $new_file_name = "/home/httpd/vhtdocs/ramco/uploaded/".$newname;

        if (!copy($tmp_upload_path.$file, $new_file_name)) echo "failed to copy
file<br>Possibly due to it being larger than 30k (30,000 bytes).\n";
        return;
        }

        if ($CurrentImage!="") { // means we need to delete the existing file if it
exists
                if(file_exists("/home/httpd/vhtdocs/ramco/uploaded/".$CurrentImage)) {
                $filename="/home/httpd/vhtdocs/ramco/uploaded/" .$CurrentImage;
                unlink($filename);
                } // file deleted if it exists
        } // end looking for existing file if $CurrentImage!=""

        // extract file extension of uploaded file
        $arr_basename=explode(".",$newname);
        $file_type=$arr_basename[1];
        if ($file_type!="jpg") {
        $Error="NotJPG";
        }

        // allow upload only for specific types

        elseif ($ImageWidth>400) {
        $Error="TooBig";
        }
        else {
        do_upload($userfile,$newname);
        }

        if ($userfile_size>$MAX_UPLOAD_SIZE) {
                $filename="/home/httpd/vhtdocs/ramco/uploaded/" .$userfile_name;
                if (unlink($filename)) {
                $Error="FileSizeTooBig";
                }
        }
} // if first upload was successful, do the next image

function do_upload2($filename2,$newname2) {
  $file2 = basename($filename2);
  $tmp_upload_path2 = "/tmp/";
  $new_file_name2 = "/home/httpd/vhtdocs/ramco/uploaded/".$newname2;

        if (!copy($tmp_upload_path2.$file2,$new_file_name2)) echo "failed to copy
file<br>Possibly due to it being larger than 30k (30,000 bytes).\n";
        return;
        }

// now do userfile2
if (($userfile2=="") OR ($userfile2=="none")) {
$userfile2="";
}

if ($userfile2!="") { // can only check all aspects of the upload it if it
is used
        // prep the image
        $newname2 = $userfile2_name;
        $Image2=$userfile2_name;        // the name that will be saved to the database
        // get the size of the image
        $TheImage2=$userfile2;
        $imagehw2 = GetImageSize($TheImage2);
        $ImageWidth2 = $imagehw2[0];
        $ImageHeight2 = $imagehw2[1];

        if ($CurrentImage2!="") { // means we need to delete the existing file if
it exists
                if(file_exists("/home/httpd/vhtdocs/ramco/uploaded/".$CurrentImage2)) {
                $filename2="/home/httpd/vhtdocs/ramco/uploaded/" .$CurrentImage2;
                unlink($filename2);
                } // file deleted if it exists
        } // end looking for existing file if $CurrentImage!=""

        // extract file extension of uploaded file
        $arr_basename=explode(".",$newname2);
        $file_type=$arr_basename[1];
        if ($file_type!="jpg") {
        $Error2="NotJPG";
        }

        // allow upload only for specific types

        elseif ($ImageWidth>400) {
        $Error2="TooBig";
        }
        else {
        do_upload2($userfile,$newname);
        }

        if ($userfile_size>$MAX_UPLOAD_SIZE) {
                $filename2="/home/httpd/vhtdocs/ramco/uploaded/" .$userfile_name2;
                if (unlink($filename2)) {
                $Error2="FileSizeTooBig";
                }
        }
} // if the uploads were successful, save the entry into the database



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to