JerieLsky 0 Junior Poster in Training

Hi, I'm still learning on how to upload and retrieve/display images. I have already have the idea on uploading an image onto the server. Well, I still have a problem when it comes to displaying the image which has been stored from the file server. I am actually using this method to display the image:

<?php
	if(!isset($_SESSION)){
		session_start();
	}
	$fileName=$_SESSION['uploadedImage'];
	$fileType=$_GET['type'];
	define("url", $_SERVER['DOCUMENT_ROOT']."practice/image upload test/htdocs/");
/*	if($fileType=="image/jpeg"){
		$image=imagecreatefromjpeg(url.$fileName);
		header('Content-Type: image/jpeg');
		imagejpeg($image);
	}else if($fileType=="image/gif"){
		$image=imagecreatefromgif(url.$filename);
		header('Content-Type: image/gif');
		imagegif($image);
	}*/
	$image=imagecreatefromjpeg(url.$fileName);
	header('Content-Type: image/jpeg');
	imagejpeg($image);
?>

So, i'm using the function imagecreatefromjpeg() which creates an new image from a certain file(.jpg, .gif, etc.) and also the function imagejpeg() which outputs the image to browser or file.

Is there any method wherein I can display a certain image aside from the above code? Since the file that i'm trying to display is an image file.

Also if I remove the commented lines and make the last three lines a comment from the given code, the image isn't displayed.

Oh, here is the other .php file which is to be executed first:

<?php
	if(!isset($_SESSION)){
		session_start();
	}
	
	define("REP", $_SERVER['DOCUMENT_ROOT']."practice/image upload test/htdocs");
	if(isset($_POST['submit'])){
		if(is_uploaded_file($_FILES['fileupload']['tmp_name'])){
			echo "temperory file name: " . $_FILES['fileupload']['tmp_name'] . "<br />";
			echo "file name: " . $_FILES['fileupload']['name'] . "<br />";
			echo "file size: " . $_FILES['fileupload']['size'] . "<br />";
			echo "file type: " . $_FILES['fileupload']['type'] . "<br />";
			$fileName=$_FILES['fileupload']['name'];
			$tmpFileName=$_FILES['fileupload']['tmp_name'];		
			$fileType=$_FILES['fileupload']['type'];
			$size=getimagesize($_FILES['fileupload']['tmp_name']);
			$imgData=addslashes(file_get_contents($_FILES['fileupload']['tmp_name']));			
			
			/*getimagesize function returns an array where array index 0=width 1=height  */
			//check file type
			/*
				$data=base64_encode(addslashes(fopen($tmpFileName, "r", filesize($tmpFileName))));
				echo "Data " . $data;
				print "Strip " . stripslashes(base64_decode($data));
			*/	
			if($fileType == "image/jpeg" || $fileType == "image/gif"){
				if(move_uploaded_file($tmpFileName, REP . "/$fileName")){
					$_SESSION['uploadedImage']=$fileName;
					if(!isset($_GET['type'])){
						$_GET['type']=$_FILES['fileupload']['type'];
					}
					echo "file successfully uploaded.<br />";
					echo "<img src=displayImage.php width=20% />";
				}else{
					echo "something went wrong.";
				}
			}else{
				echo "not a valid file type";
			}
		}else{
			$err_Number = $_FILES['fileupload']['error'];
			switch($err_Number){
				case 1: echo "<b>UPLOAD_ERR_INI_SIZE: Err Num $err_Number </b>- There was an attempt to upload a file whose size exceeds the value specified by the <i>upload_max_filesize</i> directive.";
					break;
				case 2: echo "<b>UPLOAD_ERR_FORM_SIZE: Err Num $err_Number </b>- There was an attempt to upload a file whose size exceeds the value of the <i>max_file_size</i> directive, which can be embedded into the HTML form.";
					break;
				case 3: echo "<b>UPLOAD_ERR_PARTIAL: Err Num $err_Number </b>- A file is not completely  uploaded. ";
					break;
				case 4: echo "<b>UPLOAD_ERR_NO_FILE: Err Num $err_Number </b>- No file specified.";
					break;
			}
		}
	}else{
?>
<html>
<head>
	<title>File Upload</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
	File: <input type="file" name="fileupload" />
	<br /><input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
	}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.