TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-10-2007, 04:21 AM   #1 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
/\/\ongoose is on a distinguished road
Default PHP and external files

Is there a way to make php scan a directory...for example a folder full of pictures, and then list them in some way as links or whatever you need? If show could you show an example?

I'm building a photogallery, and it will have several different categories of pictures and I would like to use php to automate adding pictures and new brands.

Heres what I want it to look like :
http://sharpenedconception.com/dcdes...otogallery.jpg

What do you think?
/\/\ongoose is offline  
Reply With Quote
Old 12-10-2007, 04:29 AM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

For that job, it would be better to use a database.

Either way, here is how to list the files in a directory
http://www.php.net/function.opendir
__________________

Village Idiot is offline  
Reply With Quote
Old 12-10-2007, 04:43 AM   #3 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
/\/\ongoose is on a distinguished road
Default

Well I followed that link that you gave me and I did this code:

<html>
<head>
</head>
<body>
<?php
$path = dirname($_SERVER['SCRIPT_FILENAME']);
?>

<h3>Files in <?php print $path; ?></h3>
</body>
</html>

And when it processed the script, all it did was output the directory name, it didnt output any files?
/\/\ongoose is offline  
Reply With Quote
Old 12-10-2007, 04:54 AM   #4 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
/\/\ongoose is on a distinguished road
Default

Nevermind, I am stupid. I'm doing that tutorial now. Although I dont understand it all. Could you explain how using a db would be better? The reason for me wanting to do this is so that all I would have to do in the future to add pictures is drag the file into the directory. Would it still be possible using a db?
/\/\ongoose is offline  
Reply With Quote
Old 12-10-2007, 05:13 AM   #5 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
/\/\ongoose is on a distinguished road
Default

Anyone that could explain using a database in this situation would be greatly appreciated. I want to know all angles of the task before I approach it
/\/\ongoose is offline  
Reply With Quote
Old 12-10-2007, 05:15 AM   #6 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

You can use a function called glob(). It uses a pattern to search for the required files. If all you have in that directory are images, you can use the following:

http://php.net/glob

php Code:
<?php
foreach (glob("*") as $szFilename) {
    echo '<img src="' . $szFilename . '" alt="Image" />';
}
?>

Database would be overkill for a simple thing like this which can be achieved using one function unless you want to output more information regarding the file like titles, short description, tags and much more..
Haris is offline  
Reply With Quote
Old 12-10-2007, 05:21 AM   #7 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
/\/\ongoose is on a distinguished road
Default

I would like to get titles for the directory its in, and I would like them to be catagorized. You can kind of see what I mean in the link above in the preview of it.
/\/\ongoose is offline  
Reply With Quote
Old 12-10-2007, 05:26 AM   #8 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
/\/\ongoose is on a distinguished road
Default

Quote:
Originally Posted by Haris View Post
You can use a function called glob(). It uses a pattern to search for the required files. If all you have in that directory are images, you can use the following:

http://php.net/glob

php Code:
<?php
foreach (glob("*") as $szFilename) {
    echo '<img src="' . $szFilename . '" alt="Image" />';
}
?>

Database would be overkill for a simple thing like this which can be achieved using one function unless you want to output more information regarding the file like titles, short description, tags and much more..
That ouputs a syntax error
/\/\ongoose is offline  
Reply With Quote
Old 12-10-2007, 05:28 AM   #9 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
/\/\ongoose is on a distinguished road
Default

Ok I got it working, but it shows like 6 extra empty thumbnails when I only have three pictures in the directory? Weird...I can't wait until I am good enough at programing just to be able to know whats what and come up with my own scripts heh.
/\/\ongoose is offline  
Reply With Quote
Old 12-10-2007, 05:32 AM   #10 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by /\/\ongoose View Post
Ok I got it working, but it shows like 6 extra empty thumbnails when I only have three pictures in the directory? Weird...I can't wait until I am good enough at programing just to be able to know whats what and come up with my own scripts heh.
Do you have other files in that directory? What are images extension?
Haris is offline  
Reply With Quote
Old 12-10-2007, 05:32 AM   #11 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
/\/\ongoose is on a distinguished road
Default

Ah I got it, in after the * if you limit the type of files for it to pick up it wont show the extra files :)

I dont think this method will work though, I think a database will be the best idea. But I'm not sure having a database will make easy for updating it with a new picture. Any ideas?
/\/\ongoose is offline  
Reply With Quote
Old 12-10-2007, 05:36 AM   #12 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by /\/\ongoose View Post
Ah I got it, in after the * if you limit the type of files for it to pick up it wont show the extra files :)

I dont think this method will work though, I think a database will be the best idea. But I'm not sure having a database will make easy for updating it with a new picture. Any ideas?
Yes, it'll be easier.

You can use INSERT query on the form submission to add an image to the database then you can use SELECT query to output the images as required.

It would just need one table.

Table - images
  • id
    • int
    • Auto increment
    • Primary
  • image
    • Varchar 225
  • title
    • Varchar 225
  • manufacturer
    • Varchar 225
  • model
    • Varchar 225
  • productManufacturer
    • Varchar 225
  • product
    • Varchar 225
Haris is offline  
Reply With Quote
Old 12-10-2007, 05:37 AM   #13 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
/\/\ongoose is on a distinguished road
Default

Hm, I have only messed with a db once and it wasn't pretty. Hopefully this will turn out better lol.
/\/\ongoose is offline  
Reply With Quote
Old 12-10-2007, 05:41 AM   #14 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by /\/\ongoose View Post
Hm, I have only messed with a db once and it wasn't pretty. Hopefully this will turn out better lol.
Take a look at the database design above.



Since, you've registered at TalkPHP where we love to help, you can just ask if any problem arises.
Haris is offline  
Reply With Quote
Old 12-10-2007, 05:43 AM   #15 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
/\/\ongoose is on a distinguished road
Default

Ok cool thanks

What do you think of this tutorial? http://www.php-mysql-tutorial.com/image-gallery/

I think with that and with this websites help I should be able to pull it off :).
/\/\ongoose is offline  
Reply With Quote
Old 12-10-2007, 05:47 AM   #16 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by /\/\ongoose View Post
Ok cool thanks

What do you think of this tutorial? http://www.php-mysql-tutorial.com/image-gallery/

I think with that and with this websites help I should be able to pull it off :).
No problem in trying.
__________________
Necessity is the mother of invention.

My blog
Haris is offline  
Reply With Quote
Old 12-10-2007, 05:53 AM   #17 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 22
Thanks: 0
/\/\ongoose is on a distinguished road
Default

Well, we will see how it goes lol, thanks for the help
/\/\ongoose is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:13 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design