The code I have should output a jpg from a list of files in a directory however it is not. I have trawled this site and tried different methods but not helped. I am a relative beginner at php so looking for any help at all.
I have tried using img src in the php code but I am trying to get the image to display within a Wordpress post so I cannot echo the img src within the script. I have tried file_get_contents and read file as well but it may be my lack of knowledge holding me back.
<?php
$imagepath = htmlspecialchars($_GET["image"]);
$imagenum = htmlspecialchars($_GET["num"]);
define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME'] );
If(LOCALHOST){
define('PATH_IMAGES', 'this_path');
}else{
define('PATH_IMAGES', '../../../Images/');
}
$arrnum = $GLOBALS[imagenum] - 1;
$dirname = PATH_IMAGES . $GLOBALS[imagepath]."/";
$images = scandir($dirname);
rsort($images);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
header('Content-type: image/jpeg');
file_get_contents('$dirname$images[$arrnum]');
}
}
?>
Have you tried readfile(...); should read and output the file. In your example you are not outputting the image data
http://php.net/manual/en/function.readfile.php
Related
This is the code I'm using to get and display images from a folder on my server:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
$dirname = "$root/Folder/".$row['city']."/";
$images = glob($dirname."".$row['id']."#*.jpg");
foreach($images as $image) {
echo "<img src=\"".$image."\">";
}
The "normal" path of a picture is /Folder/Berlin/1#1.jpg. In the rendered HTML source code I can see PHP makes this link: /var/www/user_name/html/Folder/Berlin/1#1.jpg
But unfortunately the image doesn't get loaded.
What am I doing wrong?
I'm still learning a lot of terminology with php, so I find it hard to find some answer in similar questions. I'm looking to print a custom url into a PHP statement so I can print the contents of an .svg file. I'm using drupal 7.
Below are some examples of what I have tried but the files url keeps being printed?
<?php echo file_get_contents("print $fields['field_svg']->content"); ?>
or
<?php $file=print $fields['field_svg']->content ?>
<?php echo file_get_contents("echo $file"); ?>
or
$image = file_get_contents($path);
$destination = $fields['field_svg']->content;
$file = file_save_data($image, $destination, FILE_EXISTS_REPLACE);
if (is_object($file)) {
$file->status = 1;
$file = file_save($file);
}
Thanks
I am trying to echo out all of the images in a folder directory with a couple of exceptions/ignores.
This is working ok apart from it also echoes out a blank photo for every photo it echoes out?
why is this happening can someone please show me where I'm going wrong thanks.
<?php
$dirname = "./data/photos/".$profile_id."/";
$images = scandir($dirname);
$ignore = Array("_cover.jpg", "_default.jpg");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src='./data/photos/".$profile_id."/$curimg'/ class=\"profile_photos\"><br>\n";
};
}
?>
You appear to have an extra slash after your image source:
echo "<img src='./data/photos/".$profile_id."/$curimg'/ class=\"profile_photos\"><br>\n";
//--------------------------------------------------------^ here
This may be interefering with how the browser parses the DOM and causing an extra image to appear.
Also, small suggestion, try using this line instead:
echo "<img src='".$dirname.$curimg."' class=\"profile_photos\"><br>\n";
You should ensure that $curimg is actually a jpg file:
if(!in_array($curimg, $ignore) && preg_match("/\.jpg$/i", $curimg)) {
scandir returns not just files but subdirectories, including . and ...
I have a script that scans a directory of thumbnails and echoes them to the page. It works nicely, but the thumbnails are not clickable, and i would really like this to be the case. echo "<img src='$thumbnail' class='resizesmall'>"; is the line where the thumbnails are echoed. I'm not sure how to write the path to the larger image inside the php without breaking it. Maybe this should be done inside the foreach statement? thanks for your help?
$dir = "../mysite/thumbnails/";
$dh = opendir($dir);
// echo "$dh";
$gallery = array();
while($filename = readdir($dh))
{
$filepath = $dir.$filename;
//pregmatch used to be ereg
if (is_file($filepath) and preg_match("/\.png/",$filename))
{
$gallery[] = $filepath;
}
}
sort($gallery);
foreach($gallery as $thumbnail)
{
echo "<img src='$thumbnail' class='resizesmall'>";
}
?>
</div>
<??>
The easiest way would be to setup a situation where your thumbs and your full size images were named the same. So you may have thumbs/image1.png and full/image1.png. Then instead of using $thumbnail use a variable $image, or something similar just so the code reads better. You'll also want to leave the $filepath out of the mix so that $image ends up as just the file name.
foreach($gallery as $image)
{
echo "<a href='full/$image'><img src='thumb/$image' class='resizesmall'></a>";
}
You may want to throw in some checks to make sure there is a matching image just to prevent errors or bad UX. However, the code above should work.
I'm trying to create an Intranet page that looks up all pdf documents in a UNC path and the returns them in a list as hyperlinks that opens in a new window. I'm nearly there however the following code displays the FULL UNC path - My question how can I display only the Filename (preferably without the .pdf extension too). I've experimented with the basename function but can't seem to get the right result.
//path to Network Share
$uncpath = "//myserver/adirectory/personnel/";
//get all files with a .pdf extension.
$files = glob($uncpath . "*.pdf");
//print each file name
foreach ($files as $file)
{
echo "<a target=_blank href='File:///$file'>$file</a><br>";
}
The links work fine it just the display text shows //myserver/adirectory/personnel/document.pdf rather than just document. Note the above code was taken from another example I found whilst researching. If there's a whole new better way then I'm open to suggestions.
echo basename($file);
http://php.net/basename
Modify your code like this:
<?
$uncpath = "//myserver/adirectory/personnel/";
//get all files with a .pdf extension.
$files = glob($uncpath . "*.pdf");
//print each file name
foreach ($files as $file)
{
echo "<a target=_blank href='File:///$file'>".basename($file)."</a><br>";
}
?>
You may try this, if basename() does not work for some reason:
$file_a = explode('/',$file);
if (trim(end($file_a)) == '')
$filename = $file_a[count($file_a)-2];
else
$filename = end($file_a);