View Full Version : deleting files

06-12-2013, 02:36 PM
hi guys... need help...

i have got pretty much all of a site up and running. i am uploading multiple files to a created folder for each record and its all going well :) the relevant code for the file uploads is

$dirname = $_POST["stockID"];
$filename = "../images/cars/" . $dirname . "/";

if (!file_exists($filename)) {
mkdir("../images/cars/" . $dirname, 0777);

} else {
die( '<div class="error"> "The directory" . $dirname . "exists."</div>
<div><button onclick="history.go(-1);">Back </button></div>' );

if (isset($_FILES['pix']) === true) {
$files = $_FILES['pix'];

for($x = 0; $x < count($files['name']); $x++) {

$errors = array();
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');

$file_name = $files['name'][$x];
$file_ext = strtolower(end(explode('.', $file_name)));
$file_size = $files['size'][$x];
$file_tmp = $files['tmp_name'][$x];

if (in_array($file_ext, $allowed_ext) === false) {
$errors[] = 'Extension not allowed';
if ($file_size > 4097152) {
$errors[] = 'File size must be under 2mb';

if (empty($errors)) {

if (move_uploaded_file($file_tmp, "../images/cars/" . $dirname . "/" . $file_name));

} else {
foreach ($errors as $error) {
echo $error, '<br />';


HTML form
<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1">
<label for="pix[]">Choose files to upload:</label>
<input type="file" name="pix[]" id="pix[]" multiple />
<input type="submit" name="insert" id="insert" value="Add to stock"></p>

my big question is, whilst the folders contain random amounts of images, with the usual type of image names and the directories using the Primary key to name, i cannot delete them when i delete a record. tried using
$dir = '../images/cars/' . $row_getStock["stockID"];

foreach(glob('../images/cars' . $row_getStock["stockID"] . '/*.jpg') as $file) {

rmdir ($dir);

with no joy at all..i just want to click the delete record link and it all happen :) am i asking too much

any ideas?


06-13-2013, 02:53 AM
i can only think the returned path is not correct.

i dont see where you inset the file name into your database, that would create the correct path.

06-13-2013, 08:41 AM
My mistake.. I should have clarified more :)

There is no correlation to the database for the image files, other than using the primary key to name the directory, as I'm using über gallery to handle the thumbnail creation on the fly, so to speak... Just pointing it at 'images/cars/' . $someVariable . '/', as the amount of images uploaded could vary from one upwards so didn't want to get into something like if there's 20 images and only 6 fields, write and name another 14 fields (columns?) to accommodate

As this is essentially my first dive into PHP I didn't know what the standard was so went with my gut. It's taken quite a few weeks for this simple(ish) site because at every point I've looked at tutorials and if I've d/l'd code to use, I've studied it to see if I can find out how it does what it does :) I do have to say though... Some of the stuff dreamweaver inserts automatically has me struggling lol

So, essentially all I want to do is,say, if I have a folder structure of

Then after that the variable named folders that tie to the record entries, with each folder containing a variable amount of files

What I want to do is when I delete, say, record number 6 (which is happen ending fine btw) I also want to delete folder at images/cars/6/ and all files inside that directory which could be 1- whatever depending on his uploading lol

So I need a script that will upon submitting the delete record, look how many files are in said folder, delete them and folder... Simple huh lol

Now it can either do the deleting on submit, or even another page to show the files graphically like an FTP page but not as my friend is totally not web friendly :)

This lot may or may not have clarified


07-04-2013, 05:39 AM
you will note that .. may not necessarily refer to your web directory. it could refer to your home directory. why not verify that by doing
echo getcwd();note that this may not work, it may return false if PHP isn't configured to work with it. read up on it first in the php docs.
current working directory.
you could also replace your .. with $_SERVER['DOCUMENT_ROOT' ]

THAT you can echo with no problems, it always works. be sure to see what it actually is. it could be be your web directory. also note that its value may change over time, so don't try using a canned value.