flexxall Posted August 15, 2009 Share Posted August 15, 2009 Is there a way I can have a script read the contents of an image directory and input that information into my image database without having to do each image manually viz a web form ? Quote Link to comment https://forums.phpfreaks.com/topic/170449-upload-directory-contents-into-mysql-database/ Share on other sites More sharing options...
wildteen88 Posted August 15, 2009 Share Posted August 15, 2009 Yes look into using glob or opendir, readdir and closedir to loop through the images in your image directory. Quote Link to comment https://forums.phpfreaks.com/topic/170449-upload-directory-contents-into-mysql-database/#findComment-899119 Share on other sites More sharing options...
flexxall Posted August 16, 2009 Author Share Posted August 16, 2009 So I was able to return the data ok with this <?php $path = "./"; $files = "*.gif"; $sfiles = glob($path.$files); asort($sfiles); foreach ($sfiles as $filename) { echo "<br>\n", "$filename " . filesize($filename); } ?> I could use assistance assigning the information variables that can then be uploaded to the database. My table consists of this so far and Im not sure if this is the proper way to store the data. name varchar(100) latin1_swedish_ci Yes NULL Browse distinct values Change Drop Primary Unique Index Fulltext size int(11) Yes NULL Browse distinct values Change Drop Primary Unique Index Fulltext type varchar(20) latin1_swedish_ci Yes NULL Browse distinct values Change Drop Primary Unique Index Fulltext content mediumblob BINARY Yes NULL Browse distinct values Change Drop Primary Unique Index Fulltext Quote Link to comment https://forums.phpfreaks.com/topic/170449-upload-directory-contents-into-mysql-database/#findComment-899405 Share on other sites More sharing options...
flexxall Posted August 16, 2009 Author Share Posted August 16, 2009 Made som eprogress but need help with variables Heres what I have now that kind of works <?php $path = "./"; $files = "*.gif"; $name = '$filename'; //Variable here $size = 'size'; //Variable here $type = 'type'; //Variable here $sfiles = glob($path.$files); asort($sfiles); foreach ($sfiles as $filename) { echo "<br>\n", "$filename " . filesize($filename); } $content = file_get_contents($filename); if ($conn = mysqli_connect('localhost', 'root', 'xxx', 'xxx')) { $content = mysqli_real_escape_string($conn, $content); $sql = "insert into images (name, size, type, content) values ('{$name}', '{$size}', '{$type}', '{$content}')"; if (mysqli_query($conn, $sql)) { $uploadOk = true; $imageId = mysqli_insert_id($conn); } else { echo "Error: Could not save the data to mysql database. Please try again."; } mysqli_close($conn); } else { echo "Error: Could not connect to mysql database. Please try again."; } ?> But this adds a file to the table with $filename 0 size and no type. I am having defining the variable correctly Quote Link to comment https://forums.phpfreaks.com/topic/170449-upload-directory-contents-into-mysql-database/#findComment-899442 Share on other sites More sharing options...
flexxall Posted August 17, 2009 Author Share Posted August 17, 2009 Anyone help with this ? Quote Link to comment https://forums.phpfreaks.com/topic/170449-upload-directory-contents-into-mysql-database/#findComment-900312 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.