li cannot find image - php

I have an ul with a li that cannot find a .PNG image associated with the list item 'li' in the code here:
$pngFilename= 'C:/xampp/htdocs/myProj/' . 'just_a.png';
echo 'pngFilename is "', $pngFilename, '" -- that was the .png image filename.';
// within a 'ul' is this li item that displays an image (the ul code is simplified
// to only show the item that (needs to but doesn't!) display a .PNG image.)
echo '<ul>';
echo '<li>';
echo '<a href="http://localhost/myProj/just_an.htm">';
echo '<img src="', $pngFilename, '"';
echo 'alt="http://localhost/myProj/the_other.png"';
// NOTE -- I left out the close /> to the img statement when I copied my code here but
// it was in fact in my source code.
echo '/>';
echo '</a>';
echo '</li>';
echo '</ul>';
Before I wrote the above php code, I tested just the raw html and it was fine -- my .PNG file called
C:/xampp/htdocs/myProj/just_a.png was displayed correctly.
But when I switched to php server-side generation of the html, the C:/xampp/htdocs/myProj/just_a.png image does not appear, only the 'cant find it' default small image appears that looks like a piece of paper torn horizontally.
Any ideas? The .png file exists and so does the directory and the html correctly displayed the image, but when I put the html into php 'echo' calls I must be screwing something up, just not sure what.
To make sure I have the correct path and filename you'll see I echo it out at the top of the code.
The php variable $pngFilename is displayed when I do 'View Source' in the browser as:
pngFilename is "C:/xampp/htdocs/myProj/just_a.png" -- that was the .png image filename.
The only other thing to mention is that the 'alt' link, the 'alt="http://localhost/myProj/the_other.png"' -- this link (not the image) shows as blue underlined link text.
Why did this work in my html but breaks when I use the 'echo' in php? After all, the 'echo' simply sends the html to the client side -- and that .png file is 100% definitely there and displays fine when I run the above html outside of php's "echo" command.

This is because the path to the image changes depending on how your viewing the page. in a local context or from a server context; and since you are using an absolute path instead of a Relative path the system can't adjust for the change in the location of the image. Unlike when using a PHP function that calls on the php file system functions that do use the internal file system. what your doing is having it send a text file to the browser which isn't rendered as HTML code until after PHP has finished. because of that it has no access to the php file system to resolve the path to the image on the server. the way to fix it would be to use the path to the image Relative to the PHP script or use the web accessible path to the image
$pngFilename= 'C:/xampp/htdocs/myProj/' . 'just_a.png';
Should for example be
$pngFilename= 'http://localhost/myProj/' . 'just_a.png';
or if the image and the php file are in the same directory you could just do
$pngFilename= 'just_a.png';

You should try to close your img tag:
echo '<img src="', $pngFilename, '"';
echo 'alt="http://localhost/myProj/the_other.png">';

You are generating a image tag like:
<img src="C:/xampp/htdocs/myProj/just_a.png" alt="http://localhost/myProj/the_other.png" />
You cannot use this path, you need to change it to http://localhost/myProj/just_a.png or something like file:///C:/xampp/htdocs/myProj/just_a.png.

Related

CSS Floating issue with text and images

I'm using php to display image(s) and accompanying text (This is done using a WHILE loop) See below:
While (true) {
echo "
<h2>$post_title</h2>
<img src ='new_images/$post_image' width='200' height='200'/> // Image
<div>$post_content</div> "; // Text
}
I have used CSS to float the image left. Therefore,the accompanying text appears to the right of the image.
However, the second image and accompanying text is NOT sitting below the first. It's trying to force itself into any space to the right of the first image
Can I use CSS to ensure the images are stacked??
I hope that above makes sense?
add clear:both to your images and it should work just fine

Why My images are not shown in the webpage

I'm completely new to PHP and I'm writing a page which displays some images from my hard drive.
I've tried different syntaxes and none of them worked out for me ...
I've tested
echo '<img src="/var/www/netbeans/PhpProject2/Plate.jpg" alt ="Test" />';
and It didn't show anything for me .... I've tested this code Also and it didn't worked for me too, I've attached My monitor screen page below. as you can see it seems that my images are not found. I've pasted every address of my images in my browser separately and I was able to see them but when I add them in src argument of img function I'm not able to see them!
What do you think might be the source of problem .... I need to work on a project which needs to be fast enough in working with images.. what do you think I should use ? is "
Try to remove the /var/www. Path should be the http path not the file physical path.
echo '<img src="/netbeans/PhpProject2/Plate.jpg" alt ="Test" />';

Calling image in this PHP function

