PHPMailer how to add embedded image inside foreach loop - php

PHPmailer will not let me add an embedded image inside a foreach loop the path is correct but for some reason it is not adding the image i have checked other sorces on here but with no luck the images are embedding fine when out of the loop but it is just when i call them by using variable names eg the way i need to inside a for each loop as the images will be dynamic it will not embed any help appreciated.
The path looks all good but it just will not run in the loop?
Please let me know if you need to see any more code i just did not want to include irrelevant code as i know the message is already sending fine it is just this bit that i have a problem with. I have the table working in html that is not being sent via email if that is of any use outputting the images correctly in the foreach loop
<tbody>'.
'<span style="display:none">'. $i = 1;' .</span>'.
$total_quantity_count = 0;
$grand_total = 0;
foreach($items_purchased_array as $item){
if (!$mail->addEmbeddedImage(getcwd() . '/' . $item['image'],'product_pic'.$i)) {
echo 'Failed to attach '. getcwd() . '/' . $item['image'],'product_pic'.$i;
}
</tbody>

Here was what i did to fix this
if (!$mail->addEmbeddedImage(getcwd().'/'.trim($item['image']),trim('product_pic'.$i))){
echo 'Failed to attach '.getcwd().'/'.$item['image'],('product_pic'.$i);

Related

Paginated PHP web scraper

Hi there I'm new here.
Trying to make this little code to loop over pages. And scrape off links of headings .
Scraping part works just fine but i cant make it to loop to the next page. It keeps looping on the same page.
<?php
include('../simple_html_dom.php');
// start at page 1
$xder = 1;
do {
// web page + page number (should change with every loop)
$html = file_get_html('https://webpage.com/stuff/page/$xder');
foreach($html->find('h3') as $h3)
{
foreach($h3->find('a') as $element)
{
echo $element->href . '<br>';
}
}
$xder++;
} while ($xder <= 5);
?>
I'm expecting to get list of links from all 5 pages, but I only get list of links from 1st page repeating 5 times.
I think the problem is here "/stuff/page/$xder');" I'm not sure how to add a variable to the back of an URL it doesn't appear to work.
Tried methods here:
Converting an integer to a string in PHP
Getting frustrated with this. Not sure what I'm missing here. Thanks for any thoughts :)
Php variables are treated as variables only if you use ", and not '
Change
$html = file_get_html('https://webpage.com/stuff/page/$xder');
to
$html = file_get_html("https://webpage.com/stuff/page/{$xder}");

php echo filenames in a directory?

I am using the following php code to echo out the names (and extensions) of all the files in a folder.
This works fine and I am producing the results in a table like so:
code:
<?php
$dir = new DirectoryIterator("data/uploads/");
foreach ($dir as $fileinfo) {
echo '<table><tr><td><p>' . $fileinfo->getFilename() . "\n" . '</p></td><td><p>Existing File</p></td><td><p>Delete</p></td></tr><tr></tr></table>';
}?>
result:
. Existing File Delete
.. Existing File Delete
Filename1.jp Existing File Delete
Filename2.jp Existing File Delete
Filename3.jp Existing File Delete
The problem is in my results I am also getting those weird dots at the top. I do not know why these are being shown as I only have 3 files in the directory and don't know where the dots have come from?
Can anyone please help with this? thanks
Add Condition to check that is it file or not.. try this code
if ($fileinfo->isFile()) { // add this condition
echo '<table><tr><td><p>' . $fileinfo->getFilename() . "\n" . '</p></td><td><p>Existing File</p></td><td><p>Delete</p></td></tr><tr></tr></table>';
}
Add condition and check if it is . or .. if it satisfies the condition continue your looop

unable to show image from external hd php

I'm new with programming and learning now php. I have installed xamp and have write a little bit code. I have some images on my external hd and I want to show some pictures.
The problem is I can get a list of path of my images with this code:
echo "<html><body>";
$outerDir = "x:\map\maps\more\\";
$total = count (array_diff (scandir ($outerDir), Array (".", "..")));
$dirs = array_diff( scandir( $outerDir ), Array( ".", ".." ) );
foreach ($dirs as $d) {
if (!is_dir($outerDir . $d)) {
echo $d . "<br>";
}
}
but if I want to show it as an image, i can't show the image. I tried this in the foreach:
if (!is_dir($outerDir . $d)) {
$file = $outerhDir . $d;
echo 'img src="' . $outerDir . $d . '> <br>';
echo "<img src='" . $outerDir . '\\' . $d . "'alt='" . $d . "'> <br>";
}
But none of them work. I also tried the header and file_get_content like this:
if (!is_dir($outerDir . $d)) {
$file = $outerDir . $d;
header('Content-type: image/jpeg');
header('Content-Length:' . filesize($file));
$image = file_get_contents('$file');
echo $image . "<br>";
}
Even a simple html code wont work! Like this:
echo '<img src="x:\map\maps\more\needwonder.jpg"> <br>';
BUTTTT!! When I store one of the image in the same folder as my php file it will work! Like this:
echo '<img src="needwonder.jpg"> <br>';
But I dont want to put all my files in the same folder as the php file, because of security, technicaly, comfortable reasons AND I want to learn programming not to avoid issues or problems with my code.
So I hope I defined my problem good and hope that one of you guys and/or girls know the solution to help me out
If your image files aren't in a folder that's set up to be accessible by the web server, you can't display them on a web page. Simple as that. Put them in a web directory to use them.
It is possible to write a PHP script that proxies image files (kind of like what you're trying with file_get_contents()), but doing that requires a separate PHP file for the images, and it's best avoided unless absolutely necessary, as it can easily introduce security vulnerabilities, and performs poorly. If you do want to do that, though:
Link to the PHP script in place of an image, and pass it an identifier for the image as a parameter (e.g, <img src="img.php?image=blah">).
In that script, use that identifier to send the appropriate Content-Type, then use readfile() to output the image. (It's equivalent to echo file_get_contents(...), but more concise and performs better in some situations.)
MAKE ABSOLUTELY SURE that the parameter to that script cannot be manipulated to load a file that you didn't intend to make available. This includes trickery like ?image=../../wrong/file.

Creating a variable template to display a database content

I'm working on a database website currently and part of the project is to display the content of a database in a custom form using pixels to position the various fields.
I am however having trouble getting the code to work as the way I'm creating the form doesn't seem to work in a php tag, but It requires a WHILE loop in order to continue running until the database field is empty.
I'm only including the relevant code section so as to not clog up the post, all my coding is segmented so I can isolate bugs.
<?php
$usernameh = 20;
$units = "px";
for ($x=0; $x<=2; $x++) {
$pix=$value . $units;
<div style="position:absolute;left:10px;top:20px;width:100px;height:20px;z- index:5;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:15px;">Field Name:</span></div>
$value += 150;
}
?>
The idea is to substitute the 20px on the first line of the divcommand with $pix so that with each iteration the value increases by a set amount and separates out the entries.
I'm very new to php coding, as in only really started 2 weeks ago. I'm sure there is a simple solution to this but I'm not sure what question to ask Google to get the response I'm after.
I hope my problem makes sense. The database is in MySQL but all that is fine, its just the formatting I'm struggling with. Even without using a variable in the formatting code the script crashes while the php tag is in effect.
Can anyone offer any advice on where my problem is or suggest another alternative to this.
Thanks!
You need to break out of PHP ?> before HTML and then switch back to PHP <?php before more PHP code
$value = 20;
$units = "px";
for ($x=0; $x<=2; $x++) {
$pix = $value . $units;
?>
<div style="position:absolute;left:10px;top:<?php echo $pix ?>;width:100px;height:<?php echo $pix ?>;z- index:5;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:15px;">Field Name:</span></div>
<?php
$value += 150;
}
Or for the HTML you can echo it from PHP:
echo '<div style="position:absolute;left:10px;top:' . $pix . ';width:100px;height:' . $pix . ';z- index:5;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:15px;">Field Name:</span></div>';

Links from Array

I kind of have a problem.
Maybe I'm doing the hardest way, I do not really know, if you could I'll appreciate for your help.
I upload photo to FTP and URL was upload to mysql in this format (http:///claim/img/box.png, http:///claim/img/box.png) with specific ID
-----------------------------------
ID - URL
1 - http://*/claim/img/box.png, http://*/claim/img/box.png
-----------------------------------
And later I grab this data, and to generated form by ID.
I used explode to divide URL. And with FOR loop, I would display it perefctly, But here is the glitch. Because there is need to make export this form as a doc file and send email with this generated form. I can't end echo.
Because when I do only first part is send, but not images from array.
For example:
$email = "ID 1<br>(Its the start of the form)";
$photos = explode(',', $claim['photos_url']); $arrlength = count($photos);
for($x=0;$x<$arrlength;$x++) {
echo "<img src=".$photos[$x]."><br>"; }
echo "The end of form";
I similar cases I simply use echo "the start of text".$date."the end";
But now, I have no idea how to do it.
Thank you for your help in advance.
your code looks so incomplete , but i think you have an array some how that includes image urls
and you want to echo each one of them :
$images = array("http://127.0.0.1/img/1.jpg","http://127.0.0.1/img/2.jpg","http://127.0.0.1/img/3.jpg");
foreach($images as $image)
{
echo "<img src='".$image."' />";
}
im not sure if i understand u correctly but hope it helps...

Categories