Adding a web address to a <a href="value"> - php

I have an upload script that write a index.html file, I modified it to mail me the results, however the point to the root, since the email isn't in the root, the image doesn't load.
How do I append the code to add "http://www.home.html/uploaded" prior to the ". $value ." so that the images show up in the email.
Here is portion of PHP that assigns the images to a $result:
// Process value of Imagedata field, this is JPEG-files array
foreach ($_FILES[Imagedata][name] as $key => $value)
{
$uploadfile = $destination_dir . basename($_FILES[Imagedata][name][$key]);
if (move_uploaded_file($_FILES['Imagedata']['tmp_name'][$key], $uploadfile))
{
$result .= "File uploaded: <a href='". $value . "'>" . $value . "</a><br>";
}
}
//
$result .= "<br>";
Here is what I'm now receiving in the email, :
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Upload results</title>
</head>
<body>
AdditionalStringVariable: pass additional data here<br><a>
href='gallery_grannylovesthis_l.jpg'><img border = '0'
src='QIU_thumb_gallery_grannylovesthis_l.jpg'/></a><br><br><br>File uploaded: <a
href='gallery_grannylovesthis_l.jpg'>gallery_grannylovesthis_l.jpg</a><br><br>
GlobalControlData:
PHOTO_VISIBILITY : 2<br>
GlobalControlData:
PHOTO_DESCR : Requiredtest<br>
GlobalControlData:
PHOTO_TITLE : Requiredtest<br><br>gallery_grannylovesthis_l.png<br> control: , value: <br>
</body>
</html>
Thanks in advance for any guidance...I have a feeling it's something simple.

Like this:
<a href='http://www.home.html/uploaded/". $value . "'>"

Your code has a malformed tag:
<a>
href='gallery_grannylovesthis_l.jpg'><img border = '0'
It should be like this:
<a href='gallery_grannylovesthis_l.jpg'><img border = '0'
Notice on yours you have href (you added a > when there should not be one)

Related

Accessing properties in an include when setting that include in to a file

I am trying to create a template html page which I will call via an include to set to a variable, this variable will then be used to set the value of a new file. I need the variables in the included file to be resolved so that the values are populated correctly.
To demo imagine these files:
main.php
$someVar = "someValue";
$fileText = include "aTemplate.php";
$newFileName = 'someFile.php';
if (file_put_contents($newFileName, $fileText) !== false) {
echo "File created (" . basename($newFileName) . ")";
} else {
echo "not created";
}
aTemplate.php
<?php
return
'<!doctype html>
<html lang="en">
<head>
<title><?php echo $someVar; ?></title>
</head>
</html>'
?>
What is currently happening is that the variables stay unresolved and hold no value so in the created html file the title is:
<title></title>
Instead of
<title>someValue</title>
How can I change the 'aTemplate.php' file to resolve the properties set in 'main.php'?
Just use this at your aTemplate.php:
<?php
return '<!doctype html>
<html lang="en">
<head>
<title>'. $someVar .'</title>
</head>
</html>';
?>
There are a couple of problems with your template, firstly as you have the HTML in single quotes, this won't do any of the string substitutions. Secondly, your trying to do a PHP echo whilst in HTML in PHP. I've used Heredoc to enclose the HTML as it allows any sorts of quotes and will also do the replacements.
The substitution of the value is just replaced by adding $someVar directly into the string.
So aTemplate.php becomes...
<?php
return <<< HTML
<!doctype html>
<html lang="en">
<head>
<title>$someVar</title>
</head>
</html>
HTML;
You should echo those string in page instead of return command.
The keyword return is used inside a function while your file is not a function. The browser simply puts what's inside include file has to offer. In you case it is HTML string which should be outputted using echo command.
Also the server executes code in top to bottom and left to right. Thus the variable $someVar will be accessed in aTemplate.php file.
Use below code instead to work
main.php
$someVar = "someValue";
$file = 'aTemplate.php';
// Open the file to get existing content
$fileText = include "aTemplate.php";
$newFileName = 'someFile.php';
// Write the contents back to the new file
if (file_put_contents($newFileName, $fileText) !== false)
{
echo "File created (" . basename($newFileName) . ")"; }
else {
echo "not created";
}
aTemplate.php
<!doctype html> <html lang="en"><head>
<title><?php echo $someVar;?></title>
</head>
</html>

