Search code examples
phpimagefilejpeg

PHP - Generate dropdown List of images -> Copy image to new Dir -> Delete original image


I have a drop down list that generates all files in a folder, which is working. But i would like to only see .jpg files and also i would like to exclude one file from the list as it is a place holder image lets call it "0001_Place_Holder.jpg".

The second part to this is that i want to pick a file from the dropdown list and copy it to a New folder then delete the original image.

this is "move_files_general.php" // which generates my dropdown list

<?php
$dirname = "general_2";
$dir = opendir($dirname);
echo '<form action="move_general.php" method="get">';
echo '<select name="file2">';
while(false != ($file = readdir($dir)))
{
    if(($file != ".") and ($file != ".."))
    {
        echo "<option value=".$file.">$file</option>";
    }
}
echo '</select>';
echo '<input type="submit" value="Move To Quality" class="submit" />';
echo '</form>';
?>

This is "move_general.php" // which should copy the file then delete the original

<?php
$dirpath = "general_2";
$dirpath_2 = "quality_2";
$file_to_move = $_GET['file2'];
copy("$dirpath.'/'.$file_to_move", "$dirpath_2.'/'.$file_to_move") or       die("Unable to copy");
if (copy("$dirpath.'/'.$file_to_move", "$dirpath_2.'/'.$file_to_move")) {
unlink("$dirpath.'/'.$file_to_move");
    if ( unlink ($dirpath.'/'.$file_to_move) ) {
            echo $file_to_move . " deleted.";
        echo '<script>parent.window.location.reload(true);</script>';
    } else {
    echo "Error.";
}
}
?>

Solution

  • First off, Thanks for your answers, and help. Alex Odenthal, that worked for the 1st part. i tried everything to get the 2nd part to work. I finally rewrote it a different way and it's working now, I must have had something wrong , somewhere.

    Here is my fixed "move_files_general.php"

    <?php
    $dirname = "general";
    $dir = opendir($dirname);
    echo '<form action="move_general.php" method="get">';
    echo '<select name="file2">';
    while(false != ($file = readdir($dir)))
    {
        if(($file != ".") and ($file != "..") and ($file != "0001_Place_Holder_DO_NOT_DELETE.jpg"))
        {
    if(pathinfo($file, PATHINFO_EXTENSION) ==='jpg'){
        echo "<option value=".$file.">$file</option>";
    
    }
    }
    
    }
    echo '</select>';
    echo '<input type="submit" value="Move To Quality1" class="submit" />';
    echo '</form>';
    ?>
    

    Here is my fixed "move_general.php"

    <?php
    $file_to_move = $_GET['file2'];
    $source = "general/$file_to_move";
    $dest = "quality/$file_to_move";
    copy($source, $dest);
    if (copy($source, $dest)) {
        unlink($source);
                if(file_exists($source)) {
                unlink($source); }
        else {
                echo "Deleted.";
                }
            }
    
    
    ?>