Store pdf remote file in postgres DB with php - php

I have little pdf (or txt, odt, doc) file to be store in Postgres DB with php.
I use this php functions to read file:
$fp = fopen($name, 'rb');
$content = fread($fp, filesize($name));
$content = addslashes($content);
fclose($fp);
and then try to store in db:
$sql = "insert into Table(column) values ('$content'::bytea)";
$result = #pg_query($sql);
"column" is bytea type.
When I execute the script with pdf file, I get the follow error:
ERROR: invalid byte sequence for encoding "UTF8": 0xe2e3cf HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
When I execute the script with doc file, I get the follow error:
ERROR: invalid input syntax for type bytea
When I execute the script with txt file, NO error:
What's wrong and what is the proper way to store files?

Have you tried using pg_escape_string instead of addslashes? http://www.php.net/manual/en/function.pg-escape-string.php

Related

Encoding issue with PHP while writing in a .csv file

I'm working with a php array which contains some values parsed from a previous scraping process (using Simple HTML DOM Parser). I can normally print / echo the values of this array, which contains special chars é,à,è, etc. BUT, the problem is the following :
When I'm using fwrite to save values in a .csv file, some characters are not successfully saved. For example, Székesfehérvár is well displayed on my php view in HTML, but saved as Székesfehérvár in the .csv file which I generate with the php script above.
I've already set-up several things in the php script :
The page I'm scraping seems to be utf-8 encoded
My PHP script is also declared as utf-8 in the header
I've tried a lot of iconv and mb_encode methods in different places in the code
NOTE that when I'm make a JS console.log of my php array, using json_encode, the characters are also broken, maybe linked to the original encoding of the page I'm scraping?
Here's a part of the script, it is the part who is writing values in a .csv file
<?php
$data = array(
array("item1", "item2"),
array("item1", "item2"),
array("item1", "item2"),
array("item1", "item2")
// ...
);
//filename
$filename = 'myFileName.csv';
foreach($data as $line) {
$string_txt = ""; //declares the content of the .csv as a string
foreach($line as $item) {
//writes a new line of the .csv
$line_txt = "";
//each line of the .csv equals to the values of the php subarray, tab separated
$line_txt .= $item . "\t";
}
//PHP endline constant, indicates the next line of the .csv
$line_txt .= PHP_EOL;
//add the line to the string which is the global content of the .csv
$line_txt .= $string_txt;
}
//writing the string in a .csv file
$file = fopen($filename, 'w+');
fwrite($file, $string_txt);
fclose($file);
I am currently stuck because I can't save values with accentuated characters correctly.
Put this line in your code
header('Content-Type: text/html; charset=UTF-8');
Hope this helps you!
Try it
$file = fopen('myFileName.csv','w');
$data= array_map("utf8_decode", $data);
fputcsv($file,$data);
Excel has problems displaying utf8 encoded csv files. I saw this before. But you can try utf8 BOM. I tried it and works for me. This is simply adding these bytes at the start of your utf8 string:
$line_txt .= chr(239) . chr(187) . chr(191) . $item . "\t";
For more info:
Encoding a string as UTF-8 with BOM in PHP
Alternatively, you can use the file import feature in Excel and make sure the file origin says 65001 : Unicode(UTF8). It should display your text properly and you will need to save it as an Excel file to preserve the format.
The solution (provided by #misorude) :
When scraping HTML contents from webpages, there is a difference between what's displayed in your debug and what's really scraped in the script. I had to use html_entity_decode to let PHP interpret the true value of the HTML code I've scraped, and not the browser's interpretation.
To validate a good retriving of values before store them somewhere, you could try a console.log in JS to see if values are correctly drived :
PHP
//decoding numeric HTML entities who represents "Sóstói Stadion"
$b = html_entity_decode("Sóstói Stadion");
Javascript (to test):
<script>
var b = <?php echo json_encode($b) ;?>;
//print "Sóstói Stadion" correctly
console.log(b);
</script>

Not getting correct output while reading data from a file in php row wise

I want to read data from a .txt file using php. I want to show data in one column but I am unable.
My file.txt contain this data:
ہو
تو
کر
جو
نہ
And I'm getting output like this:
ہو 㰊牢>تو 㰊牢>کر 㰊牢>جو 㰊牢>نہ 㰊牢>؟ 㰊牢>ہیں 㰊牢>ان 㰊牢夾畯栠癡⁥湡攠牲
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("Project",$conn);
mysql_query("SET NAMES UTF8");
$handle = #fopen("file.txt", "r");
while (!feof($handle)) {
$buffer = fgets($handle, 1024);
list($urdu)=explode('/n',$buffer);
echo $urdu."<br>";
$sql = "INSERT INTO fb (urdu) VALUES('$urdu')";
mysql_query($sql) or die(mysql_error());
}
So please tell me how can display text like this:
ہو
تو
کر
جو
نہ
Problem seems to be from an encoding mismatch, so first figure out where this mismatch is occurring, your problem could be:
a) your php code could be reading input using an incorrect encoding (if you are trying to read in iso-8859, but the source file is encoded some other way)
b) your php code could be writing output using an incorrect encoding
c) whatever you are using to read the output (your browser) could be set to a different encoding than the bytes you are writing.
So once you figure out which of the 3 places is causing your problem, you can figure out how to fix it by understanding what your source encoding is, and how to read/write using that source encoding instead of another encoding (which your system has probably set as the default).
Maybe you can use mb_detect_encoding and mb-convert-encoding.

