<?php

$url = '../ckupload/'.$_FILES['upload']['name'];
 $err=0;
 //extensive suitability check before doing anything with the file…
    if (($_FILES['upload'] == "none") || (empty($_FILES['upload']['name'])) )
    {
       $err=1;
	   $message = "No file uploaded.";
    }
    else if ($_FILES['upload']["size"] == 0)
    {
       $err=1;
	   $message = "The file is of zero length.";
    }
    else if (($_FILES['upload']["type"] != "image/pjpeg") && ($_FILES['upload']["type"] != "image/jpeg") && ($_FILES['upload']["type"] != "image/png"))
    {
       $err=1;
	   $message = "The image must be in either JPG or PNG format. Please upload a JPG or PNG instead.";
    }
    else if (!is_uploaded_file($_FILES['upload']["tmp_name"]))
    {
       $err=1;
	   $message = "You may be attempting to hack our server. We’re on to you; expect a knock on the door sometime soon.";
    }
    else {
      $message = "";
      $move = @move_uploaded_file($_FILES['upload']['tmp_name'], $url);
      if(!$move)
      {
         $err=1;
		 $message = "Error moving uploaded file. Check the script is granted Read/Write/Modify permissions.";
      }
	  else
	  {
	  	$err=0;
        $url = $url;
	  }
    }
$funcNum = $_GET['CKEditorFuncNum'] ;
if(!empty( $err))
{
echo "<script type='text/javascript'>alert('$message')</script>";
}
else
{
echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($funcNum, '$url', '$message');</script>";
}
?>
