Jump to content

delete old updated images with their alternate


NONAME_2

Recommended Posts

Hi all, i want delete old updated images with considering its index in array.

<?php
global $oldimg;
$oldimg = array();
//action: edit news
if (isset($_GET['id'])) {
$NewsID = (int)$_GET['id'];
if ($NewsID == 0) {
	$rdir = '<META HTTP-EQUIV="Refresh" CONTENT="1.4;URL=panel-news.php">';
	die($rdir);}

//get user from the database and put data into $_POST variables.
//Include database connection details
require_once('../config.php');	
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	 die('Failed to connect to server: ' . mysql_error());
			}
//Select database
 $db = mysql_select_db(DB_DATABASE);
 if(!$db) {
		die("Unable to select database");
  		  }
$rs = mysql_query("SELECT  newsimg1, newsimg2, ".
    " newsimg3 FROM news WHERE id = $NewsID");
if (mysql_num_rows($rs) == 0) die('no such a  newsID!');
$row = mysql_fetch_assoc($rs);
$oldimg[0] = $row['newsimg1'];
$oldimg[1] = $row['newsimg2'];
$oldimg[2] = $row['newsimg3'];
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"
             enctype="multipart/form-data">
                <input type=file name="file[]" size=20 
                accept="image/jpg,image/jpeg,image/png">
                <input type=file name="file[]" size=20 
                accept="image/jpg,image/jpeg,image/png">
                <input type=file name="file[]" size=20 
                accept="image/jpg,image/jpeg,image/png">
                <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
                <input type="hidden" name="NewsID" 
                value='<?php echo (isset($NewsID))?$NewsID:"0";?>'>
			<input type="submit" value="edit" id="save"  name="save"/>
		</form>
            <?php
		if (isset($_POST['save']) && isset($_POST['NewsID'])){
			$NewsID = (int)$_POST['NewsID'];
		  //Include database connection details
		  require_once('../config.php');
		  //Connect to mysql server
		  $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
		  if(!$link) {
			   die('Failed to connect to server: ' . mysql_error());
					  }
		  //Select database
		  $db = mysql_select_db(DB_DATABASE);
		  if(!$db) {
			  die("Unable to select database");
					}
		  //max fle size value
		  $max_file_size = 2097152;
		  //Global newsimg
		  global $newsimg;global $ctr;
		  $ctr = 0;
		  //timestamp to make files unique names
		  $timestamp = time();
		  //destination folder path
		  $destpath = "../Dir/Newsimg/";
		  //looping each file or image from the form
		  while(list($key,$value) = @each($_FILES["file"]["name"])) {
		  //check if any empty or errors
		  if(!empty($value)){
		  if ($_FILES["file"]["error"][$key] > 0) {
		  $edir ='<div id="fail" class="info_div">';
		  $edir .='<span class="ico_cancel"><strong>';
		  $edir .="err : ".  $_FILES["file"]["error"][$key]  .'</strong></span></div>';
		  echo($edir);
		  } else {
		  //add the timestamp to filename
		  $file_name = $timestamp.$_FILES['file']['name'];
		  //temp name from upload form,  key of each is set
		  $source = $_FILES["file"]["tmp_name"][$key] ;
		  //getting the file type
		  $file_type = $_FILES["file"]["type"][$key];
		  //placing each file name into a variable
		  $filename1 = $_FILES["file"]["name"][$key];
		  //lowering the file names
		  $filename = strtolower($filename1);
		  //adding timestamp to front of file name
		  $filename = "$timestamp$filename";
		  ++$timestamp;
		  if ($file_type != "image/jpeg" && $file_type != "image/png" && $file_type != "image/jpg") {
			die("
			<div id='fail' class='info_div'>
			<span class='ico_cancel'><strong>
			Invalid Format!</strong></span><br />
			<a href=\"javascript: history.go(-1)\">Retry</a>
			</div>"); }
		 //moving the file to be saved from the temp location to the destination path
		  move_uploaded_file($source, $destpath . $filename);
		  //the actual final location with timestamped name
		  $final_file_location = "$destpath$filename";
		  if (file_exists($final_file_location)) {
			  if (isset($oldimg[$ctr])) {
				  chdir('../Dir/Newsimg/');
				  echo $oldimg[$ctr];
				  unlink($oldimg[$ctr]);}
			  $newsimg[$ctr] = $filename;
			  $ctr++;
			  
?>

TNX.

Link to comment
Share on other sites

Being an early Sunday morning, cold outside, waiting for the coffee to brew, I didn't read your script; HOWEVER, if I interpret your goal correctly, you want to delete all the old unused images. Soooo.

 

Psuedo Code. (this presumes database table ONLY contains the names of the images that are current and to be kept)...

 

put all the image names into an array from the table

loop thru the image folder using glob

  if glob image NOT in_array unlink the image

end loop

 

you could even make this a CRON to run once a week in the middle of the night

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.