Image display with <img src=""> not working - php

EDIT :: I found out that i am having a permission error. For whatever reason i dont have "permission" to grab the image from the images folder ... *
a few days ago i asked this question about uploading images and was very satisfied with the anwser. I integrated it into my site and it works no problem.
Image - Upload not responding, no access to $_FILES
The problem i am having now is i cannot retrieve the images in any way shape or form. I have tried opening them and setting them to read, with fopen($image, r). I tried even more complicated things like loading the image back into a database ... then it hit me that the anwser is simple.
I CAN see the image in my images folder, so should work no problem. But it doesnt =(
Here is the PHP script one more time,
if (isset($_FILES['fupload'] ))
{
$max_size = mysql_real_escape_string(strip_tags($_POST['MAX_FILE_SIZE']));
$file = $_FILES['fupload']['name'];
if(isset($max_size) && !empty($max_size) && !empty($file)) {
$file_type = $_FILES['fupload']['type'];
$tmp = $_FILES['fupload']['tmp_name'];
$file_size = $_FILES['fupload']['size'];
$allowed_type = array('image/png', 'image/jpg', 'image/jpeg', 'image/gif');
if(in_array($file_type, $allowed_type)) {
if($file_size < $max_size) {
$path ='images/'.$file;
$move = move_uploaded_file($tmp, $path);
$sql = "UPDATE info.profile SET Profile_pic='".$move."', pic_name='".$file."' WHERE Nick='".$_SESSION['Nick']."'";
mysql_query($sql) or die ("Error: " .mysql_error());
$_SESSION['pic'] = $path;
echo "image added successfully :) :) :) ";
}
I left out all the else statements, since they are not relevent. The folowing is an example of where the image should be displayed.
<td rowspan=2 align=center width=100px> <img src="<?php $_SESSION['pic']; ?>"
alt="Here would be the picture, if it was working !!"> </td>
The alt is a small teaser of course, but i have tried everything and with the current script i managed to get the alt text to display. Sometimes a "bad image" icon displayes (like when you open images in an email before allowing images to be displayed)
When i echo the path it shows the "right" path, just like i would enter when manualy giving it in. The reason i store the path in DB and in a session is because the image is a profile image and should only be displayed for THAT user ... wouldnt want other users having other images as their profile pic. =)

Did you cross check the file permission issue?
What is the response code return when you copy and paste the image path to the browser?
e.g. http://yourserver.com/part-to-image/image.jpg
If 404 returned, you may need to troubleshoot the server directory mapping.
If 500 returned, might be file permission.
Apart from that your code
<?php $_SESSION['pic']; ?>
Should it require echo to embed to html tag?
<?php echo $_SESSION['pic']; ?>

The path should be relative to the current directory. Have u checked it ?
Try the answer given in comment by Rohit.
<img src='<?php echo $_SESSION['pic']; ?>' />

Related

Alter profile picture using <img> tag and PHP

I'm still new on php. I'm currently building a website which a user can upload an image to change their profile picture.
The file of image will we insert into table user, row image location on database and directory "upload". The formatting is like abc.jpeg
My code for the <img> tag, which is where I display the profile image. But the image does not appear. Only the thumbnail image is displayed and not the exact image.
<img src=<?php
$current = $fgmembersite->UserEmail();
if ($handle = opendir('upload/')) {
$sql = "SELECT image_location FROM user WHERE email='$current'";
//$file = mysql_real_escape_string($sql);
if ((file_exists('upload/'.$sql) == $sql)) {
echo 'upload/'.$sql.'.png';
} else if (file_exists('upload/'.$sql)) {
echo 'upload/'.$sql.'.jpg';
}
}
closedir($handle);
?> alt="">
If you don't mind, can you check my code and see where I went wrong? Thank you for your helping.

How may Sphider be modified to display an image in search results, without knowing the name of the image

Using the code below with the Sphider search engine, Sphider will display an image in search results - if the html page and jpg share the same name (i.e) page14.html, and page14.jpg.
1) Without knowing the name of the image, how could the code be modified so that the first of 2-3 jpg's is displayed from the html page returned in search results ?
<?PHP
$url_path = "http://www.webpage.com/"; // The url to indexed pages.
$url_ext = ".html"; // The file ext. of indexed pages.
$image_path = "../"; // Path to image folder.
$image_ext = ".jpg"; // The file extension of images.
$image = str_replace("$url_path", "$image_path", $url);
$image = str_replace("$url_ext", "$image_ext", $image);
if (file_exists($image)) { // Do nothing
} else { $image = $image_path . "ICS" . $image_ext; } ?>
<img src="<?PHP print $image ?>" align="left" style="margin-right:10px;" />
Or,
2) How would I modify the above php code to display the images below in search results - without renaming all the images ?
I have many gallery pages with hundreds of the following html image links;
<img SRC="chair_0.jpg">
<img SRC="table_0.jpg">
<img SRC="bed_0.jpg">
In my case, all our webpage images have the same ID as the end of each page.
Following is an example of how our pages look like: www.mywebsite.com/category/sub-category/etc/details.php?category=6&subcategory=s&sc=180&productid=98
The pictures are located in another path, and share the same last digits (i.e 98) as web page.
Following is an example of picture folder: www.mywebsite.com/admin/picures/
In addition, all pictures have an extra "p" at the front and "_full" at end of the digits, as following example "p98_full.jpg", p109_full.jpg" or "p3_full.jpg".
Following method worked for me. You will need to past this on sphider/templates/standard/search_results.html line # 76, modify it a bit to match your setting and see if it works... (You do not need to add a url link to your site to this code, this should work as is)
<?PHP
$url_ext = ".php"; // The file extension of indexed pages.
$image_path = "../admin/pictures/"; // Path to image folder.
$pic_name = "p"; // Alphabet before image, if any.
$page_end = Substr ($url2, strrpos($url2, "=")+1); // Isolate digits or alphabets starting from end of page.
$image_ext = "_full.jpg"; // The file extension of images.
$image = $image_path . "$pic_name$page_end" . $image_ext; // Select isolated digits or alphabes.
if (file_exists($image)) { // Do nothing
} else { $image = $image_path . "ICS" . $image_ext; } ?>
<img src="<?PHP print $image ?>" alt="" align="left" style="margin-right:10px; border:0;" />

one single image url for an avatar to display random image from a folder (server-side php script)

a long time ago i once did found a php script that contained only about 6 short lines of it if i remember correctly. Yet no luck, i couldn't find anything like that anymore, so i am gonna ask for help from you.
It was only one file "rotate.php" and a folder that contained all by myself chosen images, the script was supporting different dimensions and with different extensions (jpg, png, gif) as i remember.
Script generated one single url that could be used as an image url for an avatar or between [img] tags on forums with bbcode support.
Respectively, when someone visits my forums profile or sees my comment on forums, per every visit/refresh it always shows a random image as my avatar.
There seems to be many simple variations to create a random image slideshow for my own websites logo or something, but i can't figure a way to make it work for external sites like with previously mentioned single url.
Give me some hope, thanks.
Hope it helps, you can use PHP inbuilt function called opendir($path) to open the folder in path and read the directory.
<?php
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = #opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 ); // php 4.2+ not needed
$num = array_rand($ar);
return $ar[$num];
}
$path = 'imagesfolder/';
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
?>
IN HTML:
<img src="<?php echo $path . $img; ?>" alt="" />
Ref: http://www.dyn-web.com/code/basics/random_image/random_img_php.php

