php function wont display images - php

I cannot get images to show using php function. All permissions have been granted and image location is correct but get this ..see picture
index.php file
<?php
include 'inc/function.inc.php';
include 'header.php';
fetchProfile($conn);
?>
<script src="assets/js/bootstrap.js"></script>
</body>
</html>
function.inc.php file
function fetchProfile($conn){
if ($rowImg['status'] == 0) {
$username = $row['uid'];
$image = '../assets/img/profile.png';
echo "<img src='".$image."' style='width:120px;height:120px' class='img-responsive' alt=''>
<div class='captionN'>
<h4>" . $username . "</h4></div>;

Solved ->
When operating from functions in a different file, the path will remain the location from the index.php and not from the function.inc.php's location.
index.php file
<?php
include 'inc/function.inc.php';
include 'header.php';
fetchProfile($conn);
?>
<script src="assets/js/bootstrap.js"></script>
</body>
</html>
function.inc.php file
function fetchProfile($conn){
if ($rowImg['status'] == 0) {
$username = $row['uid'];
$image = 'assets/img/profile.png';
echo "<img src='".$image."' style='width:120px;height:120px' class='img-responsive' alt=''>
<div class='captionN'>
<h4>" . $username . "</h4></div>;

The source of the image is ../assets/img/profile.png, if the file index.php is at the root of your site it is normal that the image doesn't appear because you are trying to fetch a file that is outside your site.
The problem could be that the folder assets is outside of your site or that your path is not correct.

Related

Basic Image display from Database in PHP

I am learning the most basic of PHP MYSQL and am trying to display and image on my page along with Name, etc.
In the database I made a path: images/Paul.jpg alongside his first name and last name
Everything display as expected except for image. I have tried many iterations of the code but it either errors or displays a placeholder with no image or even on inspection doesnt show a path to the image directory.
Any thoughts?
<?php echo $data['first_name']; ?>
<?php echo $data['last_name']; ?>
<?php echo $data['age']; ?>
<?php echo '<img src="['image']" />'; ?>
<?php echo $data['bio']; ?>
<?php
}
?>
<!-- </table> -->
<?php mysqli_close($db); // Close connection ?>
If you have the path in your database, then you should use the path in the src attribute of your img
<?php echo '<img src="' . $data['image'] . '" />'; ?>
You also need to make sure the directory with your image is at the same level as this file to display correctly or you'll get an incorrect path.

Getting Images With PHP And Display In Script

I have a blog and I want to be able to show images that are stored in a folder using a simple PHP script. Here is the PHP:
<?php
$folder = "$segment_url/files/photos/$post_year/$post_id/";
$filetype = ".jpg";
$files = glob($folder.$filetype, GLOB_BRACE);
foreach ($files as $file)
{
echo "
<img class=\"galleryPhoto\" src=\"$file\" /> // PROBLEM 1
";
}
?>
<?php echo $folder; ?>
<?php
$display_content = "
<div class=\"pageSection text\">
$content_intro
</div>
<div class=\"contentSpace\"></div>
<div class=\"pageSection text\">
// PROBLEM 2
</div>
<div class=\"contentSpace\"></div>
<div class=\"pageSection text\">
$content_conclusion
</div>
";
?>
In reference to the above code:
Problem 1: Even though the path to the images folder is correct, no images are being displayed.
Problem 2: How do I get the echoed content to be displayed here rather than at the top of the page?
For Problem 1 : You are missing to use full image path, otherwise search will search in current directory. Try like this.
<img class=\"galleryPhoto\" src=\"$folder/$file\" /> // PROBLEM 1

Need help echoing an include that is linked to another echo

I have a 3 php files where i am trying to echo an include from a page.php to template.php but the include is another php file (Slideshow.php) it works but is positioned in the wrong place
The result needs to be like this:
http://st-margarets-barking.org.uk/SchoolDay.php
the codes i have are:
template.php:
<body>
<?php echo $slideshow; ?>
</body>
page.php:
<?php
$content = "content";
$slideshow = include("Slideshow.php");
$template = "template.php";
require($template);
?>
Slideshow.php:
<?php echo "<img src='Images/slideshow-frame.png' style='position:relative; top:-20px;'>
<div class='cycle-slideshow slideshow-width' data-cycle-speed='500' data-cycle-timeout='5000'>
<img src=Images/slides/041214%20Assembly%20(5).png>
<img src=Images/slides/Evacuation-Day-001.png>
<img src=Images/slides/Trewern-002.png>
<img src=Images/slides/church-silhouette.png>
<img src=Images/slides/Evacuation-Day-003.png>
</div>";
?>
The result is this:
http://st-margarets-barking.org.uk/SchoolDay0.php
I have tried:
page.php
$slideshow = "<?php include('Slideshow.php');";
However this shows a server error
I can do this:
template.php:
<?php include("Slideshow.php"); ?>
This works, but then I have to create more template.php as well as more Slideshows.php, this is not what I want to do
the point of this is to have different slideshow.php files so I can change the slideshow in the page.php, then the template.php will echo the slideshow and all other content on the page.php, this will save me time of having to change the slideshow images on each page.php
can anyone show me what I am doing wrong?
If you are trying to display the content of Slideshow.php inside template.php, you have to change it like this:
<?php
function images() {
$str = "<img src='Images/slideshow-frame.png' style='position:relative; top:-20px;'>
<div class='cycle-slideshow slideshow-width' data-cycle-speed='500' data-cycle-timeout='5000'>
<img src=Images/slides/041214%20Assembly%20(5).png>
<img src=Images/slides/Evacuation-Day-001.png>
<img src=Images/slides/Trewern-002.png>
<img src=Images/slides/church-silhouette.png>
<img src=Images/slides/Evacuation-Day-003.png>
</div>";
return $str;
}
?>
Then call the method inside page.php like:
include("Slideshow.php");
$slideshow = images();

PHP header function changes directory

So I am trying to practice PHP and I am stuck with using headers. I am using xampp in doing this. I have a simple form wherein the user will log in in the "index.php" now when the log in is successful the header function will start and redirect the page to "includes/profile.php". Now here is the problem, after the header I am currently in "includes" folder so when I use other .php or .css files outside includes folder i need to do "../example/example.php". This mess up my paths because my CSS file is in "css/example.css" so i need to put "../". Is there any way to always make the "pointer" go back to the parent directory even after using header so that i dont need to use "../"?
index.php
<?php
session_start();
if(isset($_SESSION['username'])&& isset($_SESSION['password']))
{header("Location: includes/profile.php");exit;}
if (isset($_POST['submit'])) {
if($_POST['username']=="junji" && $_POST['password']=="secret"){
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['expiry']=time();
$username = $_SESSION['username'];
$password = $_SESSION['password'];
header("Location:includes/profile.php");
exit;
}
else{
$username = "";
$password = "";
session_destroy();
}
}
?>
//end of index.php
now inside the profile.php
<?php
session_start();
if(isset($_SESSION['username'])&& isset($_SESSION['password']))
{if(time()-$_SESSION['expiry']<5){?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php include_once('head.php');
?>
</head>
<body>
<div class="container">
<h1>Junji's Bio</h1>
<div class="row">
<div class="col-xs-8">
<h2>Content</h2>
<?php
include_once ('content.php');?>
</div>
<form method="POST">
<div class="col-xs-4 sidebar">
<div class="alert alert-success"> <?php print "You are currenly logged in as ";
print '<br><h3>';
print $_SESSION['username'];print "\t".'<input type="submit" class="btn btn-info" name="submit" value="Logout">'; ?></h3> </div>
</div></form>
</div>
</div>
</body>
</html>
<?php if(isset($_POST['submit'])){
session_destroy();
header("Location: ../index.php?msg=You have been successfully logged out!");
exit;
}
}else
{session_destroy();
header("Location:../index.php?msg2=Your session has expired! Please Log-in again.");
exit;
}
}
else
{session_destroy();
header("Location:../index.php");exit;
}
as you can see the includes are directly called and no "includes/example.php" is needed. which means inside my "head.php" i need to make two
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css"> just so i can get the css to work on both "index.php" and "includes/profile.php"
If you don't mind using an absolute path,
Try using "http://localhost/css/example.css"
The way I handle this is to set a level variable at the top of the page:
$level = ''; This would be top level
$level = '../' This would be for files in a top level folder
$level - '../../'; This would be for files in a sub directory... and so on
Then all you need to do is set the $level var in the path:
$level.'css/example.css -- works everytime
NOTE: If you are using .htaccess you need to call the full url to both .css and .js files
You could define a constant containing the absolute root, and include that in each path. If you save your root in a variable or constant, instead of write it directly in your files, it will be easier for you if your root changes in the future.
define("ROOT", "http://localhost/");
include ROOT . 'example/example.php';
Or maybe construct a function to call?
function path($string, $echo = TRUE) {
$root = "http://localhost/";
if($echo === TRUE) {
echo $root . $string;
} else {
return $root . $string;
}
}
path('index.php');
header("Location: " . path('test.php', false));

Checking if a image exists

I want to check if the file exists before I use it on the page. From what I understand something like this should work but nothing appears so I am assuming I have not coded it correctly.
<?php
$url = get_bloginfo('template_directory');
$page_id = get_queried_object_id();
if(file_exists( $url . '/images/footerImage' . $page_id . '.png')) { ?>
<img class="footerImage" src="<?php echo get_bloginfo('template_directory'); ?>/images/footerImage<?php echo $page_id; ?>.png" />
<?php } ?>
I want it to check to see if the file exists and if it does create the image tag, if not do nothing. Why is this not showing any images? I know I have the images because it was working before I had the if statement but creating a broken image on a page that I did not have an image for.
get_bloginfo('template_directory') function returns the Absolute URL. But you need to supply the Relative path in order to check the file existence file_exists()
if(file_exists( TEMPLATEPATH . '/images/footerImage' . $page_id . '.png')) { ?>
<img class="footerImage" src="<?php echo get_bloginfo('template_directory'); ?>/images/footerImage<?php echo $page_id; ?>.png" />
<?php } ?>

Categories