How to save BLOB to SQL-Server using PHP

I'm able to upload an image file and using PHP's file_get_contents put the binary into memory. From there I've attempted to use a basic update or insert query statement to then put that data into a blob. It keeps throwing errors.
In addition I've tried to use the addslashes PHP function as well, and it still doesn't work.
I've tried:
if (is_uploaded_file($_FILES['myFile']['tmp_name'])){
$fileData = file_get_contents($_FILES['myFile']['tmp_name']);
$fileData = unpack("H*hex",$fileData);
$content = "0x" . $fileData['hex'];
}
Then I try to insert into my database and it doesn't work either.
The datatype for my BLOB field is image.
The error I am most often getting is:
mssql_query(): message: Operand type clash: text is incompatible with image (severity 16)
Any help would be appreciated!

PHP fputcsv with UTF-8 Problem

I'm trying to allow my clients view some of the MySQL data in Excel. I have used PHP's fputcsv() function, like:
public function generate() {
setlocale(LC_ALL, 'ko_KR.UTF8');
$this->filename = date("YmdHis");
$create = $this->directory."Report".$this->filename.".csv";
$f = fopen("$create","w") or die("can't open file");
fwrite($f, "\xEF\xBB\xBF");
$i = 1;
$length = count($this->inputarray[0]);
fwrite($f, $this->headers."\n");
// print column titles
foreach($this->inputarray[0] as $key=>$value) {
$delimiter = ($i == $length) ? "\n\n" : ",";
fwrite($f, $key.$delimiter);
$i++;
}
// print actual rows
foreach($this->inputarray as $row) {
fputcsv($f, $row);
}
fclose($f);
}
My clients are Korean, and a good chunk of the MySQL database contains values in utf8_unicode_ci. By using the above function, I successfully generated a CSV file with correctly encoded data that opens fine in Excel on my machine (Win7 in English), but when I opened the file in Excel on the client computer (Win7 in Korean), the characters were broken again. I tried taking the header (\xEF\xBB\xBF) out, and commenting out the setlocale, to no avail.
Can you help me figure this out?
If, as you say, your CSV file has "correctly encoded data" - i.e. that it contains a valid UTF-8 byte stream, and assuming that the byte stream of the file on your client's site is the same (e.g. has not been corrupted in transit by a file transfer problem) then it sounds like the issue Excel on the client's machine not correctly interpreting the UTF-8. This might be because it's not supported or that some option needs to be selected when importing to indicate the encoding. As such, you might try producing your file in a different encoding (using mb_convert_encoding or iconv).
If you get your client to export a CSV containing Korean characters then you'll be able to take a look at that file and determine the encoding that is being produced. You should then try using that encoding.
Try encoding the data as UTF-16LE, and ensure that the file has the appropriate BOM.
Alternatively, send your clients an Excel file rather than a CSV, then the encoding shouldn't be a problem
Try wrapping the text in each fwrite call with utf8_encode.
Then use what is suggested here: http://www.php.net/manual/en/function.fwrite.php#69566

Function returns CSV File: How to go about checking it?

I am doing something like:
$outputFile = getCurrentDBSnapshot($data);
where $data is the resource stream that am passing in, basically from command prompt am passing an file and am opening it using fopen for writing with 'w+' permissions, now getCurrentDBSnapshot would get the current state of a table and would update the $data csv file, so basically $outputFile would be updated with the current state of database table, now I want to var_dump or print the value of $outputFile to see the data present into it.
But when I do
$this->fout = fopen($outputFile,'r') or die('Cannot open file');
$test = fgetcsv($outputFile,5000,";");
var_dump($test);
It gives me an error saying that it expects parameter 1 to be a string type and am passing resource.
My goal to see the contains of $outputFile
and so my question is that
How can I see the contains present in $outputFile or how can I see what getcurrentDBSnapshot function is returning me ?
fegtcsv takes as first parameter a file handle, not a filename. You'll need to do something like:
$this->fout = fopen($outputFile,'r') or die('Cannot open file');
while ($test = fgetcsv($this->fout,5000,";"))
{
var_dump($test);
}
Note that fgetcsv merely gets a single line of the file, analogously to fgets.
Also I'm not sure why you're passing a semicolon as the third argument to fgetcsv. CSV stands for comma-separated-value; are you sure your file is semicolon-delimited?
Quoting the fgetcsv page of the manual, the first parameter passed to fgetcsv should be :
A valid file pointer to a file
successfully opened by fopen(),
popen(), or fsockopen().
Here, you are passing as first parameter $outputFile, which contains the name of the file you are trying to read from -- i.e. a string, and not a handle to an opened file.
Considering you are calling fopen and storing its return-value in $this->fout, this is probably the variable you should be passing to fgetcsv, like this :
$this->fout = fopen($outputFile,'r') or die('Cannot open file');
$test = fgetcsv($this->fout,5000,";");
var_dump($test);
As a sidenote : fgetcsv will only return the data of one line each time you call it -- which means you might have to use a loop, if you want to see the content of the whole file.
If needed, take a look at Example #1, on the manual page of fgetcsv.

Categories