Not getting desire output - php

What I want to do is,
I am initializing $message variable for mail body.
And I has to iterate through loop if multiple files are there.
So how can i iterate loop and store values in a variable.
Like,
$message = "Dear " . $fname . " <br/>
<b>Manuscript and Other Documents :</b> <br/>
Source File : " .$file1 . " <br/>
Source PDF File : ".$file2 . " <br/>
Cover Letter : " . $file3 . "<br/>
Supplementary Files : " . while($row=mysql_fetch_array($supplementary)){ echo $row[0] } . ";
So how can i do this if i have multiple supplementary files?

To do this, simply append the data to the message variable within the while loop using the string concatenation operator.
For example:
$message = "Dear " . $fname . " <br/><b>Manuscript and Other Documents :</b><br/>
Source File : " .$file1 . "<br/>Source PDF File : ".$file2 . " <br/>
Cover Letter : " . $file3 . "<br/>Supplementary Files : ";
while($row = mysql_fetch_array($supplementary)) {
$message .= echo $row[0] . '<br />';
}

First, do this:
$suplArray = array();
while($row=mysql_fetch_array($supplementary)){
$suplArray[] = $row[0];
}
Now, you have the strings in an array. I don't know what format you want, but you can do something like
$suplStr = implode(',', $suplArray);
Then in your string, you can have:
... "Supplementary Files : " . $suplStr;

Related

How to remove all the spaces and new line and make one single string?

I have following string (using $_POST), how to remove all the new line, spaces and make it as a absolute single line?
Physical Address. . . . . . . . . : E8-6A-64-DE-48-60
Physical Address. . . . . . . . . : 04-EA-56-08-E6-8F
Physical Address. . . . . . . . . : 06-EA-56-08-E6-8E
Physical Address. . . . . . . . . : 04-EA-56-08-E6-8E
Physical Address. . . . . . . . . : 04-EA-56-08-E6-92
Not always stable?
$request= mysql_real_escape_string(trim($_POST['request']));
$request_sql =str_replace("\r\n",'', $request);
$request_sql = str_replace("\\r\\n",'', $request_sql);
echo $request_sql;
trim only strips spaces at end and start of the string and you should strip \r and \n individually.
try this:
$request= mysql_real_escape_string($_POST['request']);
$request_sql =str_replace("\n",'', $request);
$request_sql = str_replace("\r",'', $request_sql);
$request_sql = str_replace(" ",'', $request_sql);
echo $request_sql;
Try using regex.
$request = $_POST['request'];
//Remove all characters that are not A-Z, a-z, 0-9 or '.', ':' or '-'
$request_sql = preg_replace("/[^A-Za-z0-9.:-]/", '', $request );
try:
$str = 'Physical Address. . . . . . . . . : E8-6A-64-DE-48-60
Physical Address. . . . . . . . . : 04-EA-56-08-E6-8F
Physical Address. . . . . . . . . : 06-EA-56-08-E6-8E
Physical Address. . . . . . . . . : 04-EA-56-08-E6-8E
Physical Address. . . . . . . . . : 04-EA-56-08-E6-92';
echo str_replace(" \n", '', $str);
Output:
Physical Address. . . . . . . . . : E8-6A-64-DE-48-60Physical Address. . . . . . . . . : 04-EA-56-08-E6-8FPhysical Address. . . . . . . . . : 06-EA-56-08-E6-8EPhysical Address. . . . . . . . . : 04-EA-56-08-E6-8EPhysical Address. . . . . . . . . : 04-EA-56-08-E6-92
After replacing the \n you can use mysql escape string to avoid sql injection.
The problem with your code is that mysql_real_escape_string will not only escape ' and " but it will escape other characters like \n and \r which you want to remove.
It will replace new line characters with a backslash character followed by l characters
so removing newlines, carriage return after they have been escaped will result in a string with extra backslashes \ and n and r characters.
Check out this
<?php
$originalString =
"Line1
Line2
";
// CASE 1 WRONG RESULT
$string1 = mysqli_real_escape_string($con, $originalString);
$string1 = str_replace("\n", '', $string1);
echo "escape then replace result \n";
echo $string1 . "\n";
//CASE 2 EXPECTED RESULT
$string2 = str_replace("\n", '', $originalString);
$string2 = mysqli_real_escape_string($con, $string2);
echo "replace then escape result \n";
echo $string2 . "\n";
this will output
escape then replace result
Line1\nLine2\n
replace then escape result
Line1Line2
So to correct your code
$request_sql =str_replace(["\n", "\r", " "],'', $_POST['request']);
$request= mysql_real_escape_string($request_sql);
echo $request_sql;
Please don't use mysql_real_escape_string , instead use prepared statements, here an answer for how to switch to them, they will make your life much more easier and safer.

