mindrage00 Posted September 9, 2007 Share Posted September 9, 2007 Hi! I am creating an image gallary where the images are numbered 1, 2, 3 etc. Users will be uploading the images so if i need to delete an image, I need the code to be able to check if that number has been deleted and replace it with the next uploaded image. I have the code pulling from a txt file with a counter to name each file. The problem is that if images "36", "29" and "2" are removed... I need those slots to replaced in numerical order with the next uploaded images. So the next image will be named image "2" and will show up as the 2nd image on the page. Here is a chunk: // checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; // if it is not empty if ($image) { // get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); // get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); // if it is not a known extension, we will suppose it is an error, print an error message //and will not upload the file, otherwise we continue if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png")) { echo '<h1>Unknown extension!</h1>'; $errors=1; } else { // get the size of the image in bytes // $_FILES[\'pictures\'][\'tmp_name\'] is the temporary filename of the file in which the uploaded file was stored on the //server $size=getimagesize($_FILES['image']['tmp_name']); $sizekb=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($sizekb > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //we will give an unique name if ($fp = fopen("Picture_Counter.txt", "r+")) { flock($fp, LOCK_EX); //exclusive lock $pic_num = fgets($fp, 1024); $image_name=$pic_num.'.'.$extension; $pic_num = $pic_num + 1; rewind ($fp); fwrite($fp, $pic_num); flock($fp, LOCK_UN); //release the lock fclose($fp); //the new name will be containing the full path where will be stored (images folder) $newname="pictures/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); //we verify if the image has been uploaded, and print error instead if (!$copied) { echo '<h1>Upload unsuccessfull!</h1>'; $errors=1; } else { // the new thumbnail image will be placed in thumbs/ folder $thumb_name='thumbs/thumb_'.$image_name; // call the function that will create the thumbnail. The function will get as parameters //the image name, the thumbnail name and the width and height desired for the thumbnail $thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT); }} }} //If no errors registred, print the success message and show the thumbnail image created if(isset($_POST['Submit']) && !$errors) { echo "<h1>Picture Uploaded Successfully!</h1>"; echo '<img src="'.$thumb_name.'">'; } //counter file not open else { echo "Image not uploaded. Please try again."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/68543-filling-in-for-deleted-images/ Share on other sites More sharing options...
Fadion Posted September 9, 2007 Share Posted September 9, 2007 I had a try to this just just because it looked fun, as im aware this may not be the best method. Try it, modify it and if u have any problem just tell. I also wrote some simple comments for better understanding: <?php $files = glob("*.jpg"); //get all jpeg files for($i=0; $i<count($files); $i++){ $files[$i] = basename($files[$i], '.jpg'); //remove the jpeg so we got only the number } sort($files); //sort them $first = $files[0]; //get the first number $last = $files[count($files) - 1]; //get the last number $numbersList = range($first, $last); //create an array with all numbers from $first to $last $diff = array_diff($numbersList, $files); //compute the difference between the files and all the numbers list print_r($diff); ?> What it does is create the array $files with all the jpeg images, strip all the values of the array from ".jpg" so u get only the numbers and sort the array. Then i got the first and last number to create another array with the ideal numbers list. The $diff array is create by computing the difference between the original files array and the ideal range. So: files array 1, 2, 3, 5, 7, 8, 9, 10, 14 ideal array 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 difference 4, 6, 11, 12, 13 This is basically what u wanted right?! Hope it helps and u find it easy to implement in your script. Quote Link to comment https://forums.phpfreaks.com/topic/68543-filling-in-for-deleted-images/#findComment-344590 Share on other sites More sharing options...
mindrage00 Posted September 9, 2007 Author Share Posted September 9, 2007 Thanks for the fast reply! Great piece of code. I'll give it a shot! Quote Link to comment https://forums.phpfreaks.com/topic/68543-filling-in-for-deleted-images/#findComment-344597 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.