image deleting from the image source folder

how to delete image from the source folder

Page 1 of 1

1 Replies - 28765 Views - Last Post: 08 March 2009 - 07:27 PM Rate Topic: -----

#1 pretender19   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-December 08

image deleting from the image source folder

Posted 08 March 2009 - 07:07 PM

how can i delete image from the source folder...
my delete only delete the pictures from the database,, but incase i want to add that particular pic again,, a message " file with that name exist" because from my image folder,, the picture still exist...

im also thinking if automatic changing of the pic name is a good idea.. random name is given to the pic...

this is my delete code..
<?php
   session_start();
   require("includes/conn.php");
?>
<?php  
   $picname= $_POST['picname'];
   $query = "DELETE from otherpic where picname = '$picname'";
   mysql_query($query);
   echo "<center><span class=\"style7\">The selected picture have been successfully deleted.</span></center>";
?>



this is my upload code..

<?php
session_start();
// Call our connection file
require("includes/conn.php");
?>
<?php
// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
	 // This is an array that holds all the valid image MIME types
	 $valid_types = array("image/pjpeg", "image/jpeg", "image/bmp", "image/gif");

	 if (in_array($file['type'], $valid_types))
		  return 1;
	 return 0;
}

// Just a short function that prints out the contents of an array in a manner that's easy to read
// I used this function during debugging but it serves no purpose at run time for this example
function showContents($array)
{
	 echo "<pre>";
	 print_r($array);
	 echo "</pre>";
}


// Set some constants

// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH = "imagemember/";

// Get our POSTed variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mi = $_POST['mi'];
$address = $_POST['address'];
$age = $_POST['age'];
$contacts = $_POST['contacts'];
$contacts2 = $_POST['contacts2'];
$size= $_POST['MAX_FILE_SIZE'];
$image = $_FILES['image'];
$eadd = $_POST['eadd'];
$heightft = $_POST['heightft'];
$heightin = $_POST['heightin'];
$weight = $_POST['weight'];
$sel_id= $_POST['sel_id'];
$gender = $_POST['gender'];
$msmonth = $_POST['msmonth'];
$msday = $_POST['msday'];
$msyear = $_POST['msyear'];
$lockmonth= $_POST['lockmonth'];
$lockday= $_POST['lockday'];
$lockyear= $_POST['lockyear'];


//Sanitize our inputs
$fname = mysql_real_escape_string($fname);
$lname = mysql_real_escape_string($lname);
$image['name'] = mysql_real_escape_string($image['name']);
$mi = mysql_real_escape_string($mi);
$address = mysql_real_escape_string($address);
$age = mysql_real_escape_string($age);
$eadd = mysql_real_escape_string($eadd);
$heightft = mysql_real_escape_string($heightft);
$heightin = mysql_real_escape_string($heightin);
$weight = mysql_real_escape_string($weight);
$sel_id = mysql_real_escape_string($sel_id);
$gender = mysql_real_escape_string($gender);
$contacts = mysql_real_escape_string($contacts);
$contacts2 = mysql_real_escape_string($contacts2);
$msmonth = mysql_real_escape_string($msmonth);
$msday = mysql_real_escape_string($msday);
$msyear = mysql_real_escape_string($msyear);
$lockmonth = mysql_real_escape_string($lockmonth);
$lockday = mysql_real_escape_string($lockday);
$lockyear = mysql_real_escape_string($lockyear);


// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH .= $image['name'];

$sql = "select fname,lname from talents where fname = '$fname' and lname = '$lname'";
$result = mysql_query($sql);
 if (mysql_num_rows($result) > 0) 
{
$_SESSION['error'] = "Talent name exist.";
	 header("Location: adminaddtalent.php");
	 exit;
}
// Make sure all the fields from the form have inputs
if ( $fname == "" || $lname == ""  || $image['name'] == "" || $contacts == "" || $mi == "" || $address == "" || 
$eadd == "" || $heightin == "" || $heightft == "" || $weight == "" || $sel_id == "" )
{
	 $_SESSION['error'] = "All fields are required. Please fill up all fields.";
	 header("Location: adminaddtalent.php");
	 exit;
}


