Link to display uploaded image(s)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
batowiise
Forum Commoner
Posts: 28
Joined: Fri Dec 17, 2010 7:11 am

Link to display uploaded image(s)

Post by batowiise »

Hi all,

Need help on displaying image that I have uploaded. The user uploads scan images into a specific user session folder.

The user submits some requests to an approver and the approver vets the details along side with a link display the uploaded images.

So far I have been able to upload the image file but my biggest problem is providing the link to display the uploaded images.

Your help is welcome.

The upload code is as follows;

Code: Select all

<?php 
session_start();
 
//require_once("../admin/db.php");

// define a constant for the maximum upload size
define('MAX_FILE_SIZE', 51200);
if (array_key_exists('upload', $_POST)){
// define constant for upload folder
define('UPLOAD_DIR', 'C:/upload_test/');
// replace any space in original filename with underscores
// at the same time, assign to a simpler variable
$file = str_replace(' ', '_', $_FILES['image']['name']);
// convert the maximum size to KB
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
// create an array of permitted MIME types
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png','application/pdf');
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;

// check that file is within the permitted size
if($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE){
	$sizeOK = true;
	}

// check that the file is of a permitted MIME type
foreach ($permitted as $type) {
if($type == $_FILES['image']['type']){
	$typeOK = true;
	break;
	}
}

if ($sizeOK && $typeOK){
	switch($_FILES['image']['error']){
	case 0:
		// $username would normally come from a session variable
		$username = $_SESSION['username'];;
		// if the subfolder does not exist yet, create it
		if (!is_dir(UPLOAD_DIR.$username)){
			mkdir(UPLOAD_DIR.$username);
		}
		// make sure file of same name does not already exist
		if (!file_exists(UPLOAD_DIR.$username.'/'.$file)){
		// move the file to the upload folder and rename it
		$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$file);
		}
		else {
		// get the date and time
		ini_set('date.timezone', 'Europe/London');
		$now = date('Y-m-d-His');
		$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
		}
		if($success){
		$result = "$file uploaded successfully";
		}
		else {
		$result = "Error uploading $file. Please try again.";
		}
		break;
		case 3:
			$result = "Error uploading $file. Please try again.";
		default:
			$result = "System error uploading $file. Contact webmaster.";
		}
	}
	elseif ($_FILES['image']['error'] == 4) {
		$result = 'No file selected';
		}
	else {
		$result = "$file cannot be uploaded. Maximum size: $max. Acceptable file type: gif, jpg, png, pdf.";
		}
}
?>
Regards.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Link to display uploaded image(s)

Post by social_experiment »

This example is from the php manual, modified to suit your purpose (or the purpose as i understand it)

Code: Select all

<?php
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
           echo '<img src="'. $file .'" width="100" height="100" alt="'. $file .'" />';
        }
    }
    closedir($handle);
}
?>
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply