how to display a picture from a folder php - php

I have a folder where uplaoded files are saved. This page is used to retrieve those saved files. A picture called "flower.jpeg" is in the folder, and I am trying to display the picture using an image tag, as shown below. I have two options, but none of them is working.
<!DOCTYPE html>
<html>
<body>
<?php
$picture=flower.jpeg
$playfile='/upload/$picture';
?>
// <img href= '<?php $playfile ?>' width="800" height="600">
// <img src= '<?php echo $playfile; ?>' width="800" height="600"> <br>
<script>
document.write('Go Back To Chat');
</script>
</body>
</html>

flower.jpeg requires quotes and the $playfile assignment is not properly specified as, to my knowledge, strings within single quotes are not parsed for variables within PHP.
$picture='flower.jpeg';
$playfile='/upload/'.$picture;
Additionally correct use of the tag is as follows
<img src="<?php echo $playfile; ?>" width="800" height="600">
As the attributes need enclosing by double-quotes, you are just using PHP to echo out a variable containing a string.

An explanation of what is wrong. You're using the following code to build your link:
$picture=flower.jpeg
$playfile='/upload/$picture';
First off, you need to quote the picture
$picture = 'flower.jpg';
//or
$picture = "flower.jpg";
The next problem is how you're building the playfile. You need to exit single quotes to use a variable or use double quotes:
$playfile = '/upload/'.$picture;
//or
$playfile = "/upload/$picture";
//or even
$playfile = "/upload/".$picture;
The single quotes will take the text as is where the double quotes will evaluate variables in them. I suggest continuing to read up on the basics of PHP and strings.

Here are a few steps that may help you in finding if the path to the image (or the 'href' as you call it) is correct. No direct solution is possible as we do not have access to your file system, as Simon pointed out.
Check if the file extension of your picture is mentioned correctly. Check also that it is in the correct CAPS. For example, if you saved your picture using MS Paint, the extension will be *.PNG but your script may contain *.png; this may cause problems in some cases.
.jpg and .jpeg are two different extensions sometimes (in the sense of the above point). You may want to take that into consideration.
You may want to verify that the directory structure of the image is correct (i.e. verify that you are specifying the correct path to the file).
Hope this helped you in some way. The above steps are rough and vague. If they failed to help you, please comment, and someone will be able to help you.

Related

data: disappears from base64 string

I have a base64 image that i need to display via php.
It's to be used on a wordperss/woocommerce site, at the cart.
The string is correct and working fine when inserting directly into plain html img tag.
The string starts as such: data:image/png;base64...............
But when it's inserted as src, via php, it doesn't include the beginning "data:" word.
It displays as: image/png;base64...............
I have tested the string with plain echo, and it does include data: when just echoed out, but as soon as it's palced inside src or href, data: goes away.
I have no idea why this is happening and search results in no meaningful information.
Added code snippet:
$img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAImCAYAAACrXu7BAAAgAE................";
echo '<img src="'.$img.''" />';
data: disappears, echoes out as:
<img src="image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAImCAYAAACrXu7BAAAgAE................" />
But if i do this:
$img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAImCAYAAACrXu7BAAAgAE................"
echo $img;
It works and echoes as:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAImCAYAAACrXu7BAAAgAE................
I found the issue.
It was indeed wordpress/woocommerce that was the issue, specifically in the cart and not syntax errors...
The issue is do to: wp_filter_post_kses
I will mark this as resolved...
Thanks to those who actually tried to help and understand that the code snippet was not the real code and that there was no syntax errors, as i stated.
Maybe people should not be overly pedantic when trying to help? Just a thought...

adding HTML image size in PHP

I'm new to PHP and coding in general so apologies for this likely silly question. I know the answer will be extremly simple but try and I might, I just can't see it.
I'm trying to pull image path data from my database and concatenate with the code below so that I can enventually display it on my site using <?php print.... ?>
I have successfully done this. The problem I have now is setting the size of this image.
Please see my code below.
$Image_Path .= " <img src = db_images_product/".'$row['ImagePath']'." ".'height="100"'."/> ";
I will be indebted to anybody who can help on this.
I have researched this question and came across some answers but just could not make them work with my problem.
I's just the image size I have an issue with, nothing else.
I ended up using the code below and then using CSS. This was inspired by #Ndianz's answer.
$ImagePath = " <img src = db_images_product/".$row['ImagePath']." ".'Class="ProductImage"'."/> ";
I have ran all this code through an editor and it runs fine with no errors.
To illustrate concatenation here is a link with many scenario and explenations to help you,
Click Here
This is the way you currently have it,
$Image_Path .= " <img src = db_images_product/".'$row['ImagePath']'." ".'height="100"'."/> "
This is the way you would write that,
$Image_Path = '<img src="db_images_product/"'.$row['ImagePath'].'" height="100"/>';
I suggest you do this,
$Image_Path = '<img src="db_images_product/'.$row['ImagePath'].'">';
Then in your css file add the style to your img tag like this,
img {
height: 100px; //or whatever you want it to be.
}
You can use getimagesize() to grab the width and height then set the property on the image tag. the $row['ImagePath'] needs to be reachable by the script so you might have to play with this and add some ../ to get to the relative path of the image.
list($width, $height) = getimagesize($row['ImagePath']);
$Image_Path .= "<img src='db_images_product/{$row['ImagePath']}' height='{$height}' >";
You've just got some syntax errors. When you concatenate be careful about where your quote marks go. I'd use single quotes to open and close your PHP string so as not to conflict with the double quotes in your HTML. So pay close attention -- the single quote closes the PHP string right before the dot concatenates the $row variable. Then after the variable you add another dot to concatenate the end of the PHP string.
$Image_Path .= '<img src="db_images_product/"'.$row['ImagePath'].'" height="100"/>';

