In the code below I would like to be able to input a single character from the keyboard without having to type afterwards. Currently I have to add at the read_stdin() statement. This greatly slows the process when I have a lot of photos. Doubling the keystrokes required to sort each photo.
<?php
// View the jpg images one at a time
$files = glob("images/*.jpg");
foreach($files as $jpg){
// echo "<img src='$jpg'></br>";
echo "view image somehow" . PHP_EOL;
echo "The jpg variable equals '$jpg'" . PHP_EOL;
// show them a message to enter letter for category
echo "Enter the category, a(family), r(friends), c(coworkers), d(delete), ctrl-letter(two folders)" . PHP_EOL;
// the script will wait here until the user has entered something and hit ENTER
$category = read_stdin();
// This will display the category message including the category they entered.
echo "You chose $category . Next photo" . PHP_EOL;
switch ($category) {
case "a":
echo "$category equals family" . PHP_EOL;
rename("$jpg", "images/sorted/family/$jpg");
break;
case "r":
echo "$category equals friends" . PHP_EOL;
rename("$jpg", "images/sorted/friends/$jpg");
break;
case "c":
echo "$category equals coworkers" . PHP_EOL;
rename("$jpg", "images/sorted/coworkers/$jpg");
break;
default:
echo "$category is not equal to a,r, or c" . PHP_EOL;
}
}
// our function to read from the command line
function read_stdin()
{
$fr=fopen("php://stdin","r"); // open our file pointer to read from stdin
$input = fgets($fr,128); // read a maximum of 128 characters
$input = rtrim($input); // trim any trailing spaces.
fclose ($fr); // close the file handle
return $input; // return the text entered
}
?>