Update/Delete images

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
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Update/Delete images

Post by ninethousandfeet »

hello,
users upload pics with items they post on my site. if they want to update their post info but not the pic, or if they want to want to update the pic but not the post info, then how do i make it work? right now, my update page creates a new directory for the file and then it is not displayed with my img tag. i've read some info on unlink() and rmlink()... but i have no idea how to integrate the functions with my code. any help would be great, thank you for taking the time to read this.

Code: Select all

 
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
 
// error array
$error = array();
// Remove whitespace and check post and product
  $_POST['post_title'] = trim($_POST['post_title']);
  $_POST['product_name'] = trim($_POST['product_name']);
  if (empty($_POST['post_title']) || empty($_POST['product_name'])) {
      $error['productinfo'] = 'Please double check that you have entered a posting title, product, and/or store name.';
  }
  // Check the post title
  $_POST['post_title'] = trim($_POST['post_title']);
  if (strlen($_POST['post_title']) > 70) {
      $error['length'] = 'Posting title cannot exceed 70 characters.';
  }
  // Check product name for length
  $_POST['product_name'] = trim($_POST['product_name']);
  if (strlen($_POST['product_name']) > 150) {
      $error['length'] = 'Product and/OR Store name cannot exceed 150 characters.';
  }
// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 51200);
// define constant for upload folder
$uploadDIR = '/home/me/domains/mysite.com/public_html/upload';
// assign simpler variable
$file = str_replace(' ', '_', $_FILES["image_data"]["name"]);
$fileTemp = $_FILES['image_data']['tmp_name'];
 
// convert the maximum size to KB
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
 
$fileOK = false;
if (!empty($_FILES['image_type']) && !empty($file)) {
if (($_FILES["image_data"]["type"] == "image/gif")
  || ($_FILES["image_data"]["type"] == "image/jpeg")
  || ($_FILES["image_data"]["type"] == "image/pjpeg")
  || ($_FILES["image_data"]["type"] == "image/png" )
  && ($_FILES["image_data"]["size"] < MAX_FILE_SIZE)) {
    $fileOK = true;
} else {
    $error['sizetype'] = 'File must be in either gif, jpeg, pjpeg, or png AND less than 6.2MB.';
}
}
if (!$error) {
$username = $_SESSION['MM_Username'];
ini_set('date.timezone','America/Los Angeles');
$now = date('Y-m-d-His');
if (!is_dir("$uploadDIR/$username")) {
mkdir("$uploadDIR/$username", 0777, true);
}
  $updateSQL = sprintf("UPDATE postingTable SET post_title=%s, product_name=%s, user_id=%s, buy_or_share=%s, category_name=%s, image_data=%s, image_type=%s WHERE post_id=%s",
                       GetSQLValueString($_POST['post_title'], "text"),
                       GetSQLValueString($_POST['product_name'], "text"),
                       GetSQLValueString($_POST['user_id'], "int"),
                       GetSQLValueString($_POST['buy_or_share'], "text"),
                       GetSQLValueString($_POST['category_name'], "text"),
                       GetSQLValueString($_POST['image_data'], "text"),
                       GetSQLValueString($_POST['image_type'], "text"),
                       GetSQLValueString($_POST['post_id'], "int"));
 
  mysql_select_db($database_connUser, $connUser);
  $Result1 = mysql_query($updateSQL, $connUser) or die(mysql_error());
 
  $updateGoTo = "userprofile.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  if (!file_exists("$uploadDIR/$username/$file")) {
  move_uploaded_file($fileTemp, "$uploadDIR/$username/$file");
  } else {
      mkdir("$uploadDIR/$username/$now");
      move_uploaded_file($fileTemp, "$uploadDIR/$username/$now/$file");
  }
  header(sprintf("Location: %s", $updateGoTo));
}
}
?>
 
... and then my image tag looks like:

Code: Select all

 
      <td><?php if ($row_getPost['image_data'] != NULL) { // Show if picture is NOT empty ?>
  <img src = "http://www.mysite.com/upload/<?php echo $row_getPost['username']?>/<?php echo $row_getPost['image_data']?>" />
  <?php } // Show if pic not empty ?>&nbsp;</td>
 
Post Reply