Delete image from server
Posted: Mon Dec 19, 2011 12:18 am
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.
and the image is displayed using this code,
I was trying this code using the unlink code,
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 />';
}
}
}
?>
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, '" />';
}
}
}
?>
Code: Select all
<?php
unlink('public_html/staffpics1/',$file_name);
?>