<?php  

      
//Check to determine if the home button has been pressed  
    if(isset($_POST['home'])){  
      header('Location: index.html');
    }
    else
    {

  //Define some variables  
      $dir = dirname(__FILE__)."/uploads/"; 
      $types = array("image/jpg","image/jpeg");  
      
//Check to determine if the submit button has been pressed  
    if(isset($_POST['submit'])){  

//Shorten Variables  
     $tmp_name = $_FILES['upload']['tmp_name'];  
     
 $firstnum = 0;
 $lastbit = sprintf("%ld", $firstnum);
 $realbit = $lastbit . "a.jpg";
 $lastbit .= ".jpg";
 
 while (file_exists($dir.$lastbit) || file_exists($dir.$realbit))
 {
  $firstnum += 1;
  $lastbit = sprintf("%ld", $firstnum);
  $realbit = $lastbit . "a.jpg";
  $lastbit .= ".jpg";
 }
 
 $sra = $_SERVER["REMOTE_ADDR"];
 //echo "sra=".$sra;
 if ($sra == '' || $sra == '//' || $sra == '::' || str_replace('.','',$sra) == $sra)
 {
   $sra = $_SERVER['HTTP_CLIENT_IP'];
   //echo "xsra=".$sra;
 }
 
 $firstnum = 0;
 $iprealbit = '0_'.$sra.".jpg";
 
 while (file_exists($dir.$iprealbit))
 {
  $firstnum += 1;
  $iprealbit = sprintf("%ld_", $firstnum);
  $iprealbit .= $sra;
  $iprealbit .= ".jpg";
 }

 $upfile = $dir.$lastbit;
     
     $new_name = $realbit;   
     
     //$new_name = $_FILES['upload']['name'];  

//Check MIME Type  
    if (in_array($_FILES['upload']['type'], $types) && file_exists($dir) && filesize($tmp_name) <= floor($_POST['MAX_FILE_SIZE'])){  
                     
         //Move file from tmp dir to new location  

        move_uploaded_file($tmp_name,$dir . $iprealbit);  
        copy($dir . $iprealbit,$dir . $new_name);  
            
        echo "{$_FILES['upload']['name']} was uploaded successfully!";  
        $toaddr = "rmetcalfe@rjmprogramming.com.au";
        $subj = "Ephemeral photo upload via ".$sra." called "."http://localhost:8888/ephemeral/uploads/".$iprealbit;
        $cont = "http://localhost:8888/ephemeral/uploads/".$realbit;
        $hdrs = "From: rmetcalfe@rjmprogramming.com.au"."\r\n";
        mail($toaddr, $subj, $cont, $hdrs);
            
    }else{  
                 
    //Print Error Message  

     echo "<small>File <strong><em>{$_FILES['upload']['name']}</em></strong> Was Not Uploaded!</small><br />";  
       
    //Debug  
   $name =  $_FILES['upload']['name'];  
   $type =    $_FILES['upload']['type'];  
   $size =    $_FILES['upload']['size'];  
   $tmp =     $_FILES['upload']['name'];  
      
   echo "Name: $name<br/ >Type: $type<br />Size: $size<br />Tmp: $tmp";  
               
    }  
        
    }        
        
else{        
        
    echo 'Could Not Upload Files';  
        
}  
}
        ?>  
  <html>
<head>
<title>Ephemeral - Upload File - RJM Programming - http://www.rjmprogramming.com.au</title>
</head>
<body style="background-color: #F2F2F2;">
          
    <form style="background-color: #E4E4E4;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">  
        
      <fieldset>  
           <legend>Upload "Ephemeral - Upload File - RJM Programming" jpg photo file(s) (<= 1,000,000 bytes) - Everybody Welcome!</legend>  
           <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
           <input style="background-color: yellow;" size="100" type="file" name="upload" />  
      </fieldset>  
      <input style="color: green;" type="submit" name="submit" value="Upload Files" />  
      <input style="color: red;" type="submit" name="home" value="Reload Home Page..." />  
      <!--a target="_blank" href="http://rjmprogramming.com.au/ephemeral">Back to Home Page for Reload ...</a-->
</form>
</body>
</html>
