craigeves Posted November 12, 2008 Share Posted November 12, 2008 I wondered if any of you PHP Freaks could help me? I want to create a really really simple script where if i type 'hello' into a text field and hit submit it shows 'hello.jpg' in a page window. Likewise i could type 'php' and it would display 'php.jpg' in a page window. Basically whatever i type in the text field the PHP script adds '.jpg' at the end of it, searches a predefined folder on the server and displays it. I don't know where to start. Does anyone have a script or offer advise? I don't know the first thing about PHP. Many thanks Craig Quote Link to comment Share on other sites More sharing options...
foxtrotwhiskey Posted November 12, 2008 Share Posted November 12, 2008 Hi Craig, One way to do this is to create a script that both displays a form to be filled out and shows the image, these can be seperated though if you want. When submitted it will add the image name to a URL paramter and the same script detects if that parameter is there, if it is it will output the HTML for the image, otherwise it will output the form. Take a look at the example below. <?php if (!isset($_GET['image'])) { //Show form ?> <form action="" method="GET"> <input type="text" name="image" /> <input type="submit" value="Submit" /> </form> <?php } else { //Show picture echo '<img src="'.$_GET['image'].'" alt="" />'; } ?> Let me know if you have any questions about it. Quote Link to comment Share on other sites More sharing options...
craigeves Posted November 12, 2008 Author Share Posted November 12, 2008 <?php if (!isset($_GET['image'])) { //Show form ?> <form action="" method="GET"> <input type="text" name="image" /> <input type="submit" value="Submit" /> </form> <?php } else { //Show picture echo '<img src="'.$_GET['image'].'" alt="" />'; } ?> Thanks so much for your help... managed to get it working with a little bit of tinkering.... Thanks again! Quote Link to comment Share on other sites More sharing options...
craigeves Posted November 12, 2008 Author Share Posted November 12, 2008 I also forgot to mention previously, that if an image with that name cannot be found how do i get it to say 'cannot be found, please try again' with another input box underneath it? Thanks again! you are a star! Hi Craig, One way to do this is to create a script that both displays a form to be filled out and shows the image, these can be seperated though if you want. When submitted it will add the image name to a URL paramter and the same script detects if that parameter is there, if it is it will output the HTML for the image, otherwise it will output the form. Take a look at the example below. <?php if (!isset($_GET['image'])) { //Show form ?> <form action="" method="GET"> <input type="text" name="image" /> <input type="submit" value="Submit" /> </form> <?php } else { //Show picture echo '<img src="'.$_GET['image'].'" alt="" />'; } ?> Let me know if you have any questions about it. Quote Link to comment Share on other sites More sharing options...
foxtrotwhiskey Posted November 12, 2008 Share Posted November 12, 2008 Well PHP has a function called file_exists(). You can put the filepath of the image through that to check if it exists, and then handle any error messages accordingly. Quote Link to comment Share on other sites More sharing options...
craigeves Posted November 12, 2008 Author Share Posted November 12, 2008 Well PHP has a function called file_exists(). You can put the filepath of the image through that to check if it exists, and then handle any error messages accordingly. I think im going wrong somewhere... here is my code but it's breaking... <?php if (!isset($_GET['image'])) { //Show form ?> <form action="" method="GET"> <input type="text" name="image" /> <input type="submit" value="Submit" /> </form> <?php $filename = "/2009/proofs/'.$_GET['image'].'.jpg"; if (file_exists($filename)) { //Show picture echo '<img src="/2009/proofs/'.$_GET['image'].'.jpg" alt="" />'; } else { echo "not here"; } ?> Can you help any further? Thanks in advance Quote Link to comment Share on other sites More sharing options...
foxtrotwhiskey Posted November 12, 2008 Share Posted November 12, 2008 The first "if" statement does not have a closing }. Here I've rearranged it for you. Just let me know if you have any questions <?php $image = ''; $found = false; //Check if image was set if (isset($_GET['image'])) { $image = $_GET['image']; $filename = "/2009/proofs/'.$image.'.jpg"; if (file_exists($filename)) $found = true; } if ($found) { //Show picture echo '<img src="/2009/proofs/'.$_GET['image'].'.jpg" alt="" />'; } else { if ($image) //The image was specified but it was not found echo "not here"; //Show form ?> <form action="" method="GET"> <input type="text" name="image" /> <input type="submit" value="Submit" /> </form> <?php } Quote Link to comment Share on other sites More sharing options...
craigeves Posted November 12, 2008 Author Share Posted November 12, 2008 I have tried this but it doesn't seem to work. Even when the image does exist it still states 'not here' and doesn't show the image... The first "if" statement does not have a closing }. Here I've rearranged it for you. Just let me know if you have any questions <?php $image = ''; $found = false; //Check if image was set if (isset($_GET['image'])) { $image = $_GET['image']; $filename = "/2009/proofs/'.$image.'.jpg"; if (file_exists($filename)) $found = true; } if ($found) { //Show picture echo '<img src="/2009/proofs/'.$_GET['image'].'.jpg" alt="" />'; } else { if ($image) //The image was specified but it was not found echo "not here"; //Show form ?> <form action="" method="GET"> <input type="text" name="image" /> <input type="submit" value="Submit" /> </form> <?php } Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 12, 2008 Share Posted November 12, 2008 Eh, that's kind of messy. =/ How about: <?php if (isset($_GET['image'])) { $path = "/2009/proofs/{$_GET['image']}.jpg"; if (file_exists($path)) { //we're good printf('<div><img src="%s" alt="" /></div>', htmlentities($path, ENT_QUOTES)); } else { echo "<p>Image could not be found, please try again.</p>"; } } //redisplay form anyway since it's easier than having to go back every time ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get"> <p><input type="text" name="image" /></p> <p><input type="submit" value="Submit" /></p> </form> Quote Link to comment Share on other sites More sharing options...
runnerjp Posted November 12, 2008 Share Posted November 12, 2008 hey pal... iv gone back to ur code you used and chnaged it so no errors will be displayed! <?php if (!isset($_GET['image'])) { //Show form ?> <form action="" method="GET"> <input type="text" name="image" /> <input type="submit" value="Submit" /> </form> <?php $filename = "/2009/proofs/'".$_GET['image']."'.jpg"; // needed chnaging to '".$_GET['image']."' if (file_exists($filename)) { //Show picture echo '<img src="/2009/proofs/'.$_GET['image'].'.jpg" alt="" />'; } else { echo "not here"; } }// didnt end you {}'s... ?> Quote Link to comment Share on other sites More sharing options...
craigeves Posted November 12, 2008 Author Share Posted November 12, 2008 Sorry guys... neither of those seem to work either.... Quote Link to comment Share on other sites More sharing options...
DarkWater Posted November 12, 2008 Share Posted November 12, 2008 Are you sure /2009/proofs is the correct directory? Can I see your current directory structure? Quote Link to comment Share on other sites More sharing options...
craigeves Posted November 12, 2008 Author Share Posted November 12, 2008 I'll email / PM you with it.... Thanks! 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.