On an Intranet site, I am moving quite a few images from the file system into MySQL and have written a little function to assist. There are no errors but neither are the images being inserted so I need to help to figure out how to do it without LOAD_FILE() as it requires folder and file permissions changes that I cannot do.
Note that it is necessary to query the albums table in order that the Primary Key from the albums table can be inserted with the image so that the programming that displays the album will know which image to load.
function uploadFolder($path,$thumbpath) {
// Use form to submit path (keeps process from running automatically when loaded)
if (isset($_POST['create']) && isset($path)) :
global $siteDB;
// Fetch image names from existing albums
$sqlAlbum = "SELECT ID, ImageName FROM albums ORDER BY ImageName";
$sqlRow = DBConnect($sqlAlbum, "Multiple", $siteDB, "assoc");
// Build queries to insert all images to table
foreach ($sqlRow as $row) :
$filename = basename($row['ImageName']);
$albumID = $row['ID'];
$pathname = $path.$filename;
$sqlUpload = "INSERT INTO images (`ImageName`,`AlbumID`,`ImageFull`) VALUES ('$filename', $albumID, LOAD_FILE('$pathname'))";
// Run the query for each file
DBConnect($sqlUpload,"Run", $siteDB);
endforeach;
// FOR TEST PURPOSES ONLY
$_SESSION['testSQL'] = $sqlUpload;
endif;
}
it is being called with:
if (isset($_POST)) :
$path = (isset($_POST['path'])) ? $_POST['path'] : "";
uploadFolder($path,"thumbnails");
endif;
Not much to the images table but here it is for reference:
CREATE TABLE IF NOT EXISTS `images` (
`ID` int unsigned NOT NULL AUTO_INCREMENT,
`ImageName` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
`AlbumID` int unsigned DEFAULT NULL,
`ImageFull` blob,
`ImageThumb` blob,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=228 DEFAULT CHARSET=utf8;
LOAD_FILE() problems aside, there are two ways I could have done this. One is to read all the files and insert them IF they exist in the albums table by querying it. The other as I showed above is to get a list of files from the albums table by querying it, then find and insert those images from the file system. Doing the latter (in theory) prevents it from inserting stray photos that are not supposed to be in the album.
Although I didn't show it here because I realized it after posting the question, doing the latter also lets me grab the Description column from the albums table so that I could tell which image was which.