Writing Database String To File

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mark007
Forum Newbie
Posts: 6
Joined: Mon Dec 01, 2008 5:28 am

Writing Database String To File

Post by mark007 »

Hi all,

I am trying to take a string from a database , and write it directly to a unix file by calling a simple shell script. Its all very easy but I can do everything but can't stop php messing with my database string. It is constantly reading parts literally, and other parts as being parts that need to be escaped. I don't want any of this. I want the string left untouched and written to file.

My shell script I know to be correct, because if i manually give it this string, hand escaped, it prints it correctly to file exactly.

So here is my code with my already read database value $db_value. My $db_value as seen in phpMyAdmin is the following (its a worst case scenario but I expect my shell script to work with this string exactly as is and untouched).

String in Database
\\\\"\\\\&\\\\'<\\\\>\\\\'\\\'\\'\'

Code: Select all

 
$argument=escapeshellarg($db_value);
$Shell_Command="/tmp/write_file.sh " . $argument;
 
Is this possible? PHP seems to do some removal of the \\\\ and it sees it as \\. The shell script only sees 2 starting \\'s.
Using addslashes($db_value) makes things even worse, because characters like " get a backslash put in front of them then, and my shell script sees too many backslashes (5 starting slashes instead of 4 written to file like \\\\\" instead of \\\\").

There is no combination that will let my shell script receive four simple starting slashes. I either get 2 (php treating \\ as \), or 5 (addslashes adding too many).

Thanks!
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Writing Database String To File

Post by jaoudestudios »

What code is in your shell script?

Try encoding your quotes etc with htmlentities then there will be nothing that requires escaping.
mark007
Forum Newbie
Posts: 6
Joined: Mon Dec 01, 2008 5:28 am

Re: Writing Database String To File

Post by mark007 »

oh my shell script is simply

echo "$1" > /tmp/output.txt

and as I say this prints whatever I give it so far.

Wouldn't htmlentities add escapes to things like > and < which I don't need escapes for as they will get sent to my shell script then? Any other commands to get around the php removal of \'s when infront of another \ or '.

These are the only two characters which are causing me problems, ie \\ and \' being treated as \ and '.

From the php documentation on strings, it seems the following line is the explanation of whats happening with my variable.

"To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash before a single quote, or at the end of the string, double it (\\). Note that attempting to escape any other character will print the backslash too."

I cannot seem to get a combination of str_replace's that can do the above line for me.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Writing Database String To File

Post by jaoudestudios »

Why do you use the shell to create the file? why not php?
mark007
Forum Newbie
Posts: 6
Joined: Mon Dec 01, 2008 5:28 am

Re: Writing Database String To File

Post by mark007 »

Well my shell script will do a whole host of things, writing it to a file is just my first step to see can i get the string accross from php to shell without losing characters.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Writing Database String To File

Post by jaoudestudios »

Oh I see. Fair enough.

No htmlentites will use html entities, take a look...
http://www.jaoudestudios.com/entities.php
and
http://uk3.php.net/manual/en/function.htmlentities.php
mark007
Forum Newbie
Posts: 6
Joined: Mon Dec 01, 2008 5:28 am

Re: Writing Database String To File

Post by mark007 »

Thanks alot for all of the links.

Unfortunately, htmlentities adds more letters / chars that wern't there originally than I would like (none added) as in the following example from the documentation. As I say, the only two characters that dont seem to get accross from database to shell script are the backslashes in the original string, and the single quotes in the original string.

$str = "A 'quote' is <b>bold</b>";

// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str);

// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str, ENT_QUOTES);
Post Reply