PHP absolute path with timThumb doesn't work - php

I'm writing a wordpress plugin, and using this script to resize images: Timthumb
This script uses absolute paths, but I can't get it to work for me; I've triple-checked all my paths but still nothing.
Here is my code:
$plugin_dir_name = "my-plugin";
$pathTimThumb = WP_PLUGIN_URL . '/' . $plugin_dir_name . '/timthumb.php';
$pathToUpload = WP_CONTENT_URL.'/uploads/'.$plugin_dir_name;
$hImg = 150;
$wImg = 150;
....
$myImage = '<img class="thumb" src="'.$pathImageThumb.'?src='.$pathToUpload.'/'.$allImages[$i].'&h='.$hImg.'&w='.$wImg.'&zc=1" alt="">';
In firebug I get this URL:
<img alt="" src="http://localhost/mu/wp-content/plugins/my-plugin/timthumb.php?src=http://localhost/mu/wp-content/uploads/my-plugin/car___1/26zhoar5.jpg&h=150&w=150&zc=1" class="thumb">
Where is the mistake?

use this one.
$my_plugin_url = plugins_url('my-plugin-name/');
$my_timthumb_url = $my_plugin_url.'timthumb.php?';
$my_image_url = 'http://localhost/images/image.jpg';
echo '<img alt="" src="'.$my_timthumb_url.'src='.$my_image_url.'&h=150&w=150&zc=1"/>';
things to consider to make timthumb work:
chmod your timthumbs cache folder
do not use external images
check you timthumb version
Cheers,Dave

WP_CONTENT_URL is a url, not an absolute path. Use WP_CONTENT_DIR instead.

TimThumb tries to determine the local path of the image by stripping out http://CURRENT_HOST.tld from the beginning of the src parameter.
Since you're running on localhost it might be getting a little confused and falsely calculating it as an external image. I doubt this is the case (I checked the source and it should be OK), but it's an educated guess.
Have you tried reading the HTTP response headers from http://localhost/mu/wp-content/plugins/my-plugin/timthumb.php?src=http://localhost/mu/wp-content/uploads/my-plugin/car___1/26zhoar5.jpg&h=150&w=150&zc=1?
If not, use HttpFox for FireFox and post back the results.

Related

Set img src without knowing extension

So I have a few images in the server (public_html/img/profile_pictures/).
This is how I currently set the image:
echo "<img src='img/profile_pictures/main_photo.png'/>";
The main_photo can change each day, but if it changes to main_photo.jpg insted, it wont show (because the extension is hardcoded on that line(.png)). Is it possible to display the photo without knowing the extension for the image file?
If you want a PHP code, then try this. This code will look for main_photo.* inside your folder and automatically set the extension upon finding one.
Remember to set the path properly
<?php
$yourPhotoPath = "img/profile_pictures/";
foreach (glob($yourPhotoPath.'main_photo.*') as $filename) {
$pathInfo = pathinfo($filename);
$extension = $pathInfo['extension'];
$fileName = chop($pathInfo['basename'], $extension);
echo "<img src='".$yourPhotoPath.$fileName.$extension."'/>";
}
?>
if a Photo isn't loaded, it's width and size is null.
Although I would advise you to write a class that checks and loads images, I get a feeling you want a simple solution. so, given by the premise that the photo is either
<img src='img/profile_pictures/main_photo.png'/>
or
<img src='img/profile_pictures/main_photo.jpg'/>
and that neither this path nor this filename ever changes and in the folder is only one picture,
you could simply echo both.
The img of the one that is empty will not be shown.
A better way was to write a class that loads your photo and checks if the photo is really there, like
$path = 'img/profile_pictures/main_photo.png';
if(!file_exists('img/profile_pictures/main_photo.png'))
{
//use the jpg path
$path = 'img/profile_pictures/main_photo.jpg';
}
You can ofc just inline this if case, but it's bad practise to intermix buisinesslogic and format logic, so I advice you to write a class for it.

How to fix the broken images even though I create the code that retrieves all images from the folder

