...

View Full Version : creating links to image files automatically



Jacki
08-30-2006, 03:29 PM
Good Morning All..

Hopefully someone here can help me because I am at a loss as to what to do...

I was told I need to do this with PHP but I have no idea how or where to begin.. I need some kind of explanation or tutorial "PHP for idiots"

Here is what I would like to do... I create images and want to be able to post them on my website.. But I want to post the links to the images... Not the images themselves.... On one directory in my file manager.. I have just about 200 images... How would I go about adding a link to each of those images to my website without having to individually copy and paste the link...

I would do image galleries or thumbnail views but they take a while to load.. so creating them as links would be better.... But I have no idea how to go about this.... Hopefully someone here can help... I've wasted 3 days trying to find out how to do this and got no where... I just hope I don't end up having to go about copying and pasting the links anyway lol... Because I have several directories to do this for

Also please keep in mind I am a PHP idiot... so hopefully someone can explain it in idiot terms lololol

Thanks!
Jacki

Fumigator
08-30-2006, 04:23 PM
You can use the glob() function to create an array of filenames from a directory and then echo html mark up from that array. Since you're new to PHP here is the syntax:



<?php
$images = glob("yourdir/*.jpg");
foreach($images as $fileName) {
print "<a href=\"yourdir/$filename\">$filename</a><br />\n";
}
?>

