PHP - Import Text Files In To Sql - Character Conversion Needed?
I'm importing some text files from my windows servers in to a SQL database, but I'm running in to what I think is some sort of issue with either a special character or something to do with UTF-8, or UTF-16... I haven't dealt with this before, so I'm really not even sure.
I read in the file as such: $handler = fopen($file, "r"); $Data = fread($handler, filesize($file)); fclose($handler); The text file itself contains the data formatted like this: Quote Unable to deliver this message because the follow error was encountered: "This message is a delivery status notification that cannot be delivered.". The specific error code was 0xC00402C7. The message sender was <>. The message was intended for the following recipients. OnlineHelp@somedomain.com If I simply echo out this data: echo $Data It'll come out in Firefox like the following. And I can see FF is choosing to view it as Western (ISO-8859-1), but if I choose to use Unicode (UTF-16) then the data displays correctly. Quote U�n�a�b�l�e� �t�o� �d�e�l�i�v�e�r� �t�h�i�s� �m�e�s�s�a�g�e� �b�e�c�a�u�s�e� �t�h�e� �f�o�l�l�o�w� �e�r�r�o�r� �w�a�s� �e�n�c�o�u�n�t�e�r�e�d�:� �"�T�h�i�s� �m�e�s�s�a�g�e� �i�s� �a� �d�e�l�i�v�e�r�y� �s�t�a�t�u�s� �n�o�t�i�f�i�c�a�t�i�o�n� �t�h�a�t� �c�a�n�n�o�t� �b�e� �d�e�l�i�v�e�r�e�d�.�"�.� � � � �T�h�e� �s�p�e�c�i�f�i�c� �e�r�r�o�r� �c�o�d�e� �w�a�s� �0�x�C�0�0�4�0�2�C�7�.� � � � � � �T�h�e� �m�e�s�s�a�g�e� �s�e�n�d�e�r� �w�a�s� �<�>�.� � � � � � �T�h�e� �m�e�s�s�a�g�e� �w�a�s� �i�n�t�e�n�d�e�d� �f�o�r� �t�h�e� �f�o�l�l�o�w�i�n�g� �r�e�c�i�p�i�e�n�t�s�.� � � �O�n�l�i�n�e�H�e�l�p�@�E�l�i�t�e�R�a�c�i�n�g�.�c�o�m� � � And finally, if I try to insert the data in to SQL, the data looks like this: Quote INSERT INTO Badmail VALUES( 2 , '00360053425643112201000000004.BDR' , 'U n a b l e t o d e l i v e r t h i s m e s s a g e b e c a u s e t h e f o l l o w e r r o r w a s e n c o u n t e r e d : " T h i s m e s s a g e i s a d e l i v e r y s t a t u s n o t i f i c a t i o n t h a t c a n n o t b e d e l i v e r e d . " . T h e s p e c i f i c e r r o r c o d e w a s 0 x C 0 0 4 0 2 C 7 . T h e m e s s a g e s e n d e r w a s < > . T h e m e s s a g e w a s i n t e n d e d f o r t h e f o l l o w i n g r e c i p i e n t s . O n l i n e H e l p @ S o m e d o m a i n . c o m ' , '2010-11-30 07:17:05' , GETDATE()) So basically, I'm not really sure if I'm supposed to convert the ASCII to UTF-8 or if I just need to do a bunch of str_replace to correct the data before inserting. I'd appreciate any feedback or suggestions anyone has. Thank you. Similar TutorialsThis topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=342991.0 Hi! I hope somebody can help me what im do wrong. i have checked that the data from the file is in $source_file but nothing imports to the database Code: [Select] <?php include('config.php'); include('opendb.php'); if(isset($_POST['upload'])) { $source_file = @$_POST['userfile']; //$source_file = fopen('http://localhost/test/upload/test.csv', 'r'); $target_table = 'foretag'; function csv_file_to_mysql_table($source_file, $target_table, $max_line_length=10000) { if (($handle = fopen("$source_file", "r")) !== FALSE) { $columns = fgetcsv($handle, $max_line_length, ","); foreach ($columns as $column) { $column = str_replace(".","",$column); } $insert_query_prefix = "INSERT INTO $target_table (".join(",",$columns).")\nVALUES"; while (($data = fgetcsv($handle, $max_line_length, ";")) !== FALSE) { while (count($data)<count($columns)) array_push($data, NULL); $query = "$insert_query_prefix (".join(",",quote_all_array($data)).");"; mysql_query($query); } fclose($handle); } } function quote_all_array($values) { foreach ($values as $key=>$value) if (is_array($value)) $values[$key] = quote_all_array($value); else $values[$key] = quote_all($value); return $values; } function quote_all($value) { if (is_null($value)) return "NULL"; $value = "'" . mysql_real_escape_string($value) . "'"; return $value; } } include('closedb.php'); echo "<br>done<br>"; ?> First and foremost I am PHP newbie so excuse the dumb question but I have been struggling with this one for hours and can not find the solution. Can someone show me how I would rewite the following wordpress code for meta tags so that it automatically strips out the character ' from the "single" page titles? Example I want this title: "My Cat's Black" to be "My Cats Black" This is the line I am trying to edit..... Code: [Select] elseif ( is_single() ) { wp_title(''); print ' | '; bloginfo('name'); } here is the full piece of code... Code: [Select] <title><?php if ( is_home() ) { bloginfo('name'); print ' | '; bloginfo('description'); } elseif ( is_search() ) { bloginfo('name'); print ' | '; _e('Search Results', 'woothemes'); } elseif ( is_author() ) { bloginfo('name'); print ' | '; _e('Author Archives', 'woothemes'); } elseif ( is_single() ) { wp_title(''); print ' | '; bloginfo('name'); } elseif ( is_page() ) { bloginfo('name'); print ' | '; bloginfo('description'); } elseif ( is_category() ) { single_cat_title(); print ' | '; bloginfo('name'); } elseif ( is_month() ) { _e('Archive', 'woothemes'); print ' | '; the_time('F'); bloginfo('name'); } elseif (function_exists('is_tag')) { if ( is_tag() ) { bloginfo('name'); print ' | '; _e('Tag Archive', 'woothemes'); print ' | '; single_tag_title("", true); } } ?></title> Any help would be greatly appreciated!! Hi i have this edit form that allows user to mofy data but the problems on the text box is that it deletes the rest of the data after the space from the first word i tried to increase the size of the varChars on mysql but did no work why it happens how can i stop from happening?? this the form input <input type="text" name="name" id="name" class='text_box' value="<?php echo $_GET['name'];?>"/> hello dear folks good evening dear PHP-Freaks,
well i am not sure if this question fits here - in miscellaneous.
but i am pretty sure that many many calc and excel-experts are here in this great forum.
what is aimed:
want to create a calendar - with 365-chunks that are imported into a caleandar form see some ideas where i can import this i posted the example-calc spreassheet that i want to import. note it has got 365 lines - for 365 days of the next year 2015 what needs to be done ; whats needs to be achieved: i need to impoort the example into the calendar form. see some of the examples -.. if i am able to import the texts (of the collumn ) in a writer or word document each line of the calc needs to get on a sheed of the word or writer. i need to know how to arrange this export of calc into the writer document. if i need to explain it more thoroughiy - just let me know! greetings you matze see some examples of calendar templates Blank daily calendar - Templates Daily lesson planner (color, landscape) - Templates note: all i need to know is to be able to export from calc to any word or calc document... see the attached sample below... Attached Files 2015_sample_version_.ods.zip 18.07KB 0 downloads ARRAY Example scaled down to two (renamed example only) fields $data = array( "field1" => "Text within quotes processes OK", "field2" => "Text within quotes processes OK" );
The frustating thing has been trying to figure out how to $text1 = $db_field['sampletext1']; $text2 = $db_field['sampletext2']; I have tried: $data = array( "field1" => $text1, "field2" => $text2 ); *** (AND) *** $data = array( "field1 => '$text1', "field2 => '$text2' );
But neither approach works, even though the text in both $data = array( "field1 => "text1", "field2 => "text2" ); I also tried using strval but that did not work.
So how on earth to get the (actual) text from $text1 and
It is probably something simple, so maybe I don't know Thanks for any enlightenent. -FreakingOUT
Edited January 28, 2019 by FreakingOUT This script is supposed to run in the command line. Like the title says, I need to: Import some columns (input the names with an argument) from a .CSV file Split the column into blocks of 500 lines Translate this pieces Put them back into the original file I already have some code but it is unfinished and not working: <?php //name of the .CSV file $filename = $argv[1]; //name of the column to be translated $header_name = $argv[2]; //open file $file_handle = fopen($filename, "r"); $i=0; $j=0; $counter = 0; $translated = ''; $string = ''; while (!feof($file_handle) ) { $counter++; $line_of_text = fgetcsv($file_handle, 1024); //find the column id that contains the header name. // headers are always on the the first line if ( $counter == 1 ){ while ($line_of_text[$i] != $header_name) $i++; $header_id = $i; } //create the blocks $string .= $line_of_text[$i]; if ($counter % 500 == 0) { $gt = new GoogleTranslateWrapper(); $translated .= $gt->translate($string, "en"); } } //Write the translate column in the same .CSV file fclose($file_handle); ?> Any help in rewriting this to work? If I was pulling back data from MySQL that was like this "monkeys / dogs / cats", how would I grab just "monkeys" from that using PHP? In other words, I want to grab all text until a "/" is seen. Hi guys, I have script which scraps content from web and writes it down in a text file, now everything seems to be okay. Its scraps and writes well but the problem is when i open that text file one weird character keeps coming after some words which i don't know about. I have attached the text file, will you guys please look at it and tell me how to replace that square looking character into new line in text file. I have a PHP Form where users have to enter and upload a file but when they click upload everything is working except that it is only sending first character of text field to Database! Can any one help?? Thanks File upload.php form Code: [Select] <form action="add_file.php" method="post" enctype="multipart/form-data"> <p><br> JobID: <input name="JobID" type="text" value="<?php echo $row_Recordset1['JobID']; ?>" readonly="readonly" /> </p> <p>Company ID: <input name="CompanyID" type="text" value="<?php echo $row_Recordset1['CompanyID']; ?>" readonly="readonly" /> </p> <p>UserID: <input name="UsersID" type="text" /> </p> <p>Select File to upload: <input type="file" name="uploaded_file" /> </p> <p>Make Sure All Details Are Correct before Submitting!</p> <p> <input type="submit" value="Submit Application" /> </p> <form> add_file.php // Check if a file has been uploaded if(isset($_FILES['uploaded_file'])) { // Make sure the file was sent without errors if($_FILES['uploaded_file']['error'] == 0) { // Connect to the database $dbLink = new mysqli('localhost', 'user', 'password', 'phpsite'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Gather all required data $JobID = $dbLink->real_escape_string($_POST['JobID']['JobID']); $CompanyID = $dbLink->real_escape_string($_POST['CompanyID']['CompanyID']); $UsersID = $dbLink->real_escape_string($_POST['UsersID']['UsersID']); $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']); $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']); $data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name'])); $size = intval($_FILES['uploaded_file']['size']); // Create the SQL query $query = " INSERT INTO `application` ( `JobID`, `CompanyID`, `UsersID`, `name`, `mime`, `size`, `data`, `N_DateAndTime` ) VALUES ( '{$JobID}', '{$CompanyID}', '{$UsersID}', '{$name}', '{$mime}', {$size}, '{$data}', NOW() )"; // Execute the query $result = $dbLink->query($query); // Check if it was successfull if($result) { echo 'Success! Your job application was successfully sent!'; } else { echo 'Error! Failed to insert the file, please try again' . "<pre>{$dbLink->error}</pre>"; } } else { echo 'An error accured while the file was being uploaded, please try again. ' . 'Error code: '. intval($_FILES['uploaded_file']['error']); } // Close the mysql connection $dbLink->close(); } else { echo 'Error! A file was not sent! Please try again!'; } // Echo a link back to the main page echo '<p>Click <a href="year1index.php">here</a> to go back</p>'; ?> MOD EDIT: [code] . . . [/code] BBCode tags added. Ok i have been working on this for a day+ now. here is my delema simple .ini text file. when a user makes a change (via html form) it makes the correct adjustments. problem is the newline issue 1. if i put a "\n" at the end (when using fputs) works great, except everytime they edit the file it keeps adding a new line (i.e. 10 edits there are now 10 blank lines!!!!) 2. if i leave off the "\n" it appends the next "fgets" to that lilne making a mess Code: [Select] ##-- Loop thruoght the ORIGINAL file while( ! feof($old)) { ##-- Get a line of text $aline = fgets($old); ##-- We only need to check for "=" if(strpos($aline,"=") > 0 ) { ##-- Write NEW data to tmp file fputs($tmp,$info[$i]." = ".$rslt[$i]."\n"); $i++; } ##-- No Match else { fputs($tmp,$aline."\n"); }//Checking for match }//while eof(old) what in the world is making this such a big deal. i dont remember having this issue in the past I tried opening with w+, and just w on the temp file a typical text line would be some fieldname = some value the scipt cycles through the file ignoring comments that are "#" ps the tmp file will overwrite the origianl once complete all i really want to know is WHY i cant get the newline to work, and what is the suggested fix EDIT: i just tried PHP_EOL and it still appends another newline I have a folder of .txt files which have an array like this: 0#|#1#|#2#|#3#|#4#|#5#|#6 NOTE: All of the textfiles are md5 hashes. Every text file has an array like this, (numbers representing the data) and i'm trying to make it so i can access every array an combine it. So in the end i want to know how many text files have the number "6" for the 6th array and so on. I'm pretty new at PHP, so please be detailed. Just thinking last night trying to speed some things up, how hard is it to have a text file on the server that can only be edited by PHP? Or is there a way of making a folder unreadable to anyone?, this way I could have all the text files in the non-readable (non-viewable) folder so no user could edit them? James Hey! I have a bunch of text files with one line of text ex. SHS9GW34 I'm trying to write a little php script on my local machine that will take all the files and put all the different codes in the same files in this kind of format: RGDGIDSG453 EOHBDSBN453 ERGD4535355 etc etc Thanks Hey, I'm looking for some PHP code which can read a certain text file i have. I need to get certain information from this text file. This is how the text file looks like: metadataIngester: now executing performAction with token: 9188147351 metadataIngester: result of performAction with token:9188147351 metadataIngester: runtime (ms) for token 9188147351: 9953 inferencer: now executing performAction with token:9188147351 inferencer: result of performAction with token:9188147351 inferencer: runtime (ms) for token 9188147351: 10903 transcoder: now executing performAction with token:9188147351 transcoder: result of performAction with token:9188147351 transcoder: runtime (ms) for token 9188147351: 65456 transcoder: now executing performAction with token:9188147351 transcoder: result of performAction with token:9188147351 transcoder: runtime (ms) for token 9188147351: 83128 streamingServerTransfer: now executing performAction with token:9188147351 streamingServerTransfer: result of performAction with token:9188147351 streamingServerTransfer: runtime (ms) for token 9188147351: 12187 middlewa now executing performAction with token:9188147351 middlewa result of performAction with token:9188147351 middlewa runtime (ms) for token 9188147351: 28148 Now, there are more parts like this with an other tokenID & runtimes. I need to get the runtime for each token (in this example it will be 9953 + 10903 + 65456 + 83128 + 12187 + 28148 = 209775 (ms)). I need PHP to display the tokenID + the total time per token. Hi guys, im logging activity on my site to text files using; Code: [Select] date_default_timezone_set('GMT'); $LogFileLocation = "xxxxxxxx.log"; $fh = fopen($_SERVER['DOCUMENT_ROOT'].$LogFileLocation,'at'); fwrite($fh,date('d.M.Y H:i:s')."\t".$_SERVER['REMOTE_ADDR']."\t".$_SERVER['REQUEST_URI']."\n"); fclose($fh); This is write something like; [date] [time] [ip address] [folder accessed] I then post it like; Code: [Select] <? $fn = "xxxxxxxxxx.log"; print htmlspecialchars(implode("",file($fn))); ?> That works fine but what i want to be able to do is read/edit the text files, so the main things i need to add is; The ability to clear the log (using password for confirmation) not sure how to go about this because i don't know how to edit the text file <input name="password" type="text"> <input type="submit" value="Clear Log?"> I also need a script to read the log and alert me an ip address that isn't mine is found, maybe something like if (an ip that isn't mine == yes) echo "an unknown ip accessed something" Also lets say i have a script that checks if a name exists, how can i use a text file containing a list of names to be read by a script if (name exists in text file) echo "name exists" else echo "name doesn't exist" All these questions are similar in the fact i need to understand how to open and read text files for specific entries, hope you understand, thanks for any help. I have some fairly small text files (2K) which are parsed where certain deliminated fields are replaced with values provided in an associated array. There will typically be less than 5 replaced fields, and I plan on using preg_replace_callback to find and replace them.
I am contemplating caching the files after being parsed (note that they will only be accessed by PHP, and not directly by Apache).
Do you think doing so will provide any significant performance improvement?
If I do go this route, I would like thoughts on how to proceed. I am thinking something like the following:
Append the filename along with the serialized value of the array, and hash it using md5().
Store the parsed file using name "file_".$hash
Get the modification time of the newly created file using filemtime(), and store the value in a new file called "time_".$hash.
bla bla bla
When the next request comes in to parse a file, create the hash again.
If the file exists for the given hash name, and the time file matches filemtime(), use that file, else parse the original file.
Is this a good approach?
Hey guys, im new hear so im not shore how things work. Iv got a project where we are ment to alow users to record a bug fault in a computer, currently i have a page that looks like ths: <?php if (empty($_POST['bug_ID'])) echo "<p>You must enter a BUG ID number</p>"; else { $bugID = addslashes($_POST['bug_ID']); $name = addslashes($_POST['bug_name']); umask(0007); if (!file_exists("../../data/lab05")) mkdir("../../data/lab05", 02777); $lab05 = fopen("../../data/lab05/". "$bugID.txt", "a"); if (is_writable("../../data/lab05/NewBug.txt")) { if (fwrite($lab05, $bugID . ", " . $name . "\n")) echo "<p>Thank you entering a new bug</p>"; else echo "<p>Cannot add bug</p>"; } else echo "<p>Cannot write to the file.</p>"; fclose($lab05); } ?> this code runs fine however I also need to write a sperate page that allows for users to search and up date bugs recorded, I want them to search using the bug_ID field and we are ment to use the fgets method but am unshore, anyhelp would be much apricated. thank you hello. i have a navigation which i downloaded from Code: [Select] http://www.dynamicdrive.com/dynamicindex17/ddaccordionmenu-bullet.htm and i would link to pull the information in from my db this is the html Code: [Select] <div class="arrowlistmenu"> <h3 class="menuheader expandable">title</h3> <ul class="categoryitems"> <li><a href="#">link</a></li> </ul> <h3 class="menuheader expandable">title</h3> <ul class="categoryitems"> <li><a href="#">link</a></li> <li><a href="#" class="subexpandable">nested title</a> <ul class="subcategoryitems" style="margin-left: 15px"> <li><a href="#">title</a></li> </ul> </li> <li><a href="#">title</a></li> </ul> <div> </div> in the database each link and title have there own parent id and level id so for example: $level $parent_id $title $link $url This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=318486.0 |