Repgahroll Posted November 10, 2009 Share Posted November 10, 2009 Hello. I want to have a "repository" for 3d models, however, i want it as simple as possible. My idea is to simply upload the files and their respective pictures to a folder and have a php script that will list them and display it properly automatically. For example, when i have a new chair model, i simply upload the files chair.jpg and chair.dae(3d model) to the folder furniture, when someone runs the script (by acessing the page) it reads everything in the folder, and define every *.jpg as a var, then it prints the var.jpg(as image) and var.dae(as link) in a table. Can someone help me to do this? I just need the part that declares vars for every file. Thank you in advance. Link to comment https://forums.phpfreaks.com/topic/180989-solved-list-all-files-in-a-folder-and-define-their-names-as-vars/ Share on other sites More sharing options...
Psycho Posted November 10, 2009 Share Posted November 10, 2009 This should get you started (not tested): $filepath = 'images/'; //Get all respective files into arrays $images = glob($filepath.'*.jpg'); $models = glob($filepath.'*.dae'); foreach ($images as $imageName) { //Check that there is a corresponding model tot he image $baseName = basename($path, '.jpg'); if (in_array($baseName.'.dae', $models)) { echo "<a href=\"{$filepath}{$imageName}\"><image src=\"{$filepath}{$imageName}\"></a><br />\n"; } } Link to comment https://forums.phpfreaks.com/topic/180989-solved-list-all-files-in-a-folder-and-define-their-names-as-vars/#findComment-954909 Share on other sites More sharing options...
Repgahroll Posted November 10, 2009 Author Share Posted November 10, 2009 Thank you very much!! This is exactly what I need!! Link to comment https://forums.phpfreaks.com/topic/180989-solved-list-all-files-in-a-folder-and-define-their-names-as-vars/#findComment-954937 Share on other sites More sharing options...
Repgahroll Posted November 10, 2009 Author Share Posted November 10, 2009 here's another version that I made: <?php $loop = 1; foreach(glob("*.dae") as $fileandext){ $filename = explode(".", $fileandext); echo "<a href=\"$filename[0].dae\"><image src=\"$filename[0].jpg\"></a><br />"; $loop++; } ?> Thanks mjdamato for pointing me on the right direction . Link to comment https://forums.phpfreaks.com/topic/180989-solved-list-all-files-in-a-folder-and-define-their-names-as-vars/#findComment-954985 Share on other sites More sharing options...
Psycho Posted November 10, 2009 Share Posted November 10, 2009 here's another version that I made: <?php $loop = 1; foreach(glob("*.dae") as $fileandext){ $filename = explode(".", $fileandext); echo "<a href=\"$filename[0].dae\"><image src=\"$filename[0].jpg\"></a><br />"; $loop++; } ?> Thanks mjdamato for pointing me on the right direction . The $loop variable in that code has no purpose/functionality. Also, explode is a poor implementationof trying to ascertain the base file name since a file *may* inlcude additional periods in the file name. Also, that code does not handle situations where there is a 'dae' file but no corresponding 'jpg' file. But, that can serve as a means of identifying those gaps. Here is wha I would suggest. Create an image to be used where there is no corresponding image file for the dae file (e.g. 'missingImage.jpg'). Then use the code below. <?php foreach(glob('*.dae') as $modelFile) { $basename = basename($modelFile, '.dae'); $imageFile = (file_exists("{$basename}.jpg")) ? "{$basename}.jpg" : "missingImage.jpg"; echo "<a href=\"{$modelFile}\"><image src=\"{$imageFile}\"></a><br />\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/180989-solved-list-all-files-in-a-folder-and-define-their-names-as-vars/#findComment-955125 Share on other sites More sharing options...
Repgahroll Posted November 10, 2009 Author Share Posted November 10, 2009 You're right. Thank you very much! Link to comment https://forums.phpfreaks.com/topic/180989-solved-list-all-files-in-a-folder-and-define-their-names-as-vars/#findComment-955164 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.