simplexml_load_file and 100mb+ XML file: Takes a lot of time to load and result in a Timeout

I've been using simplexml_load_file to parse a XML URL, however, the file size is above 100mb and instead of loading only the nodes, what's happening is that the script is loading the whole XML file before the nodes are extracted and parsed, what is resulting in a page TimeOut.
I'm using the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Boutique</title>
</head>
<body>
<?php header ('Content-type: text/html; charset=UTF-8'); ?>
<!-- --><link rel="stylesheet" href="/va/artigos-complexos/afilio/afilio-vitrine.css" type="text/css" />
<div><p class="ofertasdotopovitrine">Conheça nossas super ofertas!</p>
</div>
<div class="mainproductebayfloatright-bottom">
<?php
function parse($url, $offset = 1, $limit = -1)
{
$xml = simplexml_load_file($url);
$limitCriteria = '';
if ($limit > 0) {
$limitCriteria = 'and position() <= ' . ((int)$offset + (int)$limit + 1);
}
$products = array();
$path = sprintf('//produto[position() >= %s %s]', (int)$offset, $limitCriteria);
foreach($xml->xpath($path) as $product) {
$products[] = array(
'nome' => $product->nome,
'preco_promocao' => $product->preco_promocao,
'description' => $product->descricao,
'link_imagem' => $product->link_imagem,
'link_produto' => $product->link_produto,
'preco_normal' => $product->preco_normal,
'parcelas' => $product->parcelas,
'vl_parcelas' => $product->vl_parcelas
);
}
return $products;
}
//XML GOES HERE
$products = parse('http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895987&token=53e355b0a09ea0.74300807&progid=1010&format=XML', 5, 5);
?>
<?php
foreach ($products as $product) {
print '<div class="aroundebay"><div id="aroundebay2">';
/* */
print '<div class="titleebay"><a target="_blank" rel="nofollow" href="'. $product['link_produto'] . '">' . $product['nome'] . '"</a></div>';
print '<div class="mainproduct"><a target="_blank" rel="nofollow" href="' . $product['link_produto'] . '"><img style="height:120px" src="' . $product['link_imagem'] . '"/><br/>';
//print "De:;&nbspR$". $preco_normal . "<br/>";
print '<span>Apenas&nbspR$' . $product['preco_promocao'] . '<br/></a></span></div>';
//print "Em&nbsp" . $parcelas . "x de&nbsp:&nbspR$" . $vl_parcelas . "</a></span></div>";
print '</div></div>';
}
?>
</div>
</body>
</html>
The CSS is irrelevant.
The script works just fine when you use a smaller XML, such as this one:
http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895835&token=53e355b0a09ea0.74300807&progid=1681&format=XML
Would it be possible to load only the, for exemplo, 10 first nodes of the xml without having to load the whole file first?
I'm also accepting suggestions in other languages, such as jQuery.
Thanks in advance. You can also change the file format to JSON and RSS, just change format=XML to format=JSON or format=RSS.

Extracting images and determining the dimensions of the images

I have a php code that will extract and retrieve all the images in a website. How do I modify the code so that the dimensions(width and height) of the images are shown as well?
This is the php coding:
<?php
$page_title = "MiniCrawler";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><?php print($page_title) ?></title>
</head>
<body>
<?php
ini_set('display errors',1);
error_reporting(E_ALL|E_STRICT);
Include simple_html_dom.php.
include_once ('simple_html_dom.php');
// Add the url of the site you want to scrape.
$target_url = "http://www.alibaba.com/";
// Let simple_html_dom do its magic:
$html = new simple_html_dom();
$html->load_file($target_url);
// Loop through the page and find everything in the HTML that begins with 'img'
foreach($html->find('img') as $link){
echo $link->src."<br />";
echo '<img src ="'. $link->src.'"><br />';
}
?>
</body>
</html>
Thanks
First you would have to check, if the $link->src string already has the domain name at the beginning:
<?php
if(substr($link->src, 0, 4) == "http"){
// url already complete
$path = $link->src;
}else if(substr($link->src, 0, 1) == "/"){
// path starts absolute
$path = $target_url . $link->src;
}else{
// path starts relative -> http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php
}
?>
Then: Request the files dimensions via the getimagesize() function.
<?php
list($width, $height, $type, $attr) = getimagesize($path);
echo '<img src ="'. $link->src.'" width="' . $width . '" height="' . $height . '"><br />';
?>

