Setting up an image placeholder

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facarroll
Forum Newbie
Posts: 8
Joined: Tue Jul 27, 2010 6:05 pm

Setting up an image placeholder

Post by facarroll »

I have some code that allows a user to upload a small image file. It all works well, but I would like to have an image placeholder in a page for the occasions when the user chooses not to upload an image. Can anyone help? I an a bit confounded at the moment. My code follows.

Code: Select all

		//Use the users numerical id to name the image file
        $newname = $id .".jpg";
        // Set the folder to upload to.
        // Upload the file
        if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], "memberFiles/".$newname)) {
           echo "Your image has been uploaded and will be displayed on several pages.<br /><br />
            <a href=\"member_account.php\">Click here</a> to return to your profile edit area";
            exit();
Then I upload the image file like this...

Code: Select all

                  <table>
                    <form action="upload_image.php" method="post" enctype="multipart/form-data" name="form" id="form" >
                      <tr>
                        <td width="20"></td>
                        <td width="810" align="center"><div style="border:1px solid green;" align=center><img src="memberFiles/<?php echo "$id"; ?>.jpg" alt="Your School Logo"/></div><br /></td>
                        <td></td>
                      </tr>
                      
                      <tr>
                        <td></td>
                        <td><div align="center">
                            <input name="uploadedfile" type="file" />
                          </div></td>
                        <td></td>
                      </tr>
                      
                      <tr>
                        <td width="400"></td>
                        <td width="250"><div align="center"><input type="submit" name="Submit" value="Upload Image" /></div></td>
                        <td width="400"><div align="left">Return to your <br /><a href="member_account.php">Profile Page</a></div></td>
                      </tr>
                    </form>
                  </table>
Thanks.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: Setting up an image placeholder

Post by buckit »

where you display the image on the profile page... you could do something like this:

Code: Select all

$imagename = (file_exists("memberFiles/".$userid.".jpg")) ? $userid.".jpg" : "placeholder.jpg";
echo "<img src='memberFiles/".$imagename."'>;
Where $userid is the variable of the users id that needs to be set.
facarroll
Forum Newbie
Posts: 8
Joined: Tue Jul 27, 2010 6:05 pm

Re: Setting up an image placeholder

Post by facarroll »

Thanks Buckit.

Once again, I have to go to that place called work. Will probably be a day or so before I get back to you. BTW, you can see the page at http://www.safetytestingonline.com/demo ... it_pic.php

Cheers.
Post Reply