Jump to content

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


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

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.