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.
I am kind of stuck and need help combining some script.... or coming up with some better.
I have this to name the image
[code=php]//give image a unique name
if ($fp = fopen("Picture_ Counter.txt", "r+"))
{
flock($fp, LOCK_EX); //exclusive lock
$pic_num = fgets($fp, 1024);
$image_name=$pi c_num.'.'.$exte nsion;
$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="pictu res/".$image_na me;
$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='th umbs/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_thu mb($newname,$th umb_name,WIDTH, HEIGHT);
}} }}
//If no errors registred, print the success message and show the thumbnail image created
if(isset($_POST['Submit']) && !$errors)
{
echo "<h1>Pictur e Uploaded Successfully!</h1>";
echo '<img src="'.$thumb_n ame.'">';
}
//counter file not open
else {
echo "Image not uploaded. Please try again.";
}
}
?>
[/code]
I had some help coming up with this array and am having fits trying to implement it
[code=php] <?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($num bersList, $files); //compute the difference between the files and all the numbers list
print_r($diff);
?>
[/code]
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
Any help would be great!
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.
I am kind of stuck and need help combining some script.... or coming up with some better.
I have this to name the image
[code=php]//give image a unique name
if ($fp = fopen("Picture_ Counter.txt", "r+"))
{
flock($fp, LOCK_EX); //exclusive lock
$pic_num = fgets($fp, 1024);
$image_name=$pi c_num.'.'.$exte nsion;
$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="pictu res/".$image_na me;
$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='th umbs/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_thu mb($newname,$th umb_name,WIDTH, HEIGHT);
}} }}
//If no errors registred, print the success message and show the thumbnail image created
if(isset($_POST['Submit']) && !$errors)
{
echo "<h1>Pictur e Uploaded Successfully!</h1>";
echo '<img src="'.$thumb_n ame.'">';
}
//counter file not open
else {
echo "Image not uploaded. Please try again.";
}
}
?>
[/code]
I had some help coming up with this array and am having fits trying to implement it
[code=php] <?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($num bersList, $files); //compute the difference between the files and all the numbers list
print_r($diff);
?>
[/code]
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
Any help would be great!
Comment