I've got the broken images when I try to retrieve all jpg files from the folder where the name is 'imgProduct'.
The error message is 'Failed to load resource: the server responded with a status of 404 (Not Found)'
The directory of all images is C:\Sungsan\imgProduct.
And the directory of code below is C:\Sungsan\Website\view\shopPanel.php.
<?php
$productImg = glob("../imgProduct/*.jpg*");
echo var_dump($productImg).'<br>';
for($a = 0; $a < count($productImg); $a++) {
$num = $productImg[$a];
echo basename($num)."<br />";
echo '<img src="' .$num. '" alt="random image"> <br />';
}
?>
This image is the result that code above is excuted.
I was searching for how to troubleshoot this broken images but I have no idea. Hope to give recommendation or suggestion. Thanks
Your basepath makes ../imgProduct/img01.jpg to be img01.jpg. And I assume that your web root is not inside imgProduct, so you need to append it:
$num = basename($num);
echo "{$num}<br/><img src='/imgProduct/{$num}' alt='random image'> <br />";
Also don't mistaken server-side path with web-side url. ../imgPorduct/img01.jpg is going back in your URL from current website page, not from PHP file
I could be wrong, but by the looks of it you're trying to reference files in the wrong directory?
Change your image src to point to the folder containing the images (currently it's seemingly looking for 'img1.jpg' in the web pages current directory.
echo '<img src="../imgProduct/' .$num. '" alt="random image"> <br />';
After looking at your screenshot again and reading some of the comments, I've also noticed that you could likely also just remove basename as it is stripping out the rest of the file path.
Hope this helps.

Image Path isn't displaying image

Am wondering why these image isn't showing even after properly retrieving the image path from MYSQL i've gone through the code and i still can't figure out why this is:
<?php
$sql = "SELECT description, quantity, product_price, category, product_img_url";
$sql .= " FROM *****, *****";
$sql .= " WHERE product_id = product_img_id";
$sql .= " AND product_id = ?";
$sql .= " LIMIT 1";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $id);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows==1){
$stmt->bind_result($desc,$qty,$pr,$cat,$product_img_url);
$stmt->fetch();
?>
<div id="infobox">
<h3>Product View</h3>
<p align="left">
<?php
if(file_exists($product_img_url)){
echo '<img src="'.$product_img_url.'" alt="" width="300" height="300" />';
}else{
echo '<img src="" alt="product image" />';
}
?>
Please can someone tell me why this is happening?
If the image file does exist, then this might have something to do with the path.
I would use firebug to play with the image src path and try various things like putting a slash in the beginning, or removing it, or entering absolute path of image.
Once I get the image to show up in firebug, I would then proceed to fix the issue in the code.
As i am see you code and i just want to ask you that you have store complete path of your image in db or you have store just some part of it.
as i can see you have retrieve image path from mysql but the browser not able to retrieve image from your server path it may be because of path you r providing and and where the file store not match. the best way to check your file path and mysql path use
var_dump(your result set) and check you output weather image url match with your server image path, and i am prety much sure that your path will not match.
Can you please try with the following path structure
echo '<img src="/application_root_directory/'.$product_img_url.'" alt="" width="300" height="300" />';
Well, You can do like:
$product_img_url="guarantee.png"; ?>
<img src="images\<?php echo $product_img_url; ?>" alt="Hello">
It will not installed by default you have to install it manually. Either you can do goto tools in menubar>add-ons, and search in search fox for firebug or goto firebug website to install it.
Thanks everyone for all useful ideas presented, I appreciate. I've found a way to resolve the relative/absolute links. The images are now being displayed as they should! What I did was; since I have sub directories under my root folder, I created separate 'Meta' php scripts in each sub directory with names like INC, __ADM, etc. Each script links to another script in a toplevel directory. All I placed within the scripts were just paths, that I got using DOCUMENT_ROOT, doing this I was able to get the actual path to the image folder, that I then placed in the DB. Hope this idea helps someone else!

php function file_exists with condeigniter

I am using codeigniter, I want to show a user page with his profile picture shown. When a user uploads a profile picture, it will get the same name as the username (where the old profile picture will be overwritten). If he hasn't uploaded a profile picture, then a questionmark picture is shown. I use file_exists function, but it returns every time false.
$filename = base_url().'images/userimg/'.$this->session->userdata('username');
if(file_exists($filename)){
echo '<img src="'.$filename.'" width="300" height="300" /><br/>';
}
else {
echo '<img src="'.base_url().'images/no_image.jpg" width="300" height="300" /><br/>';
}
Change your codes into this:
$filename = './images/userimg/'.$this->session->userdata('username');
It could be that your problem results from thae usage or url instead of local server path. That base_url in the config file contains an URL and the file_exists function should work with a full local path - for which you can use FCPATH (like Svetlio suggested) or any other way. Although the file_exists should work with URL , i think there will be more trouble with it than a full path.

Using a web domain or system path URL to access a file in PHP

I have a localhost Apache/PHP configuration on my computer. I want to be able to
1) check the existence of an image file using an absolute path and
2) display it using the same URL
A simple example of something I want to do:
if(file_exists($url)) {
echo '<img src="' . $url . '" />';
}
if I set...
$url = 'http://' . $_SERVER['HTTP_HOST'] . '/myimg.jpg';
then file_exists will return false, because the URL is a web path (http://localhost/myimg.jpg).
if I set...
$url = $_SERVER['DOCUMENT_ROOT'] . '/myimg.jpg';
it will recognize that the file exists (C:/htdocs/myimg.jpg), but fail to display the image because it cannot access the image source. If I view the source and copy/paste the URL into the web browser's address bar, the image is displayed just fine.
Perhaps I'm missing something in Apache's httpd.conf file, or PHP.ini? Maybe an Alias declaration of some sort. Also, the site will be uploaded to a remote web server (such as bluehost.com) when it is complete, so I need a versatile solution.
allow_url_fopen in PHP.ini is On, and I've been restarting the server every time I make a change in either httpd.conf or PHP.ini. I've been struggling with this for days, so even a point in the right direction would be very appreciated :)
Thanks.
You're confusing server side file access with client side.
$url = 'http://' . $_SERVER['HTTP_HOST'] . '/myimg.jpg';
This produces a URI to be used by the client to fetch your image via HTTP. For example http://example.com/myimg.jpg
$url = $_SERVER['DOCUMENT_ROOT'] . '/myimg.jpg';
This produces a filesystem path to be used by PHP. For example /var/www/htdocs/myimg.jpg
Your solution needs to use both
$img = '/myimg.jpg';
if(file_exists($_SERVER['DOCUMENT_ROOT'] . $img)) {
echo '<img src="', $_SERVER['HTTP_HOST'], $img, '" />';
}
Edit: You could probably replace the echo line with
echo '<img src="', $img, '" />';
as an absolute URL (one beginning with a forward slash) always starts at the document root
An absolute path, which file_exists takes, is completely different from your web path. You'll just need to use separate paths for file_exists and your src attribute in your img tag.
$img = 'myimg.jpg';
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $img) {
echo '<img src=' . $img . ' />';
}
<?php
$url = "/myimg.jpg";
if( file_exists($_SERVER['DOCUMENT_ROOT'].$url))
echo "<img src='".$url."' />";
?>
This will give you the HTML:
<img src='/myimg.jpg' />
Which is the image since it resolves from the domain name.

Categories