Sphider with Product Images in results page - Sphider 1.3.5

I hope this can be of great help to others, it took me awhile of PHP learning, but gave me a good PHP high when I pieced it together.
For those of you who are looking to put images next to your sphider results...
I use Sphider for indexing a product folder with thousands of items, here is an example of my file structure for use with this snippet:
www/
product/
item1.php
item2.php
item3.php
images/
item1.png
item2.png
item3.png
Insert this into sphider/templates/standard/search_results.html (or sphider/templates/dark/search_results.html) in line 75 just after " class="title">
<?PHP
// Edit here
$url_path = "http:// www.yoursite.com/product/"; // The url leading to your indexed pages.
$url_ext = ".php"; // The file extension of indexed pages.
$image_path = "../images/"; // Path to image folder.
$image_ext = ".png"; // The file extension of images.
// To here
$image = str_replace("$url_path", "$image_path", $url);
$image = str_replace("$url_ext", "$image_ext", $image);
if (file_exists($image)) {
// do nothing or Your Option
} else { $image = $image_path . "ICS" . $image_ext; } ?>
<img src="<?PHP print $image ?>" alt="" align="left" style="margin-right:10px; border:0;" />
Basically, if you have a page called 1234.php in your Sphider index, this will try to find picture 1234.png to go along side it of the search result. If an image is not found it will display a default image 'ICS.png' (Image Coming Soon).
Happy coding

