Why My images are not shown in the webpage - php

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" />';

Related

How to display photo from non-public folder?

I have built my website in a way so that the actual database files are positioned as follows
root/
root/httpdocs <- public folder
root/databasefolder/actual_files_here/some_photo.jpg
How can I implement so that the php can view/load a photo from that databasefolder and echo it with tag. I simply tried "../databasefolder/" in a tag, but it doesn't work (of course it doesn't), since it's from hidden folder.
I tried Base64 which works in other browsers, but not with Internet Explorer, due to that silly kb limit.
In your HTML:
<img src="image.php?image=some_photo.jpg" />
And a php script that echoes your image.
header('Content-type: image/jpeg');
echo imagejpeg('root/databasefolder/actual_files_here/' . $_GET['image']);
You can use the get parameter "image" to load whatever image you need from that folder.

Html image source from a local folder on windows options

On my html page, I make use of images which are housed in a local folder. The paths are coming directly from the database field. How can I do this? I know it wont work if the images are not in the web root directory. Can symlinks work?
For example,
The physical path to the images is c:/Images and the database field will contain the path like this, photo/image1.jpg
I will fetch the image source path from php as shown below,
<img id="image1" src="<?php echo $this->object->imagePath; ?>" class="img-polaroid">
$this->object->imagePath here will be the string concatenation of c:/Images and photo/image1.jpg. So, putting it together t will be c:/Images/photo/image1.jpg
The problem is it does not show up. I have tried this as well to test,
<img id="image1" src="file:///C:/Images/photo/image1.jpg" class="img-polaroid">
But no luck.
first let me know if putting just C:/Images/photo/image1.jpg in a browser address bar brings an image or not. If not then that's mean you are giving wrong path. Let me know so i can edit this answer. But for now this is the answer i hope.
Symlink to the folder worked like a charm.

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.

Getting image from folder using PHP: Code working and getting image path but img not being placed into HTML

I've been looking all over the place but can't seem to find an issue quite like mine. Maybe I haven't been putting together the right keywords; but perhaps I'll have more luck just explaining my issue.
I've put together the following little code:
$image_types = array('gif','png','jpg');
$recent = array();
foreach($image_types as $image_type) {
foreach(glob('first/second/*.'.$image_type) as $filename){
$recent[$filename] = time() - filemtime($filename);
}
}
$recent = array_keys($recent, min($recent));
$recent_img = $recent[0];
What it's supposed to do is look into a specific folder and find the latest image and, later, place it into the webpage. What it does do is find the latest image, path and all. So everything is working up to there. If I echo $recent_img I get the latest image's src, so obviously I'd want to place the following code right after: <img src="<?php echo $recent_img ?>" /> and I should get the image. Surely enough, everything works fine, even up to the point locating within the HTML an <img> that refers to the image I need. HOWEVER, there's something going haywire at this point!
If glob() is set to the path first alone, the code works entirely: the image is shown in the website. But with the second folder added, it does everything except show the image on the website.
So I go to the result website and check the source code and the image code is as follows:
<img src="first/second/img.jpg" style="display: none !important; visibility: hidden !important; opacity: 0 !important; background-position: 0px 0px;" width="0" height="0">
Notice the display:none!important the visibility:hidden!important etc., almost like the CSS is set to do everything it can to avoid showing the image. Including setting the width and height to 0 when the image is obviously not 0 x 0 pixs. (Just in case: nor is there any instance in my style sheet which would result in setting everything to 0 and hidden and etc. on any image in my project.)
However, as I said, this does not happen when the path only has the one part, the folder first. I've tried putting all the images from one folder into the other and still the issue remains. I've edited the CSS values on the spot in Chrome and still nothing.
It seems like an absurd issue to have because nothing is changing except the code is looking for a file inside of a folder that is inside a parent folder. Moreover because the code actually works to the point of showing the file I need and its path but not the file itself.
Online trial example for you guys to see and TEMP solution or, rather, 'workaround'.
I hate working around an issue. It usually complicates things and/or makes them messy. First of all, in this case it is an organization issue. It's much better to have ALL images in one folder and then make sub-folders for different kinds. Having many image folders floating about the root is not a good idea to me. Second, it seems absurd to have to use a 'workaround' when there seems no logic in the error. Third, I really would like to learn what is going on and not turn this into a PHP Bermuda Triangle. So please don't take my TEMP solution as me giving up, and please continue the synergy of this wonderful place, where no doubt at some point we'll arrive to a solution.
At any rate, the 'workaround' you've probably already guessed is to place the second folder in the same directory as the first folder, which is the root. So this should solve that specific issue. But it's not a genial solution if you ask me.
I've been asked to upload an online trial of the issue and I've done so >>HERE<<. You'll be able to see the same code looking for the latest image in first/second/ and in first/, and you'll see that one works marvelously where the other mysteriously flops!
QUESTION: Just occurred to me. Could it have something to do with the length of the path name? I.e. first/ is O.K. but first/second/ is too long? Doesn't make much sense taking into consideration that the retrieval of info from the path is successful, and the issue is rather on the side of the HTML outcome, but I have since tried placing the second folder in the root and gave it a 10-letter name and the issue was recreated. This, however, only happened in WAMP localhost, as when I uploaded everything online, the length didn't seem to matter. However, even online, as you can see from my LINK the first issue remains.
You can try retrieve the src and update using jquery:
$.get('image.php').success(function(src) {
$('#myimg').attr('src',src);
});

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

Categories