<?php
session_start();
?>

<html>
<head><title>Upload</title></head>
<link rel="stylesheet" type="text/css" href="../style.css">
<body>

<?php
require("fileupload-class.php");
$path = "uploads/";

// The name of the file field in your form.

$upload_file_name = "userfile";

// Accept ONLY gifs's
#$acceptable_file_types = "image/gifs";
	
// Accept GIF and JPEG files
$acceptable_file_types = "image/gif|image/jpeg|image/pjpeg";
	
// Accept ALL files
#$acceptable_file_types = "";

// If no extension is supplied, and the browser or PHP
// can not figure out what type of file it is, you can
// add a default extension - like ".jpg" or ".txt"

$default_extension = "";

// MODE: if your are attempting to upload
// a file with the same name as another file in the
// $path directory
//
// OPTIONS:
//   1 = overwrite mode
//   2 = create new with incremental extention
//   3 = do nothing if exists, highest protection
$mode = 2;
	
	
#--------------------------------#
# PHP
#--------------------------------#
if (isset($_REQUEST['submitted'])) 
{
	// Create a new instance of the class
	$my_uploader = new uploader('en');
		
	// OPTIONAL: set the max filesize of uploadable files in bytes
	$my_uploader->max_filesize(100000);
		
	// OPTIONAL: if you're uploading images, you can set the max pixel dimensions 
	$my_uploader->max_image_size(800, 800); // max_image_size($width, $height)
		
	// UPLOAD the file
	if ($my_uploader->upload($upload_file_name, $acceptable_file_types, $default_extension)) 
	{
		$my_uploader->save_file($path, $mode);
	}
		
	if ($my_uploader->error) 
	{
		echo $my_uploader->error . "<br><br>\n";
	} 
	else 
	{
		// Successful upload!
		print($my_uploader->file['name'] . " was successfully uploaded! <a href=\"" . $_SERVER['PHP_SELF'] . "\">upload another file</a><br>");
			
		// Print all the array details...
		//print_r($my_uploader->file);
			
		// ...or print the file
		if(stristr($my_uploader->file['type'], "image")) 
		{
			echo "copy the link of the picture here&nbsp;";
			$imgurl = "/gates2/news/";
			$imgurl .= "fileupload/";
			$imgurl .= $path . $my_uploader->file['name'];
				
			//echo $imgurl;
			echo "<input type=text size=80 name=pic value=\"$imgurl\"><br>";
			//echo $my_uploader->file['name'];
			echo "<img src=\"" . $path . $my_uploader->file['name'] . "\" border=\"0\" alt=\"\">";
		} 
		else 
		{
			$fp = fopen($path . $my_uploader->file['name'], "r");
			while(!feof($fp)) 
			{
				$line = fgets($fp, 255);
				echo $line;
			}
			if ($fp) 
			{ 
				fclose($fp); 
			}
		}
	}
}

#--------------------------------#
# HTML FORM
#--------------------------------#
?>
<form enctype="multipart/form-data" action="<?= $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="submitted" value="true">
		
Upload this file:<br>
<input name="<?= $upload_file_name; ?>" type="file">&nbsp;<input type="submit" value="Upload image">
</form>

<hr>

<?php
if (isset($acceptable_file_types) && trim($acceptable_file_types)) 
{
	print("This form only accepts <b>" . str_replace("|", " or ", $acceptable_file_types) . "</b> files\n");
}
?>
</body>
</html>