Searching for file in directory using glob, not returning results

I'm trying to make a small script for retrieving configuration files containing mapping instructions for a product import, using php glob. But I fail miserably.
What I have right now:
echo "Shop type: " . $this->shopType . '<br />';
echo "Shop version: " . $this->shopVersion . '<br />';
echo "Glob search: config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php";
foreach (glob("config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
exit;
$this->shopType represents the name of the shop from which the export file should be imported. E.g. ccvshop.
$this->shopVersion represents the version of the shop, to be able to write different import mappings for different versions of the shop types.
The value of shopType = ccvshop.
The value of shopVersion = 11.2.
Which would make the search string in the glob function:
config/mappings/ccvshop_11.2.php.
Unfortunately my result is empty, and I can't see what I'm doing wrong.
Any help would be appreciated.
I added an image representing my directory structure:
glob() returns an array of matching files. It looks like you already know the name of the file that you want to read. Try something like this:
$filename = "config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php";
if (file_exists($filename))
echo "$filename:" . filesize($filename);

Trying to load an php array into an echo that prints an image

Hi I am trying to get images to load into a page using the file names from an array,
This is what I have so far
<?php
$i=0;
$img=array("1.png","2.png","3.png","4.png");
while ($i<count($img))
{
echo "<img class='loadin' alt='imgg' src=" . "'http://www/images/" . $img[i] . "'" . "/" . ">" . "<br/>";
$i++;
}
?>
It seems to ignore the file name and just enters:
http://www/images/
as the source and ignores the file name from the array
Any Help would be great Thanks
Mikey
You forgot the dollar sign with your $i variable: $img[$i]
EDIT:
(btw. using a foreach-loop would be easier...)
foreach($img AS $filename) {
echo "<img class='loadin' alt='imgg' src='http://www/images/" . $filename . "'/><br/>";
}

File flushed when attempting to update contents

I have written a very simple page counter and a logging script that increments a counter stored in a file and logs information about the client's operating system and which browser they use. It's a simple spare time project I've been working on, and as such it is extremely rudimentary, writing the counter and the logged information in a designated folder for each page on the site, in a new file for each day.
The thing is, I recently used blitz.io to test my site, and when I ran a "Rush" of 250 requests per second, the counters and the logs were completely flushed, except for the very last query.
I'm not perfectly sure what happened, but I suspect something along the lines of PHP not properly finishing up the previous query before taking on the next one.
I use file_get_contents()/file_put_contents() for the both of them, instead of file(). Would changing to file() solve the problem?
Here's the counter:
$filename = '.' . $_SERVER['PHP_SELF'];
$counterpath = '/Websites/inc/logs/counters/total/' . getCurrentFileName() . '-counter.txt';
$globalcounter = '/Websites/inc/logs/counters/total/global-counter.txt';
if (file_exists($counterpath)) {
$hit_count = file_get_contents($counterpath);
$hit_count++;
file_put_contents($counterpath,$hit_count);
}
else {
$hit_count = "1";
file_put_contents($counterpath, $hit_count);
}
And here's the logger:
$logdatefolder = '/Websites/inc/logs/ip/' . date('Y-m-d',$_SERVER['REQUEST_TIME_FLOAT']);
$logfile = $logdatefolder . "/" . getCurrentFileName() . '-iplog.html';
$ua = getbrowser();
if (false == (file_exists($logdatefolder))) {
mkdir($logdatefolder);
}
function checkRef() {
if (!isset($_SERVER['HTTP_REFERER'])) {
//If not isset -> set with dummy value
$_SERVER['HTTP_REFERER'] = 'N/A';
}
return $_SERVER['HTTP_REFERER'];
}
/* Main logger */
$logheader = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-US\"><head><title>" . getCurrentFileName() . " log</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body>";
$logentry = date("Y-m-d, H:i:s, O T") . ":" .
"<br />- Requesting: http://giphtbase.org" . $_SERVER['REQUEST_URI'] .
"<br />- Arriving from: " . checkRef() .
"<br />- Browser: " . $ua['browser'] .
"<br />- Full browser name: " . $ua['name'] .
"<br />- Operating system: " . $ua['platform'] .
"<br />- Full user agent: " . $ua['userAgent'] .
"<br />";
$logfooter = "<!-- Bottom --></body></html>";
if (file_exists($logfile)) {
$logPage = file_get_contents($logfile);
$logContents = str_replace("<!-- Bottom --></body></html>","",$logPage);
file_put_contents($logfile, $logContents . $logentry . $logfooter);
}
elseif (false == (file_exists($logfile))) {
file_put_contents($logfile, $logheader . $logentry . $logfooter);
}
You should use the FILE_APPEND flag in your file_put_contents() otherwise you will only ever see the last entry:
file_put_contents($logfile, $logContents . $logentry . $logfooter, FILE_APPEND);
As for the counter, it looks like the file is trying to be written to too many times by different threads, causing it to be inaccessible. You should either use a database, or create a file_lock, or create temporary files and run a cronjob to do the math.

stored db url does not echo image

I have a MySQL db with a table, that has the following field:
txtProductBowlImage: example " C:\BowlPhotos\Thumbs\MapleTi07-030tmb.jpg"
I want to use the data in that field to produce an image that I can print to a text file.
I read the data then try to echo it to the screen...and I see a small icon, but no image.
Here's the code:
<?php
$con = mysql_connect("localhost","xxxxxxx","zzzzzzzzzzzzzzz");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("scwdb", $con);
$result = mysql_query("SELECT * FROM tblsplintersbowlinventory WHERE txtVenue = 'Shoreline'");
while($row = mysql_fetch_array($result))
{
echo $row['intProductID'] . " " . $row['txtProductBowlCode'] . " " . $row['txtProductBowlImage'] . "]";
echo "<br />";
echo "<br />";
echo 'txtProductBowlImage: ' . $row['txtProductBowlImage'];
echo "<br />";
$img = $row['txtProductBowlImage'];
echo '...............$img: ' . $img;
echo "<br />";
echo "<img src=" . $img . ">";
echo "<img src='C://BowlPhotos//Thumbs//Ash07-013_btmb.jpg'>";
echo "<img src='Ash07-013_btmb.jpg'>";
echo "<br />";
echo "==================";
echo "<br />";
}
mysql_close($con);
?>
Note that I try 3 times to show an image:
1: using the url from the db and putting it in an echo statement
this url uses the '\' separater in the data.
2: imbedding a sample of the actual text path and filename in an echo statement
this url uses the '/' separater in the url.
3: imbedding only the filename, no path, in an echo statement
When I run this, I get the following output:
Echo #1 & #2 produce the icon, echo #3 shows a local copy of the image in the same folder as the php.
From this it appears that it is the path that is not being used, either with the '\' or '/' separater.
I assume the '\' may be seen as an escape, but why doesn't the url with the '/' work?
All below is wrong seeing as "Linking to local resources is disabled in all modern browsers due to security restrictions"
To link to a local image you use the format
<img src='file:///C:/path/to/image/pic.jpg'>
You can replace the backslashes with forward slashes using
$img = str_replace('\\', '/', $row['txtProductBowlImage']);
Now you can add the file:/// and the single quotes around the path
like so
echo "<img src='file:///" . $img . "'>";
With double quotes you could skip the dot operator to append strings
and use
echo "<img src='file:///$img'>";
The dollar $img will be recognized as a variable
It seams your trying to get a file from your local computer. So you have to run a webserver that hadle php
You might be trying to read an image where the server dont have acess.
try to place the file with your html or php file
OK, with JonOsterman's greatly appreciated aid, the final working code is:
<?php
$con = mysql_connect("localhost","root","Madge1938");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("scwdb", $con);
$result = mysql_query("SELECT * FROM tblsplintersbowlinventory WHERE txtVenue = 'Shoreline'");
while($row = mysql_fetch_array($result))
{
echo $row;
echo "<span>" . $row['intProductID'] . " " . $row['txtProductBowlCode'] . " " . $row['txtProductBowlImage'] . "</span>";
echo "<br />";
$img = $row['txtProductBowlImage'];
echo "<span> Original img: " . $img . " </span>";
$img = str_replace('\\', '/', $img);
echo "<br />";
$img = str_replace('C:/BowlPhotos/Thumbs/', 'Thumbs/', $img);
echo "<span>Folder img: " . $img . " </span>";
echo '<img src="' . $img . '">';
echo "<br />";
$img = str_replace('Thumbs/', '', $img);
echo "<span>Local img: " . $img . " </span>";
echo '<img src="' . $img . '">';
echo "<br />";
echo "<span>done</span><br />";
}
mysql_close($con);
?
There were a number of changes, but the biggest surprise was that the php could not open a folder not in the same folder or a sub folder of the php file! When I moved the entire folder to a sub of the php folder, the code above runs!
Also, as seen above, the HTML does not need a "file:///" for a local file...
And I should mention, this is intended to run only on my local server - not on the web. It is just for my own use to keep track of the wood bowls I make and sell.
Again, many thanks to Jon...

Categories