Errors displaying an image uploaded into pictures folder in my root folder

Please I need your help with my script. I have a page where i upload user informations with there signature into a signatures folder in the root folder using
this section of my code below:
{
$uploaddir = "uploads";
$file = str_replace(' ', '_', $_FILES
['signature']['name']);
switch($_FILES['signature']['type']) {
case "image/gif":
case "image/jpeg":
case "image/pjpeg":
case "image/png":
$uploaddir .= 'signatures';
break;
}
foreach($_FILES as $signature)
$tmp_name = $signature['tmp_name'];
$type = $signature['type'];
$name = $signature['name'];
$size = $signature['size'];
if (move_uploaded_file($_FILES
['signature']['tmp_name'], $uploaddir. $file)) {
$query = "INSERT INTO users (users_id, tmp_name, type, name, size)
VALUES (NULL, '$tmp_name', '$type' , '$name' , '$size')"
mysql_query($query) or
die (mysql_error());
}
This is the query i'm using to call the name of the file i want.
$query10 = mysql_query("SELECT name
FROM users
WHERE username = '".$_SESSION ['logged_username']."'") or
die (mysql_error());
$signature = mysql_fetch_array ($query10);
How i'm trying to show the signature i uploaded with a php script.
echo "<img src = \"uploads\""?><?php echo $signature['name'] ;?>"/<? php "width=\"50\" height=\"100\">" ;
The picture is not showing, although it showed a small box with nothing inside it. I'm quite sure the picture was uploaded into the signature folder of my root.
Please forgive my formatting. I meant no disprespect to this community.
I'll be most delighted with your comments, answers and suggestions.
Thank you in advance.
Well the most obvious reason seems to be that you are reading the images from a different path than where you are storing them. As you have mentioned, you are saving images in the "signatures" folder but you are reading from "uploads" folder instead. Try this echo:
echo '<img src = "signatures"' . $signature['name'] . ' width="50" height="100">';
If the above still doesn't show the image, it needs some debugging.
In the PHP code, there are 2 likely destinations for storing images - "uploads" and "signatures" folders. Please ensure into which folder are the images saving. Perhaps you should first check in which path does the image exists (using file_exists) and then accordingly decide which path to use as source.
Please ensure that the images are actually uploading
By the way, is there a specific reason for escaping the \ (back-slash) in your PHP code:
$uploaddir = "uploads\\";
$uploaddir = "signatures\\";
Hope above helps!
Replace code for displaying image with below:
$uploadpath="uploads/";
echo "<img src ='".$uploadpath.$signature['name']."' width='50' height='100'>" ;
$uploadpath="uploads/signatures/";
echo "<img src =".$uploadpath. $signature['name']." width=\"50\" height = \"100"\>" ;

Categories