Krasik Posted February 25, 2011 Share Posted February 25, 2011 For starters. I'm relatively new to PHP programming. I've had minor VB and I have SOME understanding of programming in general. I was trying to find this solution myself without consulting anyone but with PHP5 and it's deprecated functions. I can't just paste code from the net and it work. Currently I have PHP 5.3.5 installed. I'm running on the newest Apache and MySQL builds. Now to the part I want to get to work. I'm trying this just because I can. Basically. I would like php to scan a certain path and filter out the images. I have a script that does that fine. What I want to do is take the file names, append my host address and turn it into a clickable link. I also have a script that will turn text into URL's. I will paste the snippets I have. I was initially trying to use eregi_replace() but apparently that's been deprecated and I believe I should now use FileInfo() or something like that. Not quite sure what they do but am really wanting to learn. Thanks for any help you guys can offer. <?php $path = 'http://www.krasikart.no-ip.org'; $dir=$_SERVER['DOCUMENT_ROOT']."/htdocs/pics/"; $total = ''; // Retrieve all the images. $files = glob ("$dir{*.jpg,*.jpeg,*.gif}", GLOB_BRACE); // Confirm that matches were made. if (is_array($files)) { // Loop through the array. foreach ($files as $image) { $total = $path . $image; print "$total \n"; // The Regular Expression filter $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; // The Text you want to filter for urls $text = "$total"; // Check if there is a url in the text if(preg_match($reg_exUrl, $text, $url)) { // make the urls hyper links echo preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text); } else { // if no urls in the text just return the text } } } else { echo 'No files matched the search pattern!'; } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted February 25, 2011 Share Posted February 25, 2011 You say you have some programming knowledge. Take a step back and look at your logic. You are over complicating things. You are checking your paths for a url... why? You know its a url because you have pre-pended your domain name to the head of the string. You don't need any of that, get rid of it. Quote Link to comment Share on other sites More sharing options...
Krasik Posted February 25, 2011 Author Share Posted February 25, 2011 Well, in order for the parser to work, it must find text that match URLs. I can only do that if I append my host address. I don't know any other way at this time. If i just parse for images, I get ./pics/testimgae.jpg. I don't get the full domain name path. I believe. I suppose I don't know how to append the images to my host name properly. I know the code is messy. I would like to know the most efficient means to accomplish this. Quote Link to comment Share on other sites More sharing options...
Krasik Posted February 25, 2011 Author Share Posted February 25, 2011 I think I misread your question. I have to append the url in oder for the script to recognize that it's a URL. I'm wanting this script for auto link perposes. I can just place an image in the directory and it automatically turns it into a link without me having to html code it. Just retrieving the image path isn't enough to be recognized as a URL. I hope that sheds more light on it. Thanks. Quote Link to comment Share on other sites More sharing options...
trq Posted February 26, 2011 Share Posted February 26, 2011 Just retrieving the image path isn't enough to be recognized as a URL. Um, indeed it is. Quote Link to comment Share on other sites More sharing options...
trq Posted February 26, 2011 Share Posted February 26, 2011 <?php $dir = $_SERVER['DOCUMENT_ROOT']."/htdocs/pics/"; $files = glob("$dir{*.jpg,*.jpeg,*.gif}", GLOB_BRACE); if (is_array($files)) { foreach ($files as $image) { echo "<a href='pics/$image' rel='nofollow'>$image</a>"; } Your path within $dir doesn't look right to me, but this example should get you started. Quote Link to comment Share on other sites More sharing options...
Krasik Posted February 26, 2011 Author Share Posted February 26, 2011 Ok, I've slightly modified the code but now it shows direct system paths, not urls. It out puts this: C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/pics/boxes2.jpg C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/pics/boxes3.jpg C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/pics/motherE.jpg C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/pics/motherE2.jpg I would like for it to show http://www.krasikart.no-ip.org/boxes3.jpg etc. Any suggestions? Quote Link to comment Share on other sites More sharing options...
trq Posted February 26, 2011 Share Posted February 26, 2011 <?php $dir = $_SERVER['DOCUMENT_ROOT']."/htdocs/pics/"; $files = glob("$dir{*.jpg,*.jpeg,*.gif}", GLOB_BRACE); if (is_array($files)) { foreach ($files as $image) { $image = basename($image); echo "<a href='pics/$image' rel='nofollow'>$image</a>"; } } Quote Link to comment Share on other sites More sharing options...
Krasik Posted February 26, 2011 Author Share Posted February 26, 2011 The link address is http://174.51.225.164:8080/pics/C:/Program%20Files/Apache%20Software%20Foundation/Apache2.2/htdocs/pics/boxes2.jpg. How can I fix this? Thanks. Quote Link to comment Share on other sites More sharing options...
trq Posted February 26, 2011 Share Posted February 26, 2011 See previous post. Quote Link to comment Share on other sites More sharing options...
Krasik Posted February 26, 2011 Author Share Posted February 26, 2011 Thank you Thorpe! Quote Link to comment Share on other sites More sharing options...
trq Posted February 26, 2011 Share Posted February 26, 2011 Told you you where over complicating things Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.