I'm facing a problem.
I have actually this code:
// Check if image exist for the hotel
$src = '../assets/app/images/hotel-logos/007.jpg';
if(file_exists($src)) {
$src = $src;
}
else {
$src = '../assets/app/images/hotel-logos/default.jpg';
}
echo '<center><img src="'.$src.'" width="200"></center>';
This code check for an image existence.
But each time I have the fallback image default.jpg whereas I should have 007.jpg.
I check my path and it works. My 007.jpg image is into the same directory as my default.jpg image.
I already test with if(#getimagesize($src)) { ... }. The same.
Why ?
Even that your file is placed at some directory, doesn't mean that the current path of the PHP process is the same. Use the absolute path instead:
// Check if image exist for the hotel
$src = '../assets/app/images/hotel-logos/007.jpg';
if(!file_exists(__DIR__.'/'.$src)) {
$src = '../assets/app/images/hotel-logos/default.jpg';
}
echo '<center><img src="'.$src.'" width="200"></center>';
I think you are over doing it make it simpler might solve your issue Try This:
<?php
$src = '../assets/app/images/hotel-logos/007.jpg';
if (!file_exists($src)) {
$src = '../assets/app/images/hotel-logos/default.jpg';
}
echo '<center><img src="'.$src.'" width="200"></center>';
?>
Ok, I get it working by using:
$_SERVER['DOCUMENT_ROOT']
// Check if image exist for the hotel
$src = '../assets/app/images/hotel-logos/007.jpg';
if(!file_exists($_SERVER['DOCUMENT_ROOT'].'/assets/app/images/hotel-logos/007.jpg')) {
$src = '../assets/app/images/hotel-logos/default.jpg';
}
echo '<center><img src="'.$src.'" width="200"></center>';
Related
Hi I am using the below code to download image from url but its not working when i am using this code
<?php
$imageUrl = 'https://cwsimages.ingramtest.com/cdsImages/imageloader?id=pBbFysOWRLJoSy4l4lbc+yLblU6JMuhKpze3XsQNO+njA3/XYRYbXSEYYsSqKXoiGD07duAyOSVXNUVLvxDqlMx15WtRQWJn3xC/twmM2s62tw+XgriCmEXBHawun03pQLBHXLuEQhNmCb8MC3ZMNH7pe5O76s18u/mgplf8YtU=';
#$rawImage = file_get_contents($imageUrl);
if($rawImage)
{
file_put_contents("images/".'dummy1.png',$rawImage);
echo 'Image Saved';
}
else
{
echo 'Error Occured';
}
?>
but if i changed the $imageUrl with this
$imageUrl = 'http://www.samsung.com/in/common/img/home/S2_pc.png';
it works, please suggest what is wrong in first url or can't we store image from https url
The problem is that the "images" you are using in the file_put_contents is not getting the path that where to save the file. try to make the images folder along with php file which having this code and see its working.
<?php
$imageUrl = 'https://cwsimages.ingramtest.com/cdsImages/imageloader?id=pBbFysOWRLJoSy4l4lbc+yLblU6JMuhKpze3XsQNO+njA3/XYRYbXSEYYsSqKXoiGD07duAyOSVXNUVLvxDqlMx15WtRQWJn3xC/twmM2s62tw+XgriCmEXBHawun03pQLBHXLuEQhNmCb8MC3ZMNH7pe5O76s18u/mgplf8YtU=';
#$rawImage = file_get_contents($imageUrl);
if($rawImage)
{
file_put_contents("images/".'dummy1.png',$rawImage);
echo 'Image Saved';
}
else
{
echo 'Error Occured';
}
?>
try copy
$imageUrl = 'http://www.samsung.com/in/common/img/home/S2_pc.png';
if (!file_exists('folder_name'))
{
mkdir('folder_name', 0777, true);
}
$img_path="folder_name/S2_pc.png";
copy($imageUrl , $img_path);
echo $img_path;
I'm trying to retrieve images for WP posts, which are held in a wp-uploads directory, using if (file_exists() but it's not recognising the file path.
For each post, there are up to 8 images available. Each image has the letters a-g at the end of the file name (or nothing), and has str_replace to replace certain characters in the file names.
I need to show each image if it exists, and if not, to return nothing. So if the post is associated with images with b, d and f at the end, it just shows those three.
I've tested without the (file_exists()) and its able to pick up the images with a simple echo for each - but it seems the $img paths aren't being recognised.
Im a bit of a php noob so any help would be appreciated...
$uid = get_post_meta (get_the_ID(), 'Unique number', true);
$root ="/wp-content/uploads/2016/Collection/";
$path = str_replace(" ","_",$uid);
$path = str_replace(".","_",$path);
$path = str_replace(":","",$path);
$img = $root.$path.".jpg";
$imga = $root.$path."a.jpg";
$imgb = $root.$path."b.jpg";
$imgc = $root.$path."c.jpg";
$imgd = $root.$path."d.jpg";
$imge = $root.$path."e.jpg";
$imgf = $root.$path."f.jpg";
$imgg = $root.$path."g.jpg";
if (file_exists($img)) { echo "<img src='".$root.$path.".jpg' />"; } else { echo ""; }
if (file_exists($imga)) { echo "<img src='".$root.$path.".jpg' />"; } else { echo ""; }
if (file_exists($imgb)) { echo "<img src='".$root.$path."b.jpg' />"; } else { echo ""; }
if (file_exists($imgc)) { echo "<img src='".$root.$path."c.jpg' />"; } else { echo ""; }
if (file_exists($imgd)) { echo "<img src='".$root.$path."d.jpg' />"; } else { echo ""; }
if (file_exists($imge)) { echo "<img src='".$root.$path."e.jpg' />"; } else { echo ""; }
if (file_exists($imgf)) { echo "<img src='".$root.$path."f.jpg' />"; } else { echo ""; }
if (file_exists($imgg)) { echo "<img src='".$root.$path."g.jpg' />"; } else { echo ""; }`
You need to rearrange the way you are telling PHP to look up the address,
$root is probably not your absolute file path root (probably meaning absolutely) so instead use the special super variable for this, $_SERVER['DOCUMENT_ROOT'] which is the root of the web accessible filepath, therefore you then have:
$img = $_SERVER['DOCUMENT_ROOT'].$root.path.".jpg"
//while retaining your current / at the start of $root
This is the file structure to check if the file exists, not the file structure to reference in the <img> tag, that appears to be correct in your examples above.
So, your overall correction should look like this:
$root ="/wp-content/uploads/2016/Collection/";
$path = str_replace(" ","_",$uid);
$path = str_replace(".","_",$path);
$path = str_replace(":","",$path);
$img = $root.$path.".jpg";
...
if (file_exists($_SERVER['DOCUMENT_ROOT'].$img)){
....
}
An additional note is that the results of this function are cached so you should call clearstatcache() at the start of this file so that it can make fresh checks for if the images exist. Currently without this even if the image does exist, PHP will be using the cached - past- results which may not be up to date.
You start $root with a /, therefore it's starting from the server's root directory. Remove the first / and retry.
$uid = get_post_meta (get_the_ID(), 'Unique number', true);
$path = str_replace(" ","_",$uid);
$path = str_replace(".","_",$path);
$path = str_replace(":","",$path);
$uploads = wp_upload_dir();
Get base root directory.
$root = $uploads['path'];
$imgDir = $root.$path.".jpg";
if (file_exists($imgDir)){
.......
}
For example I have function named test in which I do some stuff with image. I want to be able detect if in it was passed the image or only the path to it. For example:
function test($image) {
// here I need to detec if $image is path or loaded image
}
test('/home/name/image.jpg');
test(file_get_contents('/home/name/image.jpg'));
Assuming you have these sanitized:
if (file_exists($image)) {
// path
} else {
// image
}
Probably you’ll want to check whether an image is the proper image using some image handling library in the else clause.
Use getimagesize($image);. If it has a width and height, it's an image.See http://php.net/manual/en/function.getimagesize.php for full info.
$image_size = getimagesize($image);
$image_size[0] > 0 ? /*It's an image*/ : /*It's not*/;
Try this
function test($image) {
if($image=='')
{
return false;
}
if(file_exists($image))
{
echo "file exists";
}
else
{
echo "path is passed to the function";
}
}
test('/home/name/image.jpg');
test(file_get_contents('/home/name/image.jpg'));
you can use is_file for this
Tells whether the given file is a regular file.
somebody knows why this script not working?
$imgname = get_stylesheet_directory_uri().'/images/headers/'.str_replace(' ', '', strtolower(get_the_title())).'.jpg';
if (file_exists($imgname)) {
echo '<img src="'.$imgname.'"> </img>';
} else {
echo '<img src="'.get_stylesheet_directory_uri().'/images/headers/default.jpg"> </img>';
}
it returns allways the default.jpg, even if the file exists
I checked the $imgname, and it is ok
yes you are right, I mus use like this:
$imgpath = get_stylesheet_directory().'/images/headers/'.str_replace(' ', '', strtolower(get_the_title())).'.jpg';
$imguri = get_stylesheet_directory_uri().'/images/headers/'.str_replace(' ', '', strtolower(get_the_title())).'.jpg';
if (file_exists($imgpath)) {
echo '<img src="'.$imguri.'"> </img>';
} else {
echo '<img src="'.get_stylesheet_directory_uri().'/images/headers/default.jpg"> </img>';
}
?>
You probably have to distinguish here between URLs and file paths.
When you direct your browser to a an image like
http://www.example.com/path/to/image.jpg
and it works, then still the file_exists() function will return false for this URL, because it's not an image path.
The correct path would be something like
/var/www/htdocs/path/to/image.jpg
on the local file system. file_exists() would then return true for this path.
What you need to test for with file_exists() is the local path of the image. If it exists, then you need to include the URL of the image. You do the URL inclusion correctly, but not the path usage.
I am trying to generate some HTML code to list some images for a slide show.
I arrived at the following idea:
function galGetTopPhotos()
{
//path to directory to scan
$directory = SITE_ROOT_PATH."/gallery/best/";
//get all files
$images = glob($directory . "*.*");
//print each file name
$ret = "";
$ret .= '<div id="myslides">';
foreach($images as $image)
{
$ret .= '<img src="'.$image.'" />';
}
$ret .= '</div>';
return $ret;
}
The problem is that it only works when I use root path for $directory...if I use URL it will not work. And it causes the images to not load. Here is what this code generates:
<div id="myslides">
<img src="D:/xampp/htdocs/mrasti/gallery/best/1.jpg" />
<img src="D:/xampp/htdocs/mrasti/gallery/best/10.jpg" />
</div>
So the question is, how to get the list of files so it generates img sources in http://127.0.0.1/.... format?
What I mean if I use the code like this it returns no file!
$directory ="http://127.0.0.1/mrasti/gallery/best/";
This looks like a job for PHP function basename. This takes a file path and returns only the final element of the path - in this case the actual name of the jpeg image.
You could amend your code so that it looks something like this:
$urlPath = "http://127.0.0.1/mrasti/gallery/best/";
...
...
foreach($images as $image)
{
$relative_path = $urlPath.basename($image);
$ret .= '<img src="'.$relative_path.'" />';
}
The above takes the path and appends the filename "example.jpg" to your image directory url
glob does only work for local files and not on remote files. Have a look here:
http://php.net/manual/en/function.glob.php
For remote files have a look here:
http://www.php.net/manual/en/features.remote-files.php
But i do not think that you need remote files. It seems like you want to go through a local directory and display this images.
Try something like this
...
$ret .= '<img src="http://127.0.0.1/my/path/'.basename($image).'" />';
...
You need to have some functionality to translate the file path on disk to the correct URI so that your browser can understand it.
In your specific case as outlined and with the exact data given in your question, the following could work:
foreach($images as $image)
{
$src = '/mrasti/gallery/best/'.substr($image, strlen($directory));
$ret .= '<img src="'.$src.'" />';
}