if(ereg("[^[:alpha:][:space:]]", $fname)) 
{
$_SESSION['error'] = "Firstname must contain letters only.";
	 header("Location: adminaddtalent.php");
	 exit;
}

if(ereg("[^[:alpha:][:space:]]", $lname)) 
{
$_SESSION['error'] = "Last name must contain letters only.";
	 header("Location: adminaddtalent.php");
	 exit;
}

if(ereg("[^[:alpha:]]", $mi)) 
{
$_SESSION['error'] = "M.I. contains letter only.";
	 header("Location: adminaddtalent.php");
	 exit;
}

if(ereg("[^[:digit:]]", $age)) 
{
$_SESSION['error'] = "Age must be a number";
	 header("Location: adminaddtalent.php");
	 exit;
}

if (ereg("[^[:digit:]]",$contacts))
{
$_SESSION['error'] = "Contacts must be a number";
	 header("Location: adminaddtalent.php");
	 exit;
}
if(ereg("[^[:digit:]]", $heightft)) 
{
$_SESSION['error'] = "Height(ft) must be a number";
	 header("Location: adminaddtalent.php");
	 exit;
}

if(ereg("[^[:digit:]]", $heightin)) 
{
$_SESSION['error'] = "Heeight(in) must be a number";
	 header("Location: adminaddtalent.php");
	 exit;
}

if(ereg("[^[:digit:]]", $weight)) 
{
$_SESSION['error'] = "Weight must be a number";
	 header("Location: adminaddtalent.php");
	 exit;
}
// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
{
	 $_SESSION['error'] = "You must upload a jpg, gif, or bmp";
	 header("Location: adminaddtalent.php");
	 exit;
}
//check the image file
if ($size < $image['size'])
{
	 $_SESSION['error'] = "The image size exceeds the maximum required";
	 header("Location: adminaddtalent.php");
	 exit;
}
// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
	 $_SESSION['error'] = "A file with that name already exists";
	 header("Location: adminaddtalent.php");
	 exit;
}

 
	   if ($gender == "select") 
	   {
	   	 $_SESSION['error'] = "Select gender";
		  header("Location: adminaddtalent.php");
		  exit;
	  }
	  