How to put different images on foreach in php

i'm doing a training in PHP, i'm still learning, and i'm trying to make a "poor video-game advertise", i got my php file in a folder, and a folder named "img" inside the same folder. Well, all i want to know is hows do i make to show a different image for a different game ? I'm trying to at least show Watch_Dogs img, but it isn't showing as well... And i need to show the correct image for the correct game, can anyone help me out ? here's the code, it's pretty poor i know...:
<!Doctype HTML>
<html lang="pt-br">
<head>
<title> Repetição em PHP </title>
</head>
<body>
<?php
$arrayGames = array("Watch_Dogs " => " R$199,90",
"Dead Space " => " R$149,90",
"Wolfenstein " => " R$189,90");
foreach($arrayGames as $titulo => $preco){
echo"<p>Jogo: " . $titulo . "<img src='img/watch_dogs.jpg' /> </p>" . $preco;
}
?>
</body>
</html>
<?php
$arrayGames = array("Watch_Dogs " => array(" R$199,90","watch_dogs.jpg"),
"Dead Space " => array("R$149,90","dead_space.jpg"),
"Wolfenstein " => array("R$189,90","wolfenstein.jpg")
);
foreach($arrayGames as $titulo => $preco){
echo"<p>Jogo: " . $titulo . "<img src='img/{$preco[1]}' /> </p>" . $preco[0];
}
?>
The image you are showing must be relative to your php file location.
Ex:
CURRENT_FOLDER
|_ file.php
|_ <img>
|_ watch_dog.jpg
|_ ... .jpg
You could store the data on your array as multi-dimenional:
$games = array("Watch_Dogs " => array("price"=>" R$199,90","img"=>"http://placehold.it/350x150"),
"Dead Space " => array("price"=>" R$199,90","img"=>"http://placehold.it/350x150"),
"Wolfenstien " => array("price"=>" R$199,90","img"=>"http://placehold.it/350x150")
);
$html = '';
foreach($games as $game_title =>$price_img)
{
$html .= "<p>Jogo: {$game_title} <img src='{$price_img['img']}' /> {$price_img['price']} </p>";
}
echo $html;
DEMO
You need to derive the leading part of the file name from the array key. And you need to remove the trailing spaces on the key names before appending the .jpg.
In my suggested solution, I've elected to modify the key to remove the trailing space; if the trailing space is important, you'll need to rtrim the key before using it.
<!Doctype HTML>
<html lang="pt-br">
<head>
<title> Repetição em PHP </title>
</head>
<body>
<?php
$arrayGames = array("Watch_Dogs" => " R$199,90",
"Dead Space" => " R$149,90",
"Wolfenstein" => " R$189,90");
foreach($arrayGames as $titulo => $preco ) {
echo"<p>Jogo: " . $titulo . "<img src='img/" . $titulo . ".jpg'/> </p>" . $preco;
}
?>
</body>
</html>

displaying an image from a directory

I need help in displaying my images from the directory images on my public_html directory. It's been uploaded, but it doesn't appear when I run the below php script. Please does anyone know why?
<?php
include_once("connect.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM authors") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
Echo "<img src=http://giveaway.comli.com/public_html/images/".$info['photo']."> <br>";
Echo "<b>Name:</b> ".$info['name'] . "<br> ";
Echo "<b>Title:</b> ".$info['title'] . " <br>";
Echo "<b>Phone:</b> ".$info['description'] . " <br>";
Echo "<b>Link<br>";
Echo "<b>Country:</b> ".$info['country'] . " <hr>";
}
?>
</body>
</html>
Is your public_html directory really not your site's document root? Perhaps you mean to say:
Echo "<img src=http://giveaway.comli.com/images/".$info['photo']."> <br>";
Where /images is a directory at the site document root. In a typical hosting situation, public_html would be the site document root, meaning that referenced from the web (rather than the file system), it is /.
What is the document root of your site? I'm guessing it's /home/.../public_html, which means the URL to access the images would really be http://giveaway.comli.com/images. Absolute urls of the sort you're using are really only necessary if you're using multiple different hosts/servers for your site, and/or are intending for the html to be portable. You could probably get away with using just <img src="/images/...">.
As I can see your document roo directory is public_html, so URL path to your photo will be look like this $image_url = "/images/" . $info['photo'];

Categories