I am trying to call images in this function, but they all come up as question marks. Could anyone tell me what I did wrong?
if(!$currentvotes) $currentvotes = 0;
echo '<div class="vote vote'.$id.'"><span>'.$currentvotes.'</span>';
if($user_ID && !$alreadyVoted) echo '<br /><a post="'.$id.'" user="'.$user_ID.'"><img src="/images/thumbsup.png" WIDTH=25 HEIGHT=25></a>';
if($user_ID && $alreadyVoted) echo '<br /><span class="voted"><img src="/images/thumbsup.png" WIDTH=25 HEIGHT=25></span>';
echo '</div>';
if(!$user_ID) echo '<div class="signup"><p><a style="color:#4ec1f3;" href="'.get_bloginfo('url').'/wp-login.php?action=register"><img src="/images/thumbsup.png" WIDTH=25 HEIGHT=25></p></div>';
}
I am trying to call images in this function
As in the comments:
You can't 'call' an image. You can call functions, methods,
procedures, code but images are data and can't be 'called'. – Patashu
[...] they all come up as question marks
Right click on those question marks: choose "open image in new window". You should see a new page with a 404 error. Look at the link of that page: that's the path where your image is supposed to be (if you're getting a 404 error it simply means they are not there). Create the folder images and upload the file thumbsup.png. Now reload the page.
I suppose the path you are using has to be absolute eg (http://.../images/myimage.jpg) instead /images/myimage.jpg. This is common issue when you write custom code (don't use standard framework) and use .htaccess to rewrite urls.
You can set basepath as constant so you can easy change it if you switch domains.
define("BASE_PATH", "http://mydomain.com");
and then in code use it:
BASE_PATH."/images/myimage.jpg"
Or use base tag in your head in html
<base href="http://www.yourdomain" target="_blank">
And as Saturnix said in his answer you should have folder named "images" and filename you put in src.
If an image can't be found in the location you specify in your HTML, the browser will display a broken image link marker, maybe like one of these;
Chrome
IE10
Firefox 10
Each browser has its own marker. Maybe your browser uses a question mark to indicate a broken image link.

have img src = dl-main.php?f=filename.jpg retrieve said image from remote server

I am trying to do the following; dynamically pick a server with the image on it, and then show said image in img src="". Yeah I know, I am horrible at explaining stuff like this but this should clear it up:
dl-main.php (on server0.domain.com)
$url = 'http://server2.domain.com/offerimage.php?f='.$_GET["f"];
header( 'Location: '.$url ) ;
offerimage.php (on server2.domain.com)
//Lots of link-protection stuff here
$f = "/".$_GET["f"];
$url = 'http://server2.domain.com'.$uri_prefix.$m.'/'.$t_hex.$f;
echo' <img src="'.$url.'"></img> ';
dl.php (on many other servers)
img src="http://server0.domain.com/dl-main.php?f=lalala.gif"
So it pretty much goes like this: Random person adds img src directing to dl-main.php?f=filename on server0. server0 then decides which server will provide the image. In the above example I am using only one server; server2
Now I simply want dl.php to show the photo hosted on server2.domain.com .
As it stands when I directly visit dl-main.php it succesfully redirects me to dl.php, which then succesfully shows me the image I requested. But when I use dl-main.php in a img src it doesn't show the image. I didn't expect it to work but it was worth a shot, but now I don't know what to do anymore :o
I hope this failed attempt is a good example of what I'm trying to accomplish here.
Thanks!
Here's the problem. You call image from server0 using:
<img src="http://server0.whatever/dl-main.php?f=thatimage.something" />
Where the dl-main.php code redirects to server2. Here, you do:
echo' <img src="'.$url.'"></img> ';
So basically the original img tag would get another img tag instead of the image data. That's why the browser can't render the image. You should echo the content of the image instead of an img tag.
Try using your browser's developer tools and check the request to server2 to verify my guess.
It can't work, your second script (offerimage) is producing text/plain, you should produce image/...in order to use img

How to retrieve image from server and display using json parsing?

I have created a form which takes in images into a folder. I have only one image per folder and I want to display the image from the specified folder.
Say I've uploaded one picture to folder name uploads. Now I want to retrieve that image from the folder and display it.
How do I do that?
When a file is uploaded with your form, it becomes a file on your server, immediately. The web server puts it in a temporary directory and PHP tells you where it was put through the $_FILES array. You can immediately access this file, you can move it somewhere within your website's documents, and you can immediately print out an <img> tag pointing to where the file was put.
Stop writing "an img tag won't work" as that is exactly how you display the image.
Read the PHP manual page "Handling file uploads":
http://php.net/manual/en/features.file-upload.php
The PHP manual should always be the first place you go when you're trying to do something you haven't done before.
<img src="/path/to/the/upload/folder/<?php echo $filename; ?>"/>
or:
echo '<img src="/path/to/the/upload/folder/'.$filename.'"/>";
First you should have the image and path (or user) names in PHP variables. Then use an IMG tag to display them like :
<img src="some_path/uploads/<?php echo $username . '/' . $imagename; ?>">
you can't do it without knowing the name of the image file.
<?php
header('Content-Type:image/jpeg');
readfile('path_to_your/image.jpg');

Categories