Jump to content

[SOLVED] help with image duplicate in PHP


Recommended Posts



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 database)


My script generate something like :  e17fc9a207.jpg

in my picture folder when I hit the form upload button.


What I need now is a script that will take the picture e17fc9a207.jpg, resize it 100x100, move it to a thumbnail folder and rename it to te same name but with a _t at the end ( i.e: e17fc9a207_t.jpg )



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 "<br>File $fileName uploaded<br>";
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><br></td></tr>
<tr align="left" valign="middle" height="10"><td><b><font size="2" face="Georgia, Arial" color="black">Description de la 

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 :</b></font></td><td>
<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><br>
<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>
  <br><img src="images/transparent.gif" width="363" height="10" border=0><input name="upload" type="submit" class="box" 

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





Link to post
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.