How can I let an uploaded file overwrite an existing file that has the same name?
I was able to comment out the code in the upload processor file that creates a unique filename for the uploaded file, but I get a server error if that file name already exists.
I want to keep it really simple so the files can be named ID1.jpg, ID2.jpg etc.
Save the upload date in your DB and every so often delete the ones older than a certain age. Could be set up as a cron job.
Awesome, so I can set up a cron job to delete the image itself in my "images" folder?
Do you know how to set this up? I know how to delete things out of the database but I don't know how to set up a program to delete actual files in a folder.
I realize the last post on this thread is a year ago but it still comes up high on the list of Google searches for multiple file uploads. I tested the code and it works great(in fact the best I found), but I need to add the names of the uploads to a database. I read through the whole thread and saw the addendum that allows for this but it seems to add each image as a separate record. I want to add up to 8 images to a single record (along with other info). Arrays always give me headaches! I can't figure out where or how to add to the code. I need to add the images to fields image1, image2, image3 etc. Any help greatly appreciated!
<?php
// filename: multiple.upload.form.php
// first let's set some variables
// make a note of the current working directory relative to root.
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
// make a note of the location of the upload handler
$uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'multiple.upload.processor.php';
// set a max file size for the html upload form
$max_file_size = 300000; // size in bytes
// now echo the html page
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<title>Upload form</title>
</head>
<body>
<form id="Upload" action="<?php echo $uploadHandler ?>" enctype="multipart/form-data" method="post">
<h1>Upload form</h1>
<p><input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size ?>"></p>
<p><label for="location">Location:</label>
<input type="text" name="location">
</p>
<p><label for="description">Description:</label>
<input type="text" name="description">
</p>
<p><label for="image1">File to upload:</label>
<input id="image1" type="file" name="file[]">
</p>
<p><label for="image2">File to upload:</label>
<input id="image2" type="file" name="file[]">
</p>
<p><label for="image3">File to upload:</label>
<input id="image3" type="file" name="file[]">
</p>
<p>
<label for="submit">Press to...</label>
<input id="submit" type="submit" name="submit" value="Upload us!">
</p>
</form>
</body>
</html>
I added the following to the original code on the processor page
What I get with the echo is
[QUOTE]INSERT INTO properties ('location','description','image1','image2','image3') VALUES ('Main Street','2 unit townhouse','Array','Array','Array',)[QUOTE]
So 2 problems:
1. how do I get the actual name of the file and
2. how do I get rid of the final comma in the query.
I should add that immediately before the code I added is the following
Code:
// now let's move the file to its final location and allocate the new filename to it
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);
which is where the $uploadFilename in my query came from.
OK I see part of my error now. So I replaced
$uploadFilename
with
$uploadFilename[$key]
in the query and I get the file name, including the whole path from c:
If I echo this
Code:
foreach($active_keys as $key)
{
echo $_FILES[$fieldname]['name'][$key];
}
I get the simple names of the files without the path. Somehow I have to put each file name into a variable like $filename1, $filename2, etc. up to $filename8 and add it to the query. The problem is I don't know how to build an array to do that. Also, the user may not upload 8 files so I'll get an error in my query if there are fewer images. I've been struggling with this for days now, so if anyone can help you'll save me from pulling out my hair
Bookmarks