I'm receiving a text file upload and then just printing it back out. For some reason, special characters are appearing as black boxes with a white check mark. I tried htmlentities() and utf8_encode() on the content to be print to screen, but that didn't help.
Here's all of my code:
<?php ini_set("auto_detect_line_endings", true);
header('Content-Type: text/html; charset=utf-8');
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body style="overflow:visible;">
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="upload" value="upload">Upload</button>
</form>
<pre>
<?php
if($_POST['upload']) {
//$fileName = 'old.txt';
$fileName = $_FILES['file']['tmp_name'];
if(file_exists($fileName)) {
$file = fopen($fileName,'r');
while(!feof($file)) {
$name = fgets($file);
echo(htmlentities($name));
}
fclose($file);
}
}
?>
</pre>
</body>
</html>
This code works on my localhost LAMP server, but the character probelm appears on some other people's servers. What can I do to maek the special characters show up?
Related
I'm trying to get text from a pdf file with Hebrew in it and manipulate it, but when I'm using echo it shows these letters instead of Hebrew:
Ço̬mÀÃ6ÜÍzWÃýCW¶°ÐÞ]Aµ±¸¤:ÄÞ[JÞaCå+wÎ[n6GZù>"âÊù+ýÕ9^6ÓF½íoßEcì¸_pùnÚbïjÅÅß^UtýÝ-®»þgåĿٻƷ8ԯβzÅr
I made sure the page is in utf-8 and converted the returned text to utf-8 but it doesn't fix it.
When The text wasn't in utf-8 it showed these symbols:
��G�W����/��<� ������%�M����>����z.�m47�M �O�4�Nf�/7ʓ쓻#2FGj��,U8�J
I feel like I'm just missing something.
This is my code:
<?php
header('Content-type: text/html; charset=UTF-8');
$formReturn = $_POST["formReturn"];
if ($formReturn)
{
$file = $_FILES["gradesPdf"]["tmp_name"];
$text = file_get_contents($file);
$text = utf8_encode($text);
}
$html = '
<!DOCTYPE html>
<html lang="he">
<meta charset="utf-8" />
<head>
<title>נסיון</title>
</head>
<body>
<form enctype="multipart/form-data" method="post">
<input type="file" name="gradesPdf" id="gradesPdf">
<br><br>
<button type="submit">run</button>
<input type="hidden" name="formReturn" value="1">
</form>
'. $text .'
</body>
</html>
';
echo $html;
Btw I can't use pdfParser, I tried the demo on their site and it didn't return the text the way I wanted. I think since my pdf has a table in it.
I want to create a php form page. Which when submitted then it automatically create a html static page with filled form data in local directory of the web server. tell me please how can i do that.
Since it us tough to write html , meta tag etc every time in textarea ....
So creating a template file and then using it as base , and finally editing only the body and title tag ( you can do more ) , it makes life much easier ...
First we need to create file.php which harnesses all the logic of creating a new file and injecting all the code ( using php methods fopen which creates a new file and file_put_contents which puts all the data inside it ).
Secondly , we create a template file from which all the boilerplate is used up.
(Containing some variable like {BODY} ).
finally we use file.php by filling form and get all required inputs ( getting all inputs and using loop to change all the template variables with inputs entered ).
file.php
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
if (isset($_POST['filename'])) {
$body = $_POST['body'];
$title = $_POST['title'];
$swap_var = array(
"{BODY}" => $body,
"{TITLE}" => $title
);
$template = "template.php";
if (file_exists($template)) {
$html = file_get_contents($template);
} else {
die ("Unable to locate your template file");
}
foreach (array_keys($swap_var) as $key) {
if (strlen($key) > 2 && trim($swap_var[$key]) != '')
$html = str_replace($key, $swap_var[$key], $html);
}
$filename = $_POST['filename'];
if (!file_exists($filename)) {
fopen($filename, "w");
}
file_put_contents($filename , $html);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width">
<title>Creating and adding text in File</title>
<style type="text/css" media="all">
input , textarea {
width: 100% ;
}
body{
padding: 10%;
}
</style>
</head>
<body>
<form action="file.php" method="post" accept-charset="utf-8">
<input type="text" name="filename" id="filename" placeholder="Enter Filename (with extension like .html)" />
<br>
<br>
<input type="text" name="title" id="title" placeholder="title" />
<br>
<br>
<textarea name="body" id="body" rows="8" placeholder="enter body"></textarea>
<br>
<br>
<button type="submit">Create New html file</button>
</form>
</body>
</html>
template.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width , initial-scale=1.0">
<title>{TITLE}</title>
</head>
<body>
{BODY}
</body>
</html>
Make sure to create these 2 files in same directory and note this code will generate all the new files in the same directory as these 2 files , remember you can always change the location of generation of new files .
You can use php file functions like example.
$myfile = fopen("WHATEVER_THE_FILENAME_YOU_WANT.html", "w") or die("Unable to open file!");
$data = """
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
ECHO YOUR FORM DATA HERE.
</body>
</html>
""";
fwrite($myfile, $data);
fclose($myfile);
//im using this code to import data from a csv exel file to mysql database
code works in windows wamp but not in linux apache2
i dont understand why this is happening the logic is ok since it runs on windows
<?php
include("dbcon.php");
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("insert into client (nome,cognome, shenime) values
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
//
//redirect
header('Location: import.php?success=1'); die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import leads php csv</title>
</head>
<body>
<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
try this, when you save your file.csv in linux in field options, field delimiter, chose point and coma (;) and save.
i have two simple page:
(first.php)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<form action="second.php" method="post" accept-charset="utf-8">
<input type="text" id="search" name="search" placeholder="Search..."/>
<input type="submit" name="submit" value=">>" id="submit" />
</html>
(second.php)
<?php
header ('Content-Type: text/html; charset=UTF-8');
$url=$_GET['search'];
$url= urlencode($_GET['search']);
echo $url
?>
output in second.php:
%C8%E1%ED%C8%E1%C8
the question: how can i return same arabic character in second.php
thank you 4 you'r Patience
I modified the second.php for you ,I test it and it's work fine with arabic words
You don't need to use urlencode function again.
<?php
header ('Content-Type: text/html; charset=UTF-8');
$url=$_GET['search'];
$url= $_GET['search'];
echo $url
?>
When I click the button the text from the textbox should be written to a .txt file, which then should be downloaded. Somehow the isset function doesn't work, I already tried to link the php file with a <form>, but then I can't read the textbox text.
Here's my code:
<?PHP
if(isset($_POST['submit']))
{
$text = $_POST['text'];
print ($text);
$filename = 'test.txt';
$string = $text;
$fp = fopen($filename, "w");
fwrite($fp, $string);
fclose($fp);
header('Content-disposition: attachment; filename=test.txt');
header('Content-type: application/txt');
readfile('test.txt');
}
?>
<html>
<head>
<title>Text Editor</title>
</head>
<body>
<textarea name="text" rows="20" cols="100"></textarea>
<p>
<button type="submit" value="submit">Download Text</button>
</body>
</html>
Firstly, you need to have a <form> tag to be able to submit your data:
<body>
<form action="add your php filename here" method="post">
...
</form>
</body>
You might also need to make your <button type="submit" into <input type="submit"
Add a form tag and post back to same page if php document is within the same file.
<?php
if(isset($_POST['submit']))
{
$text = $_POST['text'];
print ($text);
$filename = 'test.txt';
$string = $text;
$fp = fopen($filename, "w");
fwrite($fp, $string);
fclose($fp);
header('Content-disposition: attachment; filename=test.txt');
header('Content-type: application/txt');
readfile('test.txt');
}?>
<html>
<head>
<title>Text Editor</title>
</head>
<body>
<form method="post">
<textarea name="text" rows="20" cols="100"></textarea><p>
<input type="submit" value="submit">Download Text</button>
</form>
</body>
</html>
In your current HTML, you need a <form> tag.
Try reading about this here.
You haven't added the form TaG in HTML.