Displaying image on page. Is this code to simple?

Discussion in 'PHP' started by Mitchell, Jun 24, 2010.

  1. #1
    Do I need more than this to get my page to display an image?
    Are my quotes OK?
    Thanks for advise.

    
    if (file_exists("C:\upload_test\'$postid.jpg'"))
            echo "img src='C:\upload_test\$postid.jpg' border='1' align='left' />";
    
    PHP:

     
    Mitchell, Jun 24, 2010 IP
  2. .TIEU

    .TIEU Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    if (file_exists("C:\upload_test\".$postid.".jpg")){
            echo '<img src="C:\upload_test\' .$postid. ".jpg" .'" border="1" align="left" />';
    }
    
    PHP:
     
    .TIEU, Jun 24, 2010 IP
  3. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the help.

    Although I think your code is an improvement upon mine. Some of the code displays in grayed out colors in my in my code editor. When I try the code I get a parse error on the second line, the line that echos. Perhaps there is still something wrong with the quotes.
     
    Mitchell, Jun 24, 2010 IP
  4. .TIEU

    .TIEU Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It is was your slashes, I didn't quite see that.

    
    if (file_exists("C:\\upload_test\\".$postid.".jpg")){
            echo '<img src="C:\\upload_test\\' .$postid. ".jpg" .'" border="1" align="left" />';
    }
    
    PHP:
     
    .TIEU, Jun 24, 2010 IP
  5. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks. It works, sort of. I think your code is correct. The code colors display like they should in my editor. The error went away and now for the first time a broken picture icon is showing up on my web page. At leased progress is being made.

    I wonder if the folder the image is in needs to be on my servers root directory instead of C:\upload_test.

    I don't really understand why the double back slashes. I will have to read up on that.
     
    Mitchell, Jun 24, 2010 IP
  6. .TIEU

    .TIEU Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Create a folder named upload_test and put a jpeg in there named 01.jpg.

    
    <?php
    $postid = $_GET['postid'];
    
    if (file_exists("/upload_test/".$postid.".jpg")){
            echo '<img src="/upload_test/' .$postid. ".jpg" .'" border="1" align="left" />';
    }
    ?>
    
    Code (markup):
    Then go to script.php?postid=01
     
    .TIEU, Jun 24, 2010 IP
  7. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I am new at this. I don't understand your instructions. Maybe it would help if I showed you the rest of the code. $postid is a 10 digit time stamp number I gave the image upon upload. Now I am trying to display that image along with the rest of the code.
    
    <?php //new_post10.php
    	include 'npfunctions04.php';
    	include 'conn_mysqli.inc.php';
    	nukeMagicQuotes();
    	define ('MAX_FILE_SIZE', 500000);
    
    	// create short variable names	
    	if (isset($_POST['title'])) $title=sanitizeString($_POST['title']);
    	if (isset($_POST['price'])) $price=sanitizeString($_POST['price']);
    	if (isset($_POST['description'])) $description=sanitizeString($_POST['description']);
    	if (isset($_POST['email'])) $email=sanitizeString($_POST['email']);
    	
    /*get and process image*/
    	if (array_key_exists('post', $_POST)) {
    		define('UPLOAD_DIR', 'C:/upload_test/');
    		$file = $_FILES['image']['name'];
    		$file = str_replace(' ', '_', $file);
    		preg_match("/([a-zA-Z0-9_-]+).([a-zA-Z0-9]+)/", $file, $extension);
    		$postid = time();
    		$newName = $postid.".".$extension[2];
    		$max = number_format(MAX_FILE_SIZE/1024, 1).'kb';
    		$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
    		$sizeOK = false;
    		$typeOK = false;
    		
    		if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
    		$sizeOK = true;
    		}
    		
    	foreach ($permitted as $type) {
    		if ($type == $_FILES['image']['type']) {
    		 $typeOK = true;
    		 break;
    		 }
    	}
    	
    	if ($sizeOK && $typeOK) {
        switch($_FILES['image']['error']) {
    	  case 0:
            // move the file to the upload folder and rename it
    		$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$newName);
    		if ($success) {
              /*$result = "$file uploaded successfully"; --May have to remove this*/
    	      }
    		else {
    		  $result = "There was an error uploading $file. Please try again.";
    		  }
    	    break;
    	  case 3:
    		$result = "There was an error uploading $file. Please try again.";
    	  default:
            $result = "System error uploading $file. Contact webmaster.";
    	  }
        }
    	elseif ($_FILES['image']['error'] == 4) {
    		$result = 'No file selected';
    	}
    	else {
    		$result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
    	}
      }
    	if (isset($result)) {echo "<p><strong>$result</strong></p>";}
    	
    	/*price and email*/
    	$price = doubleval($price);
    	$email = validate_email($email);
    	
    	if ($email == "no"){ echo "No Email was entered<br />"; exit;}	
    		else if ($email == "invalid"){ echo "The Email address is invalid<br />"; exit;}
    			/*else{ echo "Form data successfully validated: $email.";}*/
    			else{$email = $email;}
    	
    		
    
    	if (!$title || !$price || !$description) {
    		echo "You have not entered all the required details.<br />"
    		."Please go back and try again.";
    		exit;
    	}
    
    	@ $db = new mysqli('localhost', 'mitchell', 'mpassword', 'animals06');
    
    	if (mysqli_connect_errno()) {
    	echo "Error: Could not connect to database.  Please try again later.";
    	exit;
    	}
    	
    /*Use preparred statement to input into database*/
    	$query = "insert into mammals06 (postid, title, price, description, email) values (?, ?, ?, ?, ?)";
    	$stmt = $db->stmt_init();
    	if ($stmt->prepare($query)) {
    		$stmt->bind_param('dsdss', $postid, $title, $price, $description, $email);
    		$stmt->execute();
    		}
    	$db->close();
    
    /*Use MySQLI to get info out of database to display on page*/
    $db = dbConnect('query');
    	$sql = "SELECT * FROM mammals06 WHERE postid = '$postid'";
    	$result = $db->query($sql)or die ($db->error);
    	while($row = $result->fetch_assoc()) {
    		echo 
            <<<_END
            <table>
                <tr>
                    <td>title:{$row['title']}</td>
                </tr>
    			<tr>
                    <td>price:$ {$row['price']}</td>
                </tr>
    			<tr>
                    <td>description:{$row['description']}</td>
                </tr>
    			<tr>
                    <td>email:{$row['email']}</td>
                </tr>
            </table>
    _END;
            }
    
    if (file_exists("C:\\upload_test\\".$postid.".jpg")){
            echo '<img src="C:\\upload_test\\' .$postid. ".jpg" .'" border="1" align="left" />';
    		}
    
    ?>
    
    PHP:
     
    Last edited: Jun 24, 2010
    Mitchell, Jun 24, 2010 IP
  8. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I think I understand now what you are telling me to do. The word script in this (script.php?postid=01) confused me.

    I created the code you suggested on a new php file named new_file.php and put 01.jpg in the upload test folder.

    In my browser I located this new_file.php?postid=01 on my server and it displayed a broken image icon.
     
    Mitchell, Jun 24, 2010 IP
  9. .TIEU

    .TIEU Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Maybe try changing

    echo '<img src="/upload_test/' .$postid. ".jpg" .'" border="1" align="left" />';

    to

    echo '<img src="/upload_test/' .$postid. ".jpg" .'" border="1" align="left">';

    Notice the /> at the end turns into a >


    If this fails against paste the output here. On the broken icon page, copy the whole source of the page.
     
    .TIEU, Jun 24, 2010 IP
  10. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    
    <?php
    $postid = $_GET['postid'];
    
    if (file_exists("/upload_test/".$postid.".jpg")){
            echo '<img src="/upload_test/' .$postid. ".jpg" .'" border="1" align="left">';
    }
    ?>
    
    PHP:
    The code I pasted above is for new_file.php?postid=01

    In Firefox view page source only had this.

    <img src="/upload_test/01.jpg" border="1" align="left">
     
    Last edited: Jun 24, 2010
    Mitchell, Jun 24, 2010 IP
  11. .TIEU

    .TIEU Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Now did you upload the 01.jpg into the upload_test folder?
     
    .TIEU, Jun 26, 2010 IP
  12. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #12
    Side note:

    Change:

    $postid = $_GET['postid'];
    PHP:
    Too:

    $postid = (int) $_GET['postid'];
    PHP:
     
    danx10, Jun 27, 2010 IP
  13. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanks for your ongoing efforts in helping me.
    As stated above, I did what I thought you asked me to do.

    1. I put the 01.jpg in the upload test folder.
    2. I put this code by itself in a new PHP file and named it new_file.php.
    3. I used my browser to go to this address new_file.php?postid=01
    4. All that showed was a blank page with a broken image icon.
    5. I selected view page source.
    6. A page appeared that had only this on it. <img src="/upload_test/01.jpg" border="1" align="left">
    7. Just now I realize this “/upload_test/01.jpg” is a clickable link, so I click it and this appeared.
    8.
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html>
    <head>
    <title>404 Not Found</title>
    </head>
    <body>
    <h1>Not Found</h1>
    <p>The requested URL /upload_test/01.jpg was not found on this server.</p>
    </body>
    </html>
    HTML:
     
    Mitchell, Jun 27, 2010 IP
  14. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I tried
    
    $postid = (int) $_GET['postid'];
    
    PHP:
    I get a completely blank page. I select view page source and it to is blank.
     
    Mitchell, Jun 27, 2010 IP
  15. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I tried removing border="1" align="left" , but I still get just a broken image icon. Also I reduced the size of the image down to 300 x 203, but nothing has changed. I wondered if the php.ini file setting must be set, but I tried an example out of my book that displays an image and it works fine.

    Book code.
    
    function showProfile($user)
    {
        if (file_exists("$user.jpg"))
            echo "<img src='$user.jpg' border='1' align='left' />";
    }
    
    PHP:
    My code.
    
    <?php //new_file03.php
    $postid = $_GET['postid'];
    
    if (file_exists("/upload_test/".$postid.".jpg")){
            echo '<img src="/upload_test/"'.$postid.'".jpg">';
    }
    ?>
    
    PHP:
     
    Mitchell, Jun 28, 2010 IP
  16. .TIEU

    .TIEU Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Well from the error
    <p>The requested URL /upload_test/01.jpg was not found on this server.</p>

    It means the file isn't in the right directory. Maybe try browsing to the file and seeing if it is found there. If not just try to upload it to there or change the directory

    eg: /upload_test/01.jpg ---> /01.jpg
     
    .TIEU, Jun 28, 2010 IP
  17. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Last edited: Jun 28, 2010
    Mitchell, Jun 28, 2010 IP
  18. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Solved. Removed the forward slash before "upload_test/". Thank you guys for your assistance. Special thanks to. TIEU for staying with me for so long to help me learn.

    
    <?php //new_file03.php
    $postid = $_GET['postid'];
    
    if (file_exists("upload_test/".$postid.".jpg")){
            echo '<img src="upload_test/"'.$postid.'".jpg">';
    }
    ?>
    
    PHP:

    </span></div>
     
    Mitchell, Jun 29, 2010 IP