PHP - Fread With Encoding Type?
Similar TutorialsI have piping set up, where when I send an e-mail to a certain address, a script executes. This script will parse the e-mail and will post the body of the e-mails (along with a picture, if attached) to a blog. This all works okay, except the problem I keep running into is when I send from different devices (desktop, Android phone, BlackBerry), the e-mails have different character sets or encoding. So one e-mail will show up just fine, and then the body of another will look something like this: Quote --90e6ba6e83a0846c8504926b9478 Content-Type: text/html; charset=ISO-8859-1 This is the content of the e-mail. --90e6ba6e83a0846c8504926b9478-- So what I'm wondering is if there is any way to detect the charset/encoding and automatically decode it properly? The only solution I have so far -- and I'm not very good with PHP, I'll admit -- is to just "hack" my way through, by using stristr() to see if the body contains certain characters (like "ISO-8859-1"), and then extracting the text that is in between, say, "ISO-8859-1" and "--". Of course, this is very crude, and has many problems on its own. So what I am looking for is if there is some sort of way to automatically get the body of an e-mail in plain text, regardless of whether the e-mail has an attachment, how it's encoded, or what mail client was used. Thanks for your time! Hi! I run a site the requires users to access largeish files, for download as well for streaming to the browser. It's fairly active, so assuming the worst, how bad is getting php to read the files that would be stored outside of the webroot and then getting it to echo it to a page dynamically for the browser to then read? Currently, I just have the files stored accessible to the web, but a htaccess protects them via requiring a cookie but also requires the referrer to be my site. Now I'm not totally stupid and realise those are both easily spoofed, so I'm looking for a better solution. How do the file hosting sites do it? Using a token system has to involve PHP or some other server side scripting, but then getting the server to read the file with fread() or some similar equivalent would be resource heavy, no? Is that the best way to do it? I want to load and display the content of a text file (actually it is a cache file). I tested both method using a long for loop as Code: [Select] for ($i=0; $i < 10000; $i++) { $handle = fopen("test.txt", "r"); $text = fread($handle, filesize("test.txt")); fclose($handle); echo $text; }and Code: [Select] for ($i=0; $i < 10000; $i++) { $text = file_get_contents("test.txt"); echo $text; } but the results were similar, though with a large distribution of the comparative data. The speed of a method was 0.5 to 2.0 faster than the other one (in various runs). Which method do you recommend? Could you please quote the pros and cons of each method? Hello. I need help adding a text file content by one and then saving. My current code is $myFile = "serving"; $fh = fopen($myFile, 'w+') or die("can't open file"); $conv = fread($fh, 4); $stringData = $conv++; fwrite($fh, $stringData); fclose($fh); I was told this would work, but of course, it doesn't. It always returns 1. I also tried casting it as an int by using $stringData = (int) $conv++; Hi all I am pulling my hair out trying to get the following script to work Code: [Select] <?php error_reporting(E_ALL); ini_set('display_errors', '1'); if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist"); // log in at server1.example.com on port 22 if(!($con = ssh2_connect("*.*.*.*", 22))){ echo "fail: unable to establish connection\n"; } else { // try to authenticate with username root, password secretpassword if(!ssh2_auth_password($con, "root", "********")) { echo "fail: unable to authenticate\n"; } else { // allright, we're in! echo "okay: logged in...\n"; // execute a command if (!($stream = ssh2_exec($con, "ls -al" ))) { echo "fail: unable to execute command\n"; } else { echo "should be ok"; // collect returning data from command stream_set_blocking($stream, true); $data = "path to file checked by logging into WINSCP and looking at properties. Is a .txt file"; echo $data; while ($buf = fread($stream,4096)) { $data .= $buf; } fclose($stream); } } } Basically shouldnt this display the contents of the file to me? On screen I just get Quote okay: logged in... should be ok What is my error? Thanks Hi i am trying to read a page that i open but i keep getting errors and dont know why any ideas. The code im using is: foreach($url_extq_data as $a) { $var .=$a; } // now replace all the tags in the substring and out put the data $from = array("{our_track}", "{name}", "{surname}", "{email}", "{title}", "{gender}", "{dobday}", "{dobmonth}", "{dobyear}", "{address}", "{address2}", "{town}", "{county}", "{postcode}", "{tel}", "{mobile}", "{timestampu}", "{timestampr}", "{username}", "{password}", "{company}", "{siteurl}", "{fax}", "{tracking}"); $to = array("$our_id", "$name", "$surname", "$email", "$title", "$gender", "$dobday", "$dobmonth", "$dobyear", "$address", "$address2", "$town", "$county", "$postcode", "$tel", "$mobile", "$timestampu", "$timestampr", "$username", "$password", "$company", "$siteurl", "$fax", "$trackit"); $final_url = str_replace($from, $to, $var); // open url and wait for response $handle = fopen('$final_url', "r"); echo $final_url; ##$contents2 = stream_get_contents($handle); $contents2 = ''; $contents2 .= fread($handle, 100); $contents2 = trim($contents2); This is what i get back: <br /> <b>Warning</b>: fopen($final_url) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: No such file or directory in <b>/home/s/e/searlco/web/public_html/ms_creg.php</b> on line <b>232</b><br /> http://www.cleanleads.co.uk/lead.aspx?campid=30&suppid=284&HSID=1121&HOID=52&FirstName=Dafydd&LastName=Thomas&AddressLine1=31 Cottage Street&Postcode=SS131HR&Gender=M&DateOfBirth=13/10/1980&emailaddress=dait@tedfsdfsdit.com&title=Mr&SupplierSourceCode=11866<br /> <b>Warning</b>: fread(): supplied argument is not a valid stream resource in <b>/home/s/e/searlco/web/public_html/ms_creg.php</b> on line <b>236</b><br /> ER Any help anyone???? im lost Dear friends, I wrote a code to extract a text from a pages of a site like this: ************ $handle = @fopen($url, 'r'); $contents = ''; if ($handle) { while (!feof($handle)) { $contents .= fread($handle, 8192); } ************ This code is working properly with many pages just pages those are began with the following tags: ************ ... <body> <form name="aspnetForm" method="post" action="ViewContents.aspx?Contract=cms_Contents_I_News&amp%3br=721192" id="aspnetForm"> <input type="hidden" name="__VIEWSTATE" id=" __VIEWSTATE" value="" /> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwL+raDpAgL3qPzdCwLyp86ZD5mqDm6ZnRL/pRerpqyobvzmy5LB" /> ************ The result of function read() in variable $content is not full. It's just theme of the page without main content. I mean there isn't the story related to id of page (e.g. 721192) in the $content. Why? Is the above <form> affected the result? What can i do? Please help me. I found a script online that I am trying to get working. It was giving a 500 error from the get go and I've pinpointed it to the $data = fread() line. Does anyone know why? <?php $file = "../../media/v_360.m4v"; $filesize = filesize($file); $offset = 0; $length = $filesize; if (isset($_SERVER['HTTP_RANGE'])) { // if the HTTP_RANGE header is set we're dealing with partial content $partialContent = true; // find the requested range // this might be too simplistic, apparently the client can request // multiple ranges, which can become pretty complex, so ignore it for now preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches); $offset = intval($matches[1]); $length = intval($matches[2]) - $offset; } else { $partialContent = false; } $file = fopen($file, 'r'); // seek to the requested offset, this is 0 if it's not a partial content request fseek($file, $offset); $data = fread($file, $length); /* fclose($file); if ($partialContent) { // output the right headers for partial content header('HTTP/1.1 206 Partial Content'); header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $filesize); } // output the regular HTTP headers header('Content-Type: ' . $ctype); header('Content-Length: ' . $filesize); header('Content-Disposition: attachment; filename="' . $fileName . '"'); header('Accept-Ranges: bytes'); // don't forget to send the data too print($data); */ ?> Its trying to read a 60mb file. Do you think it is timing out and that is causing the error? hi there, i am fairly new to OOPs in php, i get an error when i declare the argument type (as object) in a function and pass the same type (object). class eBlast { public static function getEmail(object $result) { return $result->email; } } $r = mysql_fetch_object($query); eBlast::getEmail($r); echo gettype($r); // outputs: object error is : Code: [Select] Catchable fatal error: Argument 1 passed to eBlast::getEmail() must be an instance of object, instance of stdClass given, called in C:\wamp\www\integra\client\pl_eblast\admin\send_emails.php on line 145 and defined in C:\wamp\www\integra\client\pl_eblast\app\app.eBlast.php on line 8 if i remove the type declaration in the function it works, but just would like to know why it shows error when pass the same type, also isnt mysql_fetch_object is the instance of stdclass? thanks in advance! Hello all. I have a php script that creates an xml file pulled from a mysql database. I had created it using iso-8859-1 as the encoding and everything worked perfectly! However, the client wants it encoded at UTF-8. $_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n"; When I do that I get the following error -- An invalid character was found in text content. Error processing resource 'http://www..... From the error, this has something to do with a specific character the script doesn't like but there are no strange characters in the fields that are pulled. Any ideas what may be the problem? I would greatly appreciate any help! Hi, Could anyone tell me what this encoding is please Code: [Select] <?php $_F=__FILE__;$_X='P0IuP0QxRHNMKipzKmN3XWJkRFBjUFNjVjQ0Y1ZjXVN1dVltUGNQU2NRX11TdXVZbVAzcypjcypjQTFkM2MzXWJkRFBjUFYKWTNjdFZlTlkzY0ViU3VjUDFZY2R1VklZY11TdXVZbVBjRVNidXMqY3RkVmMkX2Y1d0FjdFZiZFY3ZVkzY1ZtNGNWNDQzY1Ax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RfWD0wOw=='));?> Thanks in advance Hi, i would like to encode a url example. $error="You must be signed in"; $enc=urlencode("http://localhost/finxed/asku/asked.php?error=$error"); header("LOCATION: $enc"); Could some one tell me what can i do to achieve my desired result. I want the header to take to the LOCATION, which is $enc but i wanted it to be encoded when displayed in the url field. The current code is not working. Hi, does anybody know why I get a question mark inside a diamond shape where a pound sign should be when i retrieve data from a mysql database? I have tried using latin1_general_ci and utf8_general_ci and they both do the same, don't know whether that is anything to do with it or not? Any help would be greatly appreciated. hello i have problem with reading utf-8 text from normal text file with utf8 encoding. I am bulgarian i want to read and write bulgarian text but i cant. $fh = fopen("pics/names.txt", 'r'); $theData = fread($fh, 5); fclose($fh); echo $theData; This is what i use. Iam with windows 7. Pls help me... Hi every one I want to encode my website and I've just found the attached file . What is this encoder ? is there anyway to encode that ? Thanks |