TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   PHP and external files (http://www.talkphp.com/general/1689-php-external-files.html)

/\/\ongoose 12-10-2007 04:21 AM

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?

Village Idiot 12-10-2007 04:29 AM

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

/\/\ongoose 12-10-2007 04:43 AM

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 12-10-2007 04:54 AM

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 12-10-2007 05:13 AM

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

Haris 12-10-2007 05:15 AM

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

/\/\ongoose 12-10-2007 05:21 AM

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 12-10-2007 05:26 AM

Quote:

Originally Posted by Haris (Post 6118)
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 12-10-2007 05:28 AM

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.

Haris 12-10-2007 05:32 AM

Quote:

Originally Posted by /\/\ongoose (Post 6122)
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?

/\/\ongoose 12-10-2007 05:32 AM

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?

Haris 12-10-2007 05:36 AM

Quote:

Originally Posted by /\/\ongoose (Post 6124)
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

/\/\ongoose 12-10-2007 05:37 AM

Hm, I have only messed with a db once and it wasn't pretty. Hopefully this will turn out better lol.

Haris 12-10-2007 05:41 AM

Quote:

Originally Posted by /\/\ongoose (Post 6126)
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. :-)

/\/\ongoose 12-10-2007 05:43 AM

Ok cool thanks :-D

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

Haris 12-10-2007 05:47 AM

Quote:

Originally Posted by /\/\ongoose (Post 6128)
Ok cool thanks :-D

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

/\/\ongoose 12-10-2007 05:53 AM

Well, we will see how it goes lol, thanks for the help


All times are GMT. The time now is 08:12 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0