Jump to content

A really simple PHP Script.... Help Please!


craigeves

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.