/*if ( $_POST['cbskills'] == "" ) 
{
	$_SESSION['error'] = "Check at least one skill in the checkbox below.";
	header("Location: adminaddtalent.php");
		  exit;
	  }
	  */
	  if ($_POST['Class'] == "") 
{
	$_SESSION['error'] = "Choose the talent's class.";
	header("Location: adminaddtalent.php");
		  exit;
	  }
	  
			if ($_POST['talenttype'] == "") 
{
	$_SESSION['error'] = "Choose the talent's type..";
	header("Location: adminaddtalent.php");
		  exit;
	  }
	  
	   if ($_POST['talenttype'] == "Exclusive" && $msmonth == "N/A") 
	   {
	   	 $_SESSION['error'] = "select membersince and lockout date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  }
		   if ($_POST['talenttype'] == "Exclusive" && $msday == "N/A") 
	   {
	   	 $_SESSION['error'] = "select membersince and lockout date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  }
		   if ($_POST['talenttype'] == "Exclusive" && $msyear == "N/A") 
	   {
	   	 $_SESSION['error'] = "select membersince and lockout date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  }
	  
	  if ($_POST['talenttype'] == "Exclusive" && $lockmonth == "N/A") 
	   {
	   	 $_SESSION['error'] = "select membersince and lockout date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  }
		   if ($_POST['talenttype'] == "Exclusive" && $lockday == "N/A") 
	   {
	   	 $_SESSION['error'] = "select membersince and lockout date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  }
		   if ($_POST['talenttype'] == "Exclusive" && $lockyear == "N/A") 
	   {
	   	 $_SESSION['error'] = "select membersince and lockout date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  }
	   if ($_POST['talenttype'] == "Freelance" && $msmonth != "N/A") 
	   {
	   	 $_SESSION['error'] = "Freelance cannot have a membersince and lockout date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  } if ($_POST['talenttype'] == "Freelance" && $msday != "N/A") 
	   {
	   	 $_SESSION['error'] = "Freelance cannot have a membersince and lockout date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  } if ($_POST['talenttype'] == "Freelance" && $msyear!= "N/A") 
	   {
	   	 $_SESSION['error'] = "Freelance cannot have a membersince and lockout date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  }
			 if ($_POST['talenttype'] == "Freelance" && $lockmonth != "N/A") 
	   {
	   	 $_SESSION['error'] = "Freelance cannot have a membersince and lockout date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  } if ($_POST['talenttype'] == "Freelance" && $lockday != "N/A") 
	   {
	   	 $_SESSION['error'] = "Freelance cannot have a membersince and lockout date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  } if ($_POST['talenttype'] == "Freelance" && $lockyear!= "N/A") 
	   {
	   	 $_SESSION['error'] = "Freelance cannot have a membersince and lockout date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  }
	  if ($lockyear < $msyear)
	  {
	   $_SESSION['error'] = "Yout lockout date is invalid. It must not be lower than the membersince date.";
		  header("Location: adminaddtalent.php");
		  exit;
	  }
	  
// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
	 // NOTE: This is where a lot of people make mistakes.
	 // We are *not* putting the image into the database; we are putting a reference to the file's location on the server
	 $sql = "insert into talents (
	 				class, 
					lname, 
					fname, 
					mi , 
					gender , 
					age , 
					height_ft, 
					height_in, 
					weight, 
					eadd, 
					address, 
					contacts,
					contacts2, 
					talent_type, 
					agent_ID, 
					talentpic,
					msmonth,
					msday,
					msyear,
					lockmonth,
					lockday,
					lockyear) 
			values (
				'$_POST[Class]',
				'$lname', 
				'$fname',
				'$mi',
				'$gender',
				'$age', 
				'$heightft',
				'$heightin',
				'$weight', 
				'$eadd', 
				'$address',
				'$contacts',
				'$contacts2',
				'$_POST[talenttype]',
				'$sel_id',
				'" . $image['name'] . "',
				'$msmonth', 
				'$msday',
				'$msyear',
				'$lockmonth' , 
				'$lockday' , 
				'$lockyear')";
				
	 $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
	 $talentid =mysql_insert_id();
 
foreach($_POST['cbskills'] as $value){

$query = "INSERT INTO talents_skills (ID,skillsname) VALUES ('$talentid','$value')";
mysql_query($query) or die ("Could not insert data into DB: " . mysql_error());
}

 	$_SESSION['error'] = "Successfully added talent.";
	header("Location: adminaddtalent.php");
	exit;
}

else
{
	 // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
	 // Make sure you chmod the directory to be writeable
	 $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
	 header("Location: adminaddtalent.php");
	 exit;
}
?>
?>



Is This A Good Question/Topic? 0
  • +

Replies To: image deleting from the image source folder

#2 Valek   User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: image deleting from the image source folder

Posted 08 March 2009 - 07:27 PM

<?php
   session_start();
   require("includes/conn.php");
 
   $picname= mysql_real_escape_string($_POST['picname']);
   $query = "DELETE from otherpic where picname = '$picname'";
   mysql_query($query) or die(mysql_error());
   unlink($picname);
   echo "<center><span class=\"style7\">The selected picture have been successfully deleted.</span></center>";
?>


This will only work if the delete script is running from the same directory as the pictures, or if $picname contains the path to the image from the directory the script is in. If the pictures are in another directory, you'll have to make sure the path is included so that it is correct from the path the script is in to the image being removed.

I've also made a couple of security and error checking changes to your code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1