/** removeFile()
    ** Remove a remote file in upload dir(if we have perms)
    ** @returns 1 if file was removed, 0 if an error occurred
    **/
    function removeFile( $file )
    {
    //check if file exists
if (file_exists($this->cls_upload_dir."/".$file))
{
if( @unlink($this->cls_upload_dir."/".$file) )
{
return(1);
}
else
{
return(0);
}
}
else
{
return (0);
}
    }
    /** setCompleteFilename()
    ** Complete path where we want to upload the file.
    ** @returns void
    **/
    function setCompleteFilename()
    {
      $this->cls_copyfile = $this->cls_upload_dir ."/". $this->cls_filename;
    }   /** checkExtension()
    ** Function to make sure the extension of the file is accepted
    ** @returns boolean
    **/
   function checkExtension()
   {
// correct filename just in case
$this->cls_filename = ereg_replace(" ", "_", $this->cls_filename );
$this->cls_filename = ereg_replace("%20", "_", $this->cls_filename ); // Check if the extension is valid
return( in_array( $this->get_extension( $this->cls_filename ), $this->cls_arr_ext_accepted ));
}   /** checkFileSize()
** Function to make sure the uploaded file is not too big
** @returns boolean
**/
function checkFileSize()
{
return( !($this->cls_filesize > $this->cls_max_filesize) );
}
/** filenameExist()
** Function to check if a file with the same filename
** exist in the directory we upload to.
** @returns Boolean
*/
function filenameExist()
{
return( file_exists( $this->cls_copyfile ) );
} /** xCopy()
** Funtion to copy the file.
** @returns 1 if copy was succesfull, 0 if an error occurred.
** Also sets error flag( look show_progressMessage() method below)
*/
   function xCopy($uploaded_name="")
{ global $HTTP_REFERER; $url=parse_url( $HTTP_REFERER );
$this_domain = $url["scheme"]."://".$url["host"]; //get domain script is called from if ( $this->cls_file!="none")
{
if( !$this->checkExtension() )
{
$this->cls_copyCode = 1; // extension not accepted
}
else
{
$this->setCompleteFilename(); if( $this->filenameExist( $this->cls_copyfile ) )
{
if ( $this->cls_overWrite ) //check overwrite flag
{
if (!unlink( $this->cls_copyfile ))
{
$this->cls_copyCode = 3; // can't delete remote file
}
else //continue the copy process
{
$this->cls_copyCode = $this->resumeCopy($this_domain,$uploaded_name);
}
}
else
{
$this->cls_copyCode = 4; // file exists and OverWrite not set
}
}
else
{
$this->cls_copyCode = $this->resumeCopy($this_domain,$uploaded_name);
}
} }
else
{
if (!empty($this->cls_filename) )
$this->cls_copyCode = 2;
else
$this->cls_copyCode = 8;
}
return (!$this->cls_copyCode);
}
/** resumeCopy()
** Private funtion to resume the copy process.
** DO NOT CALL THIS FUNCTION, IS AUTOMATICALLY CALLED IN xCopy() method
** @returns 0 if copy was succesfull, int code if an error occurred.
** Also sets error flag( look show_progressMessage() method below)
*/
function resumeCopy($this_domain,$uploaded_name)
{
if ($cls_domain_check)
{
// make sure calling from this domain
if( $this_domain != $this->cls_referer_domain )
{
return 7; // external script!
}
} // try to copy the file
if( copy( $this->cls_file, $this->cls_copyfile ))
{
// rename uploaded file if needed
if( $this->cls_rename_file )
{ //check if user give a new name or wants a random name
$temp_name = ($uploaded_name)? $this->changeFilename($uploaded_name) : $this->changeFilename(); if( !rename( $this->cls_copyfile, $temp_name ))
{
return 5; // copy sucess, but renaming file failed
}
else
{
              $this->cls_copyfile = $temp_name; // just in case we need final complete path
}
           } return 0; // copy success         }
else
{
return 6; // copy failed!
} } /** changeFilename()
* Function to change the filename before copying the file in the new
* directory. You can provide a name or get a random one
*
* @returns string new filename
*/
function changeFilename( $new_name="" )
{
if ( ! $new_name )
{
// Remove the extension
$extension = $this->get_extension( $this->cls_copyfile ); // Get a random name for the file, 10 chars long.
$new_name = strtolower( rndName( 10 )); // put back extension
$new_name .= $extension;
$new_name = strtolower( $new_name ); } // update the class var
$this->cls_filename = $new_name; // rebuild complete path name
return ( $this->cls_upload_dir ."/$new_name" ); } /* ---------------------- */
/* Some utilily functions */
/* ---------------------- */ /** get_extension()
* Return everything after the . of the file name (including the .)
* @ Returns String
*/
function get_extension( $filename )
{
return strrchr( $filename, "." );
   } function get_filesize(){
return ($this->cls_filesize);
}
   ## get_filename()
   ## get the uploaded filename, maybe to insert it into database
   ## returns the filename string
   function get_filename ()
   {
   return ( $this->cls_filename );
   }   ## get_progressMessage()
   ## catch the copy code and returns the message for each file upload progress
   ## you can edit your error messages
   ## returns nothing
   function show_progressMessage ()
   {
switch( $this->cls_copyCode )
   { case 0: $msg = "The file <b>".$this->get_filename()."</b> was succesfully uploaded.\n";
break;
case 1: $msg = "<b>".$this->get_filename()."</b> was not uploaded. <b>".$this->get_extension($this->get_filename())."</b> extension is not accepted!\n";
break;
case 2: $msg = "The file <b>$this->cls_filename</b> is too big or does not exists!";
break;
case 3: $msg = "Remote file could not be deleted!\n";
     break;
case 4: $msg = "The file <b>".$this->cls_filename."</b> exists and overwrite is not set in class!\n";
break;
case 5: $msg = "Copy successful, but renaming the file failed!\n";
break;
case 6: $msg = "Unable to copy file :(\n";
break;
case 7: $msg = "You don't have permission to use this script!\n";
break;
case 8: $msg = ""; // if user does not select a file
break;
default:$msg = "Unknown error!\n";   }
   if ($msg)
   //echo $msg."<br>" ;
Return $msg."\\n";
   }
function get_copycode() {
Return $this->cls_copyCode;
}
}; // class ends
function rndName( $name_len = 10 )
{
$allchar = "ABCDEFGHIJKLNMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890_" ;
$str = "" ;
mt_srand (( double) microtime() * 1000000 );
for ( $i = 0; $i<$name_len ; $i++ )
$str .= substr( $allchar, mt_rand (0,25), 1 ) ;
return $str ;
}<?phpinclude('./XUploadForm.inc.php');// Create the upload form
$mf = new XUploadForm('./upload.php');
?>