Location: PHPKode > projects > cms.libe.net > admin/supa/upload.php
<?php @include("../passwort.php"); if ($_COOKIE["loginadmin"] != md5($Passwort)){die(); }?>
<?php
  /*
   * I am placing this code in the Public Domain. Do with it as you will.
   * This software comes with no guarantees or warranties but with
   * plenty of well-wishing instead!
   */
  
  // This piece of code's sole purpose is providing a small demo app for SUPA.
  // Please do not use this as a reference for your implementation 
  // as good coding style was left out for simplicitys sake!

	// the result of this script should be text/plain,
	// consisting of either "OK:" <file-url>
	//                   or "ERROR:" <errormessage>
	//
	// "client side" debuggin is easy: just print your stuff. The demo
	// application treats everything not starting with OK as an error and will
	// display it via javascript:alert()

	// the uploaded image will be put into the subdir "data" of this script
	define( 'FILESTORE_PATH', "../../image" );
	define( 'FILESTORE_URLPREFIX', "data" ); 

	// As the request is started via ajax, the session context would be available
	session_start(); 

	// this script returns plain error messages or the URL to the uploaded image
	header('Content-Type: text/plain');

	//print_r( $_FILES );
	//echo "ERROR: ".FILESTORE_PATH;
	//echo "ERROR: ".FILESTORE_URLPREFIX;
	//die();

	// see if there's a file at all
	if( ! $_FILES['screenshot'] ) {
		echo "ERROR: NO FILE (screenshot)";
		exit;
	}

  if( $_FILES['screenshot']['error'] ) {
    echo "PHP upload error: ".$_FILES['screenshot']['error'];
    exit;
  }

	// generate filename
  $filename = "default_filename.jpg";
  if( $_FILES['screenshot']['name'] ) {
    $filename = $_FILES['screenshot']['name'];
  }

  // demonstrate the automatic form posting by appending
  // otherparam to the filename
	if( isset($_POST['otherparam']) ) {
		$filename = $_POST['otherparam']."-".$filename;
	}

	// full path to the uploaded file
	$file= FILESTORE_PATH."/".$filename;

	// Files are uploaded base64 encoded.
	// so: base64 decode the uploaded file and copy to destination
	$fh = fopen( $_FILES['screenshot']['tmp_name'], "r" );
	if( !$fh ) {
		echo "ERROR: could not read temporary file";
		//die();
	}
	$data = fread( $fh, filesize( $_FILES['screenshot']['tmp_name'] ) );
	fclose( $fh );

	//TODO: optimize for memory usage?
	$fh = fopen( $file, "w" );
	if( !$fh ) {
		echo "ERROR: could not open destination file";
		die();
	}
	fwrite( $fh, base64_decode( $data ) );
	fclose( $fh );

	// remove temporary uploaded file
	if( is_uploaded_file( $_FILES['screenshot']['tmp_name'] ) ) {
		unlink( $_FILES['screenshot']['tmp_name'] );
	}

	// all done
	echo "OK:".FILESTORE_URLPREFIX."/".$filename;

	$tag=str_replace("-screenshot.png", "", $filename);
	$tag=str_replace("../", "", $tag);
	
	$imgname= "../../image/" .$tag. "-1";$imgname2="-1";
	for( $xf = 2; $xf < 999; $xf++ )
	{
		if(file_exists($imgname .".jpg") or file_exists($imgname .".png") or file_exists($imgname .".gif")) 
		{
		$imgname= "../../image/" .$tag. "-". $xf;$imgname2="-". $xf;
		}else break;
	}$imgname2="$imgname2.jpg";

	$tag=str_replace("-screenshot.png", $imgname2, $file);

	png2jpg($file,$tag,100);
	function png2jpg($originalFile, $outputFile, $quality) {
	$image = imagecreatefrompng($originalFile);
	imagejpeg($image, $outputFile, $quality);
	imagedestroy($image);

//resize
							include('../SimpleImage.php');		
							$image = new SimpleImage();
							$image->load($outputFile);

								$image_info = getimagesize($outputFile);
								$myimageinfo=explode("height=", $image_info[3]);$endung="jpg";if ($image_info[2]=="3") $endung="png";if ($image_info[2]=="1") $endung="gif";						
								$x=str_replace("\"", "", str_replace("width=\"", "", trim($myimageinfo[0])));
								$y=str_replace("\"", "", str_replace("height=\"", "", trim($myimageinfo[1])));

							$tag=str_replace(".jpg", "", $outputFile);
									$image->load($outputFile);
									if ($x > 799 or $y > 799) {if ($x>$y) $image->resizeToWidth(800);else $image->resizeToHeight(800);}
								      	$image->save($tag. "-800.jpg");
									if ($x > $y) $image->resizeToWidth(150);else $image->resizeToHeight(150);
								      	$image->save($tag. "-150.jpg");
									$image->load($outputFile);					
									if ($x > 500 or $y > 500) {if ($x>$y) $image->resizeToWidth(500);else $image->resizeToHeight(500);}
									$image->save($tag. ".jpg");
														

//resize
			
					unlink($file);
}

?>

Return current item: cms.libe.net