Hi. I'm working on a script to upload images to my server. What I want it to do is rename all the uploaded images in order like image1, image2, image3 etc. Then just an option to upload or delete exisiting images. Here's the code I came up with:
<HTML>
<HEAD>
<TITLE>Intro Loader</TITLE>
</HEAD>
<BODY>
<form method="post" action="introLoader.php" enctype="multipart/form-data">
<p>Select an image to upload:</p>
<input type="file" name="newImage"><br>
<input type="hidden" name="MAX_FILE_SIZE" value="1">
<input type="submit" name="submitUpload" value="upload now">
</form>
<?php
$itemCount = 1;
$uploaddir = '../introImages/';
$d = dir($uploaddir);
while (false !== ($entry = $d->read())) {
if ($entry!="."&&$entry!="..") {
$itemCount++;
}
}
$d->close();
if (isset($_REQUEST['MAX_FILE_SIZE'])) {
$uploadTempSpot = $_FILES['newImage']['tmp_name'];
$uploadfile = $uploaddir . "image".$itemCount.".jpg";
@move_uploaded_file($uploadTempSpot,$uploadfile);
echo "Image Uploaded";
}
//$_POST['username'];
echo '<FORM method="POST" ACTION="introLoader.php">';
$itemCount=0;
$itemsChanged = false;
$d = dir($uploaddir);
while (false !== ($entry = $d->read())) {
if ($entry!="."&&$entry!="..") {
$itemCount++;
$currentImage = $_POST['removeImage'.$itemCount];
if ($currentImage=="on") {
$itemsChanged = true;
unlink ($uploaddir.$entry);
}
else {
echo '<input type=checkbox name = "removeImage'.$itemCount.'">';
echo '<img src="'.$uploaddir.$entry.'"width=200 height=200>';
}
}
}
$d->close();
echo '<input type="submit" name="submitChanges" value="Make Changes"></FORM>';
if ($itemsChanged) {
$itemCount=0;
$d = dir($uploaddir);
while (false !== ($entry = $d->read())) {
if ($entry!="."&&$entry!="..") {
$itemCount++;
rename ($uploaddir.$entry , $uploaddir . "image" . $itemCount. ".jpg");
}
}
$d->close();
}
if ($submitChanges) {
echo "changed";
}
?>
</BODY>
</HTML>
As you can see, it's fairly simple. I'm running into a couple problems though. It doesn't seem to rename the images correctly a lot of the time. I have it set so if you delete an image it will loop through and rename other ones so they are in order again, but this works erratically. My other problem is that if you delete say 'image4' and upload another image that is renamed 'image4' the browser will display the previous image because it's still in your temp files cache.
Any suggestions on improving this from the peeps who have been through this already would be greatly appreciated.