Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    55
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Php Image unlink Issue

    Hello,

    I am attempting to remove images from my site using a form and post. I am supplying the user with a field to put "remove" in and when submitted will be sent back to the same page via POST and checked to make sure it does equal "remove." Upon verification an unlink is performed, removing the image.
    I know the unlink code should work as I am using it elsewhere and it is working properly. I have tested to make sure that the img1Rs and img1Rl are being filled with information and have even fit the if statements requirement. For some reason this code still is not working for me. Any input would be appreciated.

    Code:
    <?php	
    	if(empty($_POST)){
    		$status = 'To edit your lot, fill out the form below. Click the Modify Lot button once.';
    		
    		$id=$_GET['id'];
    		$sql="SELECT small_file_name, large_file_name ";
    		$sql.="FROM table ";
    		$sql.="WHERE id='$id' ";
    		
    		$result = mysql_query($sql);
    		$row = mysql_fetch_assoc($result);
    		
    		$small_file_name = $row['small_file_name'];
    		$large_file_name = $row['large_file_name'];
    	}else{
    		$small_file_name=$HTTP_POST_FILES['small']['name'];
    		$small_tmp_name=$HTTP_POST_FILES['small']['tmp_name'];
    		$large_file_name=$HTTP_POST_FILES['large']['name'];
    		$large_tmp_name=$HTTP_POST_FILES['large']['tmp_name'];
    		$img1Rs=$_POST['img1Rs'];
    		$img1Rl=$_POST['img1Rl'];
    		
    		$error_list = array();
    				
    		if(empty($error_list)){
    			
    			$sql="UPDATE table ";
    			$sql.="SET ";	
    			
    			if($img1Rs == 'remove'){
    				if(is_file("$destination/$small_file_name")){
    					unlink("$destination/$small_file_name");
    					$sql.=", small_file_name = ' ' ";
    				}				
    			}
    			if($img1Rl == 'remove'){				
    				if(is_file("$destination/$large_file_name")){
    					unlink("$destination/$large_file_name");
    					$sql.=", large_file_name = ' ' ";
    				}
    			}					
    			$sql.="WHERE id='$id' ";
    									
    			if(mysql_query($sql)){
    				header('Location:index.php');
    			}else{
    				$status = 'Unable to modify post.';
    			}
    		}else{
    			$status = '<ul>';
    			
    			foreach($error_list as $error_message){
    				$status .= "<li>$error_message</li>";
    			}
    			$status .= '</ul>';
    		}
    	}
    ?>
    
    <form enctype="multipart/form-data" action="lot_modify.php" method="post">
    	<dl>
    		<?php
    			if(!empty($large_file_name)){
    				echo "<a href=\"$destination/$large_file_name\" rel=\"lightbox[$name]\" title=\"$name\"><img src=\"$destination/$small_file_name\" alt=\"$name\" /></a>  \n";
    				?>
    				<br />									
    				<dt><label for="img1Rs">Remove Small Image - Type "remove" to delete this image.</label></dt>
    				<dd><input type="text" name="img1Rs" id="img1Rs" value="<?php echo $img1Rs; ?>" /></dd>
    				<dt><label for="img1Rl">Remove Large Image - Type "remove" to delete this image.</label></dt>
    				<dd><input type="text" name="img1Rl" id="img1Rl" value="<?php echo $img1Rl; ?>" /></dd>									
    			}						
    		<dt><label for="small">Thumbnail Image (Max Dimensions: 150px by 150px)*</label></dt>
    		<dd><input type="file" name="small" id="small" value="<?php echo $small_file_name; ?>" /></dd>
    		<dt><label for="large">Large Image (Max Dimensions: 650px by 650px)*</label></dt>
    		<dd><input type="file" name="large" id="large" value="<?php echo $large_file_name; ?>" /></dd>
    	</dl>
    	<div>
    		<input type="submit" name="submit" value="Modify Lot" />
    	</div>
    </form>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    First, drop the $HTTP_POST_FILES usage. Registered long arrays have been deprecated since 4.2 and will be completely removed as of 6.0. Replace it with $_FILES superglobal.

    Second, see if its a problem with the if (which it doesn't look like), by printing out the contents of the $sql variable before running through the mysql_query. If this lists it as emptying the field for the image(s), its correct.
    Then, you'll want to ensure you're error reporting is enabled:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    If I had to take a stab in the dark, I'd say its a permissions issue on the unlink attempt, the file must be owned the by the user running apache or PHP (assuming its probably apache module), or have the write privilege set for it. Unless you're on an windows system, in which case permissions can become more interesting to deal with.

    Edit:
    I should mention as well, that for simplicity you may want to consider using a checkbox instead of a text input for this task.
    Last edited by Fou-Lu; 03-05-2009 at 07:41 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Serihon (03-05-2009)

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    55
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    First, drop the $HTTP_POST_FILES usage. Registered long arrays have been deprecated since 4.2 and will be completely removed as of 6.0. Replace it with $_FILES superglobal.

    Second, see if its a problem with the if (which it doesn't look like), by printing out the contents of the $sql variable before running through the mysql_query. If this lists it as emptying the field for the image(s), its correct.
    Then, you'll want to ensure you're error reporting is enabled:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    If I had to take a stab in the dark, I'd say its a permissions issue on the unlink attempt, the file must be owned the by the user running apache or PHP (assuming its probably apache module), or have the write privilege set for it. Unless you're on an windows system, in which case permissions can become more interesting to deal with.

    Edit:
    I should mention as well, that for simplicity you may want to consider using a checkbox instead of a text input for this task.
    Thanks for the reply Fou-Lu. I wholeheartedly agree that a checkbox would be better but due to the problems of getting this to work I wasn't sure that the code for the checkbox was hindering me. Once I get this to work with the input I will be hoping to change it to a checkbox for sure.

    As for the $HTTP_POST_FILES usage I am required to use it due to the version of php/MySQL the server is using, which is 4.0 I believe. The newer $_FILES wasn't working for me and thats when I realized it was the version issue and had to use the older version of the code. Sorry for not stating the version of code I was using when I posted originally.

    The folder containing the files I wish to delete does have proper permissions granted to allow removal.

    I went ahead and double checked what $sql was printing out and noticed that it wasn't printing out the small_image_file at all which made me think that the if() statement wasn't being processed fully.

    I then decided to check the if statement to see how far through it I was getting. Turns out I would get through the original if($img1Rs=='remove') but I wasn't getting through if(is_file("$destination/$small_file_name")). Seems that there is an issue with this particular part.

    Then I decided to check that $destination and $small_file_name had proper contents. This is where the issue is at it seems. This code I am attempting to add is to remove images if they exist and the user doesn't want them there anymore. Since nothing is inputted into the small_file_name input nothing is passed with POST and my $small_file_name is remaining empty when it comes to the if(is_file()) check. So it can't prove the image exists and doesn't finish through with the statement.

    I believe I need to pull the $small_file_name back from the database so that $small_file_name isn't empty when I run the is_file check.

    I went ahead and added what I thought I would need and I have gotten it to work. Here is the final code.

    If statement
    Code:
    if($img1Rs == 'remove'){
    	$sql2="SELECT small_file_name ";
    	$sql2.="FROM table ";
    	$sql2.="WHERE id='$id' ";
    		
    	$result = mysql_query($sql2);
    	$row = mysql_fetch_assoc($result);
    				
    	$smallRemove1 = $row['small_file_name'];				
    	if(is_file("$destination/$smallRemove1")){
    		unlink("$destination/$smallRemove1");
    		$sql.=", small_file_name = ' ' ";
    	}			
    }
    Checkboxes
    Code:
    <?php
    	echo "Remove thumbnail image #1.<input type=\"checkbox\" name=\"img1Rs\" id=\"img1Rs\" value=\"remove\" /> \n";
    ?>
    There is a huge space between the text and the checkbox though. Any idea how to remove the spacing around the checkbox?

    ___________

    Also what does
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    do and how is it used? I have never seen or used it before.

    Thanks again for your help Fou-Lu. I appreciate your assistance with this.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The ini_set tells the runtime configuration to allow displaying of errors (printed to the screen, and should be disabled in live environments), and the error_reporting tells it to send you feedback on any error including notices (undefined variable appending, invalid function calls, etc).

    I totally missed out on the file seeking part. Lol, but glad you got that fixed. Yes, you definitely want to retrieve it from the database instead of a file (for the path / name).

    The spacing is a CSS thing. I'd wrap the text in a <label> tag (and apply a for to identify the checkbox's id). That should remove the spacing between it, but I suppose that depends on the rules associated with the checkbox.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    55
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I guess I am not sure when to stop.
    I decided I would like to turn this whole process into a function to clean up some of my code.

    This is the original code that works
    Code:
    if($img1Rl == 'remove'){
    	$sql2="SELECT large_file_name ";
    	$sql2.="FROM premiere ";
    	$sql2.="WHERE id='$id' ";
    		
    	$result = mysql_query($sql2);
    	$row = mysql_fetch_assoc($result);
    				
    	$largeRemove1 = $row['large_file_name'];				
    	if(is_file("$destination/$largeRemove1")){
    		unlink("$destination/$largeRemove1");
    		if(empty($large_file_name)){
    			$sql.=", large_file_name = ' ' ";
    		}
    	}				
    }
    And here is the code that I am trying to use now as a function.
    Code:
    function imgRemove($imgDest, $imgSrc, $imgID){
    	$sql2="SELECT $imgSrc ";
    	$sql2.="FROM table ";
    	$sql2.="WHERE id='$imgID' ";
    		
    	$result = mysql_query($sql2);
    	$row = mysql_fetch_assoc($result);
    				
    	$imgRemove = $row['$imgSrc'];			
    	if(is_file("$imgDest/$imgRemove")){
    	      unlink("$imgDest/$imgRemove");
    	      if(empty($imgSrc)){
    			$sql.=", $imgSrc = ' ' ";
    		}
    	}		
    }	
    			
    if($img1Rs == 'remove'){	
    	imgRemove($destination, 'small_file_name', $id);
    }
    I have tried echoing out the variables once inside the function but never see anything. I have echo'd out information from the if() statement that triggers the function so I know it is getting at least that far. But other than that it isn't working. Any input on this would be great.

  • #6
    New Coder
    Join Date
    Mar 2009
    Posts
    55
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Ok... this is bizarre.

    I go ahead and start changing something else on the page and look and it seems the function is working now.... very bizarre.

    So the final code that works is this.
    Code:
    function imgRemove($imgDest, $imgSrc, $imgID){
    	$sql2="SELECT $imgSrc ";
    	$sql2.="FROM table ";
    	$sql2.="WHERE id='$imgID' ";
    		
    	$result = mysql_query($sql2);
    	$row = mysql_fetch_assoc($result);
    				
    	$imgRemove = $row[$imgSrc];			
    	if(is_file("$imgDest/$imgRemove")){
    		unlink("$imgDest/$imgRemove");
    	}		
    }	
    			
    if($img1Rs == 'remove'){	
    	imgRemove($destination, 'small_file_name', $id);
    	if(empty($small_file_name)){
    		$sql.=", small_file_name = ' ' ";
    	}
    }
    So disregard previous post I guess.
    Last edited by Serihon; 03-05-2009 at 10:23 PM. Reason: Code was wrong just a tad. Not sure how to return information yet.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •