Page 1 of 1

Delete image from server

Posted: Mon Dec 19, 2011 12:18 am
by andy1212
I have code so someone can upload an image to the server in a specific folder but I've searched high and low and tried to find a way that someone can delete the image they uploaded completely off the server or even just unlink the image and hvaen't found a solution. Is there code where someone can press a delete button and the image gets deleted? If there is no way for someone to delete the image off the server other than myself the admin, is there a way to rename the file? I've tried using code from a tutorial to rename an image file and it only added the new file name infront of the old filename and extension. Here is the code I have to upload the file I found from a tutorial.

Code: Select all

<?PHP
			
				if (isset($_FILES['image'])) {
				$errors = array();
				$allowed_ext = array('jpg', 'jpeg', 'png', 'gif', 'psd', 'pdd', 'bmp', 'eps', 'pdf', 'pdp', 'ai', 'tiff', 'tif');
	
				$file_name = $_FILES['image']['name'];
				$file_ext = strtolower(end(explode('.', $file_name)));
				$file_size = $_FILES['image']['size'];
				$file_tmp = $_FILES['image']['tmp_name'];
	
				if (in_array($file_ext, $allowed_ext) === false) {
						$errors[] = "<span class='login2'>Extension not allowed. Allowed extensions are .jpg, .jpeg, .png, .gif, .psd, .pdd, .bmp, .eps, .pdf, .pdp, .ai, .tiff, .tif</span>";
				}
	
				if ($file_size > 167772160) {
						$errors[] = "<span class='login2'>Maximum file size is 20mb.</span>";
				}
	
				if (empty($errors)) {
					
					if (move_uploaded_file($file_tmp, 'staffpics1/'.$file_name)) {
						echo "<span class='login2'>File Uploaded</span>";
					}
				} else {
					foreach ($errors as $error) {
						echo $error, '<br />';
					}
				}
				}
			
            	?>
and the image is displayed using this code,

Code: Select all

<?php

					$dir = 'staffpics1';
					$file_display = array('jpg', 'gif', 'png');

					if(file_exists($dir) == false) {
						echo "Not Found";
					}
					else {
						$dir_contents = scandir($dir);
	
						foreach ($dir_contents as $file) {
							$file_type = strtolower(end(explode('.', $file)));
		
							if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
								echo '<img src="', $dir, '/', $file, '" alt="', $file, '" />';
							}
						}
					}

					?>
I was trying this code using the unlink code,

Code: Select all

<?php
unlink('public_html/staffpics1/',$file_name);
?>

Re: Delete image from server

Posted: Mon Dec 19, 2011 4:54 am
by phpHappy
is there a directory under the one the file is in named public_html?
I think maybe the problem lies there
this file catches a delete form:

Code: Select all

<?php
$dir =  '../staffpics1';   // adjust


if(isset($_POST['filename'])) {

	 if( unlink($dir.'/'.$_POST['filename']) ) {
	 	echo 'File removed successfully.';
	}
	 else {
	 	echo 'File was not removed';
	 }
	exit();
}
?>


Re: Delete image from server

Posted: Mon Dec 19, 2011 12:30 pm
by andy1212
Ok I'll try that, no the directory that the file is in has no other directories within it. It just said in the tutorial to type of the complete path of the file starting from the root directory. Does unlink actually delete the file of the server? I tried reading up on it and alot of posts from people who have used it say it doesn't delete it, just unlinks it from the page. Thanks for your time and help.

Re: Delete image from server

Posted: Mon Dec 19, 2011 1:09 pm
by califdon
Yes, the PHP function unlink() deletes a file. Check out http://www.w3schools.com/php/func_filesystem_unlink.asp and http://php.net/manual/en/function.unlink.php.

Are you getting an error message? If not, you should temporarily insert the following 2 lines at the beginning of your PHP code:

Code: Select all

ini_set("display_errors",1);
ERROR_REPORTING(E_ALL);
I assume that you have checked to see that the file with that filename is actually there in that directory. Be aware that on a Linux system the filenames are case sensitive.

The first thing I would check is whether you have assigned the correct value to the variable $file_name. Temporarily insert a line just before the line with the unlink:

Code: Select all

echo "The filename is: " . $file_name; 
If the filename doesn't appear when you run the script, it means that the variable is blank, so of course it wouldn't be able to find the file to delete it.

Re: Delete image from server

Posted: Mon Dec 19, 2011 2:28 pm
by andy1212
Thanks, ill try that as soon as I get the chance post back here with my result

Re: Delete image from server

Posted: Wed Dec 21, 2011 2:18 am
by andy1212
I'm not really sure how I should have this set up but I have the code on it's own page so it doesn't interfere with the page the other php code is on that displays the image. I named the new page delete and simply have this code to test out the unlink function, however it isn't working. Here's the code I have on the delete.php page,

Code: Select all

<html>
<input type="submit" id="submit" Value="Delete" />
</html>
<?php
$dir =  '../staffpics1';   // adjust
$file_name = $_FILES['image']['name'];

if(isset($_POST['submit'])) {

		echo "The filename is: " . $file_name; 
         if( unlink($dir.$file_name.$_POST['submit']) ) {
                echo 'File removed successfully.';
        }
         else {
                echo 'File was not removed';
         }
        exit();
}
?>
Thanks for your time.

Re: Delete image from server

Posted: Wed Dec 21, 2011 11:39 am
by califdon
You should use exactly the same syntax in your unlink function that used immediately above to check on its value. In other words, use $filename, not $dir.$file_name.$_POST['submit']. That's the only way to know for certain what your unlink function is being told to delete.

If so, then did it display 'File was not removed'?

As I suggested in my previous post, insert those 2 lines immediately after the line:

Code: Select all

<?php

Re: Delete image from server

Posted: Wed Dec 21, 2011 9:59 pm
by andy1212
So I tried this code,

Code: Select all

<form method="post">
<input type="submit" id="filename" name="filename" Value="Delete" />
</form>

Code: Select all

<?php
echo "The filename is: " . $file_name; 
$dir =  '../staffpics1/';   // adjust

if(isset($_POST['filename'])) {

         if( unlink($dir.'/'.$_POST['filename']) ) {
                echo 'File removed successfully.';
        }
         else {
                echo 'File was not removed';
         }
        exit();
}
?>
and it echo's back file was not removed. I feel like this part of the code, unlink($dir.'/'.$_POST['filename'] should have $file_name in it somewhere but not sure where to put it. Or maybe at this part of the code, $dir = '../staffpics1/'; // adjust, I just be adjusting something there? Thanks for your time and help and looking forward to figuring this out.

Re: Delete image from server

Posted: Thu Dec 22, 2011 1:11 am
by andy1212
Ok so i figured it out but I set it up a little differently following a tutorial about removing a directory and applying what you had posted and put it together. Thanks for your help it got the ball rolling for me. Here's the new code I have.

<html>
<form method="post">
<input type="submit" id="filename" name="filename" Value="Delete" />
</form>
</html>

Code: Select all

<?php
$dir = "staffpics1";
$dirContents = scandir($dir);
foreach($dirContents as $contents => $dirContentname){
	$currentPath = $dir.'/'.$dirContentname;
	
	if($_POST['filename']){
		unlink($currentPath);
		echo "file deleted";
	}
}

echo $dir.$dirContents;
?>

Re: Delete image from server

Posted: Thu Dec 22, 2011 3:11 pm
by phpHappy
good deal
paths and relative paths can be confusing til you learn them