Spookster
08-30-2006, 04:34 PM
It's seems obvious you chose not to read our posting guidelines even though we've stated just about everywhere to do so before posting. In the future, please use a more descriptive subject when posting a question. See posting guidelines. (http://www.codingforums.com/postguide.htm)

Jacki
08-30-2006, 04:44 PM
My apologies Spookster!.. It's not that I chose not to read the posting guidelines... I only joined a couple of hours ago so I am still reading through things... And getting to know who things work here... Since I am also new to PHP I really have no idea of the "technical term" for what I need help with... I will keep my post subject more specific.... My apologies....

Thank you Fumigator for the code.. Looks like a simple enough code.... Where do I put it?? LOL... Does it go inside with the HTML codes for the particular page where I want the images to go? Like I have a folder called "Angels" Would I place that code in the "index.htm" file I have in this directory?

Thanks so much for the help! (and patience)

Jacki

Fumigator
08-30-2006, 05:58 PM
You imbed php into any html file. Go through this tutorial here (http://us2.php.net/manual/en/tutorial.firstpage.php) to get the hang of it.

Jacki
08-30-2006, 06:44 PM
Oh Lordy.. Out comes the idiot in me!!!

I started out on a blank webpage... Added the code you posted.. And I KNOW there has got to be something I need to change in the code.. But not sure what because after I put the code inside the HTML file... All I get is this

http://wickdimpressions.com/TestFolder/new_page_1.htm

It's a blank page that says $filename and some symbols... no idea what they mean!! LOL.. Yeah I know I'm not bright... Lack common sense (sometimes)...

What variables do I need to change within the code to get it to work?

Thanks!!!
Jacki

rafiki
08-30-2006, 06:52 PM
yourdir needs to be the directory of your images for example "/images/*.jpg"
or change the jpg to the extension of your images

Jacki
08-30-2006, 07:17 PM
Do you mean the full path? like wickdimpressions.com/images or just images/?

I'm going to try again... Thanks rafiki!

Jacki

Jacki
08-30-2006, 07:58 PM
I tried.. I put in the location of the folder where the images are stored as wickdimpressions.com/TestFolder/images and also just as TestFolder/images

and when I click to view the page all I get is.........
$filename
\n"; } ?>

Is there something else I need to change within the code? I keep getting this same thing on the page every time I try to view


Thanks!!

Jacki

Spookster
08-30-2006, 08:19 PM
If you want to run PHP in your page you need to use a .php file extension. Change your file name from new_page_1.htm to new_page_1.php

Jacki
08-30-2006, 08:40 PM
Thanks Spookster!

Apparently I am more of an idiot that I first thought... I did as you said... Now the page is completely blank

http://wickdimpressions.com/TestFolder/phptestpage.php

Got rid of the error... But not sure what I should do now?

Does the HTML codes get added to this page.. ?

I'm confused (again)

Sorry



Jacki

Fumigator
08-30-2006, 08:46 PM
"yourdir/*.jpg" should be the relative path to the directory you want to list. You are running phptestpage.php out of TestFolder-- where is your images directory in relation to that? If it is TestFolder/images then you just want to use "images/*.jpg".

Jacki
08-30-2006, 09:51 PM
"Test Folder" Is the folder the images are in so I did this code


<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$images = glob("TestFolder\*.jpg");
foreach($images as $fileName) {
print "<a href=\"TestFolder\$filename\">$filename</a><br />\n";
}
?>
</body>
</html>

There are no subdirectories within "TestFolder"... I've been building webpages for just about 3 years now... Nothing professional or anything.. Just for friends, family. But I've never dealt with PHP, CGI, ASP etc... I thought since I knew HTML that learning PHP would be a breeze.. I was wrong LOL.... Well I can create forms with PHP.. but not this...

With this PHP code.. Is this the ONLY code I need to know or have to do what I need it to do? Do I have to create other pages besides the PHP file with the code? Can I add HTML into the PHP file?

Something is still wrong.. It just wont work for me!
I appreciate the help though, and you being so patient with me..

I've been reading tons of info on PHP, haven't slept in just over 24 hours now... ANd this one code just won't work... I know there is something I'm missing or just not doing right but can't put my finger on it!

Thanks again for the help (hopefully I can get this to work some time soon... I've spent 3 days trying to figure this out... and haven't done any work in that time.. Luckily I can't fire myself lol) If I can't figure this out soon I may just need to get started on adding the links one by one.. boring!



Jacki

arnyinc
08-30-2006, 10:22 PM
It looks like it is just a case-sensitive typo. Change $fileName to $filename.

If that still doesn't work, we'll look at the path more closely but it appears to be correct at first glance.



<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$images = glob("TestFolder\*.jpg");
foreach($images as $filename) {
print "<a href=\"TestFolder\$filename\">$filename</a><br />\n";
}
?>
</body>
</html>

Fumigator
08-30-2006, 10:23 PM
If your images are in the same folder the php file is in then don't even specify a folder-- just use "*.jpg". That is assuming the images all have a jpg extension.

Jacki
08-30-2006, 11:17 PM
Thank you Fumigator! I'm getting close I can feel it!


The .jpg's are in the same folder "TesTFolder" so I removed the directory name from the PHP code.... I save the file and opened the page to view still nothing.. but when I clicked view source this is what i got..

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<a href="$filename"></a><br />
<a href="$filename"></a><br />
<a href="$filename"></a><br />
<a href="$filename"></a><br />

</body>
</html>


ANd this is the code I have in the file

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
$images = glob("*.jpg");
foreach($images as $fileName) {
print "<a href=\"\$filename\">$filename</a><br />\n";
}
?>
</body>
</html>

Getting so close I'm excited!.. Or at least I HOPE I'm getting close lol.

Thanks for all the help everyone!



Jacki

Fumigator
08-30-2006, 11:53 PM
Meh you caught my typo looks like... $filename and $fileName are different variables. Change $filename to $fileName.

Jacki
08-31-2006, 12:09 AM
Getting so much closer lol.. This time the links DID show on the page.. BUT... WHen you click them you get "page can not be found"?

http://wickdimpressions.com/TestFolder/phptestpage.php

I know the directory name is correct... and I didn't remove the files from the directory.. What could cause this?

Thank again!

Jacki

Jacki
08-31-2006, 12:23 AM
Wooooo hooooo!!!!!
It works
it works
it works...

Oh did I mention it works!!!! LOL


I went back into the file and looked... There are 3 places where it says $fileName... and I didn't change the one.......

Now with this PHP file.. can I add standard HTML to it?.. Like backgrounds, banners etc? or does the html need to be done in a separate file and if so.. How do I add the PHP to HTMP file?

Thanks SO much ... This little code right here is going to save me HOURS of time when it comes time in the future to adding more or updating my lists.. Oh one more question... When I add a new image to say "TestFolder" will that automatically gget added to the web page?

I think I can finally get some sleep... Well over 24 hours of no sleep... I'm so excited I think I'll start on my website!

Thanks !!!!!!!!



Jacki

googleit
08-31-2006, 12:38 AM
normal html can be added to the php file just make sure you dont put it in the <?php ?> tags
good luck :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum