Jump to content

[SOLVED] List all files in a folder and define their names as vars.


Repgahroll

Recommended Posts

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.

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";
    }
}

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 :D.

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 :D.

 

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";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.