Update image into 3 folder using dreamweaver

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
neodev
Forum Newbie
Posts: 3
Joined: Wed Jan 28, 2015 4:19 am

Update image into 3 folder using dreamweaver

Post by neodev »

I have build a small application in dreamweaver.It consist in an add page that load some text field ad an image into a database (mysql).The image field can load an image and it upload that image into 3 folder:large, small and micro.After, I've created an "edit.php" page that edit the text field and let update the image into the 3 folder .All work nice, but I can't delete the old image into the 3 folder after the update operation. Please, could someone help me with the php code to "unlink" the old images into the three folder? Thanks.

Edit.php page

Code: Select all


<?php require_once('Connections/prodotti.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
   case "text":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
   break;   
   case "long":
   case "int":
  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
   break;
   case "double":
  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
   break;
   case "date":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
   break;
   case "defined":
  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
   break;
  }
  return $theValue;
}
}





$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}







if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {






  $updateSQL = sprintf("UPDATE articoli SET marca=%s, nome=%s, colore=%s, price=%s, descrizione=%s, small=%s, `large`=%s WHERE id=%s",
   GetSQLValueString($_POST['marca'], "text"),
   GetSQLValueString($_POST['nome'], "text"),
   GetSQLValueString($_POST['colore'], "text"),
   GetSQLValueString($_POST['price'], "double"),
   GetSQLValueString($_POST['descrizione'], "text"),
   GetSQLValueString("small/".$_FILES["img_upload"]["name"],"text"),
   GetSQLValueString($_POST['large'], "text"),
   GetSQLValueString($_POST['id'], "int"));





  mysql_select_db($database_prodotti, $prodotti);
  $Result1 = mysql_query($updateSQL, $prodotti) or die(mysql_error());

  $updateGoTo = "list.php";
  if (isset($_SERVER['QUERY_STRING'])) {
  $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
  $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

$colname_rs_update = "-1";
if (isset($_GET['id'])) {
  $colname_rs_update = $_GET['id'];
}
mysql_select_db($database_prodotti, $prodotti);
$query_rs_update = sprintf("SELECT * FROM articoli WHERE id = %s", GetSQLValueString($colname_rs_update, "int"));
$rs_update = mysql_query($query_rs_update, $prodotti) or die(mysql_error());
$row_rs_update = mysql_fetch_assoc($rs_update);
$totalRows_rs_update = mysql_num_rows($rs_update);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
include_once("script/edit.php");
original();
large();
small();
micro();
?>


<form action="<?php echo $editFormAction; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table align="center">
   <tr valign="baseline">
   <td nowrap="nowrap" align="right">Id:</td>
   <td><?php echo $row_rs_update['id']; ?></td>
   </tr>
   <tr valign="baseline">
   <td nowrap="nowrap" align="right">Marca:</td>
   <td><input type="text" name="marca" value="<?php echo htmlentities($row_rs_update['marca'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
   </tr>
   <tr valign="baseline">
   <td nowrap="nowrap" align="right">Nome:</td>
   <td><input type="text" name="nome" value="<?php echo htmlentities($row_rs_update['nome'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
   </tr>
   <tr valign="baseline">
   <td nowrap="nowrap" align="right">Colore:</td>
   <td><input type="text" name="colore" value="<?php echo htmlentities($row_rs_update['colore'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
   </tr>
   <tr valign="baseline">
   <td nowrap="nowrap" align="right">Price:</td>
   <td><input type="text" name="price" value="<?php echo htmlentities($row_rs_update['price'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
   </tr>
   <tr valign="baseline">
   <td nowrap="nowrap" align="right">Descrizione:</td>
   <td><input type="text" name="descrizione" value="<?php echo htmlentities($row_rs_update['descrizione'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
   </tr>
   <tr valign="baseline">
   <td nowrap="nowrap" align="right"> </td>
   <td> </td>
   </tr>
   <tr valign="baseline">
   <td nowrap="nowrap" align="right"> </td>
   <td> </td>
   </tr>
   <tr valign="baseline">
   <td nowrap="nowrap" align="right">Immagine attuale:</td>
   <td><img src="<?php echo $row_rs_update['small']; ?>" /></td>
   </tr>
   <tr valign="baseline">
   <td nowrap="nowrap" align="right"> </td>
   <td> </td>
   </tr>
   <tr valign="baseline">
   <td nowrap="nowrap" align="right">Cambia immagine:</td>
   <td><label for="img_upload"></label>
   <input type="file" name="img_upload" id="img_upload" /></td>
   </tr>
   <tr valign="baseline">
   <td nowrap="nowrap" align="right"> </td>
   <td> </td>
   </tr>
   <tr valign="baseline">
   <td nowrap="nowrap" align="right"> </td>
   <td><input type="submit" value="Update record" /></td>
   </tr>
  </table>
  <input type="hidden" name="MM_update" value="form1" />
  <input type="hidden" name="id" value="<?php echo $row_rs_update['id']; ?>" />
</form>
<p> </p>
</body>
</html>
<?php
mysql_free_result($rs_update);
?>






And this is the php script that resize the image and insert it into 3 folder:

Code: Select all


<?php


////////////////// ORIGINAL IMAGE SIZE STORED INTO "ORIGINAL" FOLDER ///////////////////////////

error_reporting(0);
function original(){



if($_FILES["img_upload"]["name"]!=""){
$message = "You must waiting the upload proccess";
if(!copy($_FILES["img_upload"]["tmp_name"],"original/".$_FILES["img_upload"]["name"])){
$message = "Can not upload the Image!!!";
}else{
$message = "The Image was uploaded successfuly...";
echo "<img src='"."original/".$_FILES["img_upload"]["name"]."'/>";
}
}else{
$message = "You must select an Image File!!!";
}
if($message != ""){
echo "<div style='background:yellow;color:red;font-size:20px;padding:12px;text-align:center;'>".$message."</div>";
}
}



////////////////// LARGE IMAGE SIZE (600PX WIDE) STORED INTO "LARGE" FOLDER ///////////////////////////

function large(){




if($_FILES["img_upload"]["name"]!=""){


$message = "You must waiting the upload proccess";
$img_file = $_FILES["img_upload"]["tmp_name"];
list($width_ori,$height_ori) = getimagesize($img_file);
$folder = "large/";
if(!is_dir($folder)){
mkdir($folder);
}
$width_new = 300;
$ratio_ori = $width_ori / $height_ori;
$height_new = $width_new / $ratio_ori;
$img_one = imagecreatefromjpeg($img_file);
$img_two = imagecreatetruecolor($width_new,$height_new);
imagecopyresampled($img_two,$img_one,0,0,0,0,$width_new,$height_new,$width_ori,$height_ori);



if(imagejpeg($img_two,$folder."/".$_FILES["img_upload"]["name"],100)){
$message = "The File was Uploaded!!!";
echo "<img src='"."large/".$_FILES["img_upload"]["name"]."'/>";
}



}else{
$message = "You must select an Image File!!!";
}
if($message != ""){
echo "<div style='background:yellow;color:red;font-size:20px;padding:12px;text-align:center;'>".$message."</div>";
}
}



//////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////// SMALL IMAGE SIZE STORED INTO "SMALL" FOLDER ///////////////////////////

function small(){



if($_FILES["img_upload"]["name"]!=""){


$message = "You must waiting the upload proccess";
$img_file = $_FILES["img_upload"]["tmp_name"];
list($width_ori,$height_ori) = getimagesize($img_file);
$folder = "small/";
if(!is_dir($folder)){
mkdir($folder);
}
$width_new = 100;
$ratio_ori = $width_ori / $height_ori;
$height_new = $width_new / $ratio_ori;
$img_one = imagecreatefromjpeg($img_file);
$img_two = imagecreatetruecolor($width_new,$height_new);
imagecopyresampled($img_two,$img_one,0,0,0,0,$width_new,$height_new,$width_ori,$height_ori);



if(imagejpeg($img_two,$folder."/".$_FILES["img_upload"]["name"],100)){
$message = "The File was Uploaded!!!";
echo "<img src='"."small/".$_FILES["img_upload"]["name"]."'/>";
}


}else{
$message = "You must select an Image File!!!";
}
if($message != ""){
echo "<div style='background:yellow;color:red;font-size:20px;padding:5px;text-align:center;'>".$message."</div>";
}
}



//////////////////////////////////////////////////////////////////////////////////////////////////////





////////////////// MICRO IMAGE SIZE STORED INTO "MICRO" FOLDER ///////////////////////////

function micro(){



if($_FILES["img_upload"]["name"]!=""){





$message = "You must waiting the upload proccess";
$img_file = $_FILES["img_upload"]["tmp_name"];
list($width_ori,$height_ori) = getimagesize($img_file);
$folder = "micro/";
if(!is_dir($folder)){
mkdir($folder);
}
$width_new = 50;
$height_new = 50;
$ratio_width = $width_new / $width_ori;
$ratio_height = $height_new / $height_ori;
if($width_ori > $height_ori){
$width_crop = $width_ori * $ratio_height;
$height_crop = $height_new;
}else if($width_ori < $height_ori){
$width_crop = $width_new;
$height_crop = $height_ori * $ratio_width;
}else{
$width_crop = $width_new;
$height_crop = $height_new;
}
$img_one = imagecreatefromjpeg($img_file);
$img_two = imagecreatetruecolor($width_new,$height_new);
imagecopyresampled($img_two,$img_one,-(($width_crop / 2) - ($width_new / 2)),-(($height_crop / 2) - ($height_new / 2)),0,0,$width_crop,$height_crop,$width_ori,$height_ori);
if(imagejpeg($img_two,$folder."/".$_FILES["img_upload"]["name"],100)){
$message = "The File was Uploaded!!!";
echo "<img src='"."micro/".$_FILES["img_upload"]["name"]."'/>";
}




}else{
$message = "You must select an Image File!!!";
}
if($message != ""){
echo "<div style='background:yellow;color:red;font-size:20px;padding:12px;text-align:center;'>".$message."</div>";
}
}
?>



Thanks in advance.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Update image into 3 folder using dreamweaver

Post by Celauran »

Code: Select all

error_reporting(0);
Stop doing that. Turn error reporting on and actually see where it's failing!
neodev
Forum Newbie
Posts: 3
Joined: Wed Jan 28, 2015 4:19 am

Re: Update image into 3 folder using dreamweaver

Post by neodev »

I apologize. Well, no error is displayed. But the image rest into the folders.
After some search on the web, I know that I need to "unlink" the image, so dreamweaver
will replace with the new one.
Could you help me for the code?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Update image into 3 folder using dreamweaver

Post by Celauran »

I don't see unlink() being called anywhere. You've got the images resized and moved into three directories, using the same, original name. On edit, you can simply pull the name out of the database, check that the file_exists() and then call unlink()
neodev
Forum Newbie
Posts: 3
Joined: Wed Jan 28, 2015 4:19 am

Re: Update image into 3 folder using dreamweaver

Post by neodev »

Thanks for answer, but I'm not expert on php coding, so I try to find on web what you wrote.
It give me error. Thanks anyway.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Update image into 3 folder using dreamweaver

Post by Celauran »

Well, what have you written so far and what errors are you encountering?
Post Reply