PHP onmouseover change image called from database

echo'<img src="'.$row['filename'].'" onmouseover="this.src='.$row['back_filename'].'" onmouseout="this.src='.$row['filename'].'" />';
I'm calling in 2 images from a database using mySql and php, How come this onmousover doesn't work?
ps. I'm calling a path to the image not storing the image in the database itself.
try this
echo'<img src="'.$row['filename'].'" onmouseover="this.src=\''.$row['back_filename'].'\'" onmouseout="this.src=\''.$row['filename'].'\'" />';
You are not providing the needed quotes for the inline javascript, you need single quotes '' around the filename as it is a string, causing whatever the variables hold to be interpreted by javascript as something other than what you expect.
Also use a heredoc to help with preventing errors caused by misquoting and worrying about escaping quotes.
echo <<<END
<img src="{$row['filename']}" onmouseover="this.src='{$row['back_filename']}'" onmouseout="this.src='{$row['filename']}'" />
END;

Writing HTML that contains PHP tags to file

I think what I'm trying to do must be impossible; incredibly difficult; or pretty easy, and I just don't know how to express what I want correctly to Google.
What I want to do is the following:
For the purposes of templating, I am outputting HTML to a file. However, I want the file to contain PHP tags - not the parsed value of the PHP code (this value will change based upon included scripts in the output file) - but the actual <?php ?> tags themselves - containing the unmodified code, such that the code is still executable at run time.
This is an example of what I want to get outputted:
<html>
<body>
<img src="<?php echo IMG_PATH;?>img.jpg" />
</body>
</html>
What I'm getting is a combination of things that are wrong, depending on which method I use. Basically, I need to be able to write parsable PHP code to a HTML file. Now, whether or not this is a good idea is debatable, and a subject I'm not too interested in at the moment. I just want to know if a sane solution exists.
I came up with the following insane solution:
$content='<html><body><img src="zxzxzx?php echo IMG_PATH; ?qjqjqj" />';
file_put_contents($file,$content);
$command='perl -p -i.bktmp -e "s/zxzxzx/</g" '.$file.' 2>'.$path.'error.log';
$command2='perl -p -i.bktmp -e "s/qjqjqj/>/g" '.$file.' 2>'.$path.'error.log';
exec($command);
exec($command2);
Now, on one hand, this feels devilishly clever. And, it also feels insane. Is there a better/possible way to do this?
You are misinterpreting how PHP operates. The following code:
<?php
echo 'This string <?php echo 'contains' ?> some PHP code';
Will produce as output
This string <?php echo 'contains' ?> some PHP code
You will NOT get:
This string contains some PHP code
PHP's parser is not recursive. PHP code embedded inside PHP code, e.g. inside a string, as it is above, is NOT treated as PHP code - it'll just be some text.
If you want to write out some PHP code, then just write it out:
$string = 'This string <?php echo 'contains' ?> some PHP code';
file_put_contents('file.php', $string);
You don't need to take any special measures for the PHP code in the string to be written out, it'll just "work".
Now, that being said, if you ever put that string into a context where it COULD be evaluated as code, e.g.
eval($string);
then you WILL have to take measures to keep PHP from seeing the opening <?php tag and switching into "code mode".
Instead your HTML file needs to be a PHP file. See below:
File: test.php
<html>
<head>
</head>
<body>
<? echo 'My PHP embedded in HTML'; ?>
<img src="<? echo $imagePath; ?>"/>
</body>
</html>

How do I echo forward slash in php

I want to echo forward slash in php as follows
<a class="submenu" href="<?php echo base_url('products').'/'.rawurlencode('Agarbatte/Candle');?>"
Agarbatte/Candle is not a directory but when i do this, href takes this as directory and give me the error page not found.
Any help will be appreciated.
A slash in the URL will always be treated as a directory separator. And if you replace it with something else you won't have a slash anymore. But that's most likely the easiest solution..
If you have a 1:1 mapping between the path in the URL and your filesystem you are out of luck. If your application uses a "routing layer" though, you can modify it to not treat the / as a separator when some criterium is met.
Just use 'urlencode' on the 'Agarbatte/Candle'. I do not see a special need for 'rawurlencode'.
Though you can use the urlencode() function as others have said, I recommend not getting into this habit. Assuming you have these products in a database, use the id and you'll never run into these issues.
For example if the Candle product has id of 6 in the database... the PHP should resolve to: <a class="submenu" href='products/6'>
Furthermore, there are some shortcuts you can take here which will help you in the long run.
Instead of <?php echo "something" ?> turn on php short tags in php.ini and use <?= "something" ?>
You don't need to call base_url every time you write a link. Use the <base> HTML tag appropriately: <base href="http://localhost/yourapp/"> in <head>
So... with that said after using 1 and 2:
<a href='products/<?= $id;?>'><?= $name_of_product ?></a>
how about using "Agarbatte-Candle" in the url then on the output page
$url_bit=string_replace('-','/',$url_bit);

Categories