View unanswered posts | View active topics It is currently Mon Jun 25, 2018 2:59 am

Reply to topic  [ 1 post ] 
 Automatic Thumbnail code 
Author Message
Junior Member

Joined: Thu Nov 12, 2009 6:04 pm
Posts: 6
Post Automatic Thumbnail code

I have a php form where I can upload 3 things into a database.

1. Image Description
2. Image category
3. Image (the image is uploaded in a folder with a unique random name, and the unique path name is inserted into the


My script generate something like : 3a54b42fa7103570ea1729e17fc9a207.jpg
in my picture folder when I hit the form upload button.

What I need now is a thumbnail version of that image created in the same folder but with a different file name and in

smaller size , something like :

Anyone can help me? I kind of new with php,mysql.

My php code is:


// image directory
$uploadDir = ( $_SERVER['DOCUMENT_ROOT'].'/vitrail_path/photos/' );

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$description = $_POST['description'];
$categorie = $_POST['categorie'];

// get the file extension first
$ext = substr(strrchr($fileName, "."), 1);

// generate the random file name
$randName = md5(rand() * time());

// unique file name for the upload file
$filePath = $uploadDir . $randName . '.' . $ext;

// move the files to the specified directory with error reporting
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";

if (file_exists(( $_SERVER['DOCUMENT_ROOT'].'/vitrail_path/photos/' ) . $_FILES['userfile']['tmp_name']))
echo $_FILES['userfile']['tmp_name'] . ' already exists. ';

if(!$_POST['description'] | !$_POST['categorie']) {
die('Inscrire une description et une catégorie.');

$fileName = addslashes($fileName);
include 'library/config.php';
include 'library/opendb.php';

$query = "INSERT INTO upload2 (name, size, type, path, description, categorie) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$description', '$categorie')";

mysql_query($query) or die('Error, query failed');

include 'library/closedb.php';

echo "
File $fileName uploaded
header('Location: upload.php');


And my form code is
<form method="post" enctype="multipart/form-data" name="uploadform">
<table border="0" align = "center">
<tr align="left" valign="top"><td>
<tr align="left" valign="middle" height="10"><td><b><font size="2" face="Georgia, Arial" color="black">Description de


photo :</font></b></td><td height="10">
<input type="text" name="description" maxlength="75" size="75">
<tr align="left" valign="middle"><td><font size="2" face="Georgia, Arial" color="black"><b>Catégorie

<select name="categorie">
<option selected>Animaux</option>
<option>Bord de mer</option>
<option>Décorations des fêtes</option>
<tr align="left" valign="middle">
<td> </td>
<td> </td>
<tr align="left" valign="middle">
<td><font size="2" face="Georgia, Arial" color="black"><b>Sélection de la photo :</b></font>

<td><input type="hidden" name="MAX_FILE_SIZE" value="60000000">
<input name="userfile" type="file" id="userfile" size="25"></td>
<tr align="left" valign="middle"><td></td>

<img src="images/transparent.gif" width="363" height="10" border=0><input name="upload" type="submit"


id="upload" value=" Ajouter la photo "></td></tr>



Thu Nov 12, 2009 6:08 pm
Report this post
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
© Copyright 2003-2008 The ultimate PHP Editor and PHP IDE site.