Log in

View Full Version : Link to display uploaded image(s)



batowiise
01-11-2011, 02:34 PM
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;



<?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.

bluewalrus
01-11-2011, 03:48 PM
Is C:/upload_test/ web accessible?

cindylou
01-11-2011, 03:49 PM
hi there..

you're problem is getting the link of the uploaded images right?...have you tried trapping it in a way that when the images are uploaded, you assign a variable to hold the url so that when neede, you can call the variable..instead of calling the whole link...

batowiise
01-11-2011, 03:56 PM
Is C:/upload_test/ web accessible?
Yes its accessible

batowiise
01-11-2011, 03:57 PM
hi there..

you're problem is getting the link of the uploaded images right?...have you tried trapping it in a way that when the images are uploaded, you assign a variable to hold the url so that when neede, you can call the variable..instead of calling the whole link...

I have not done any, Can you help me out?

cindylou
01-11-2011, 04:13 PM
i can't actually give you the exact lines of the script if i don't code it myself... but maybe this site might help you..

http://php.net/manual/en/language.variables.php

:)

Sarutobi sensei
01-11-2011, 04:26 PM
Hey guys i need anwers or question for final year student of SS3 in Nigeria ..if you get any leads please reply to this thread.:)

bluewalrus
01-11-2011, 05:35 PM
No idea what you're talking about Sarutobi sensei.

Schmoopy
01-11-2011, 06:04 PM
The easiest way to get around this problem is to save the files in somewhere more accessible. For example:



define('UPLOAD_DIR', '/your_upload_dir'/);


This way you can actually access the file in a link like so:



<a href="/your_upload_dir/filename.jpg">Link to file</a>


If you really do want to keep using the absolute path of C:/..., you could make a file that handles the opening of uploaded files, but it gets a bit more complex.

Easiest thing to do is move the upload directory.