Hello I have Problem While I Listing Files in folder named upload, When There's arabic file It show ��� ����� ������
$target = "upload";
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($target));
while($it->valid()) {
if (!$it->isDot()) {
$nom=$nom+1;
echo $it->getSubPathName();
echo '<tr align="center"><td> Play </td><td>' . $it->getSubPathName() . '</td><td>' . $nom . '</td></tr>'; } }
First, convert the text to UTF-8:
iconv('CP1256', 'UTF-8', $it->getSubPathName());
Then, ensure that the web browser properly decodes the page as UTF-8. Put this as the top of your PHP file:
<?php
header("Content-Type: text/html; charset=UTF-8");
// The rest of the code
Related
First, I have a few knowledge in PHP, and that is all I could do with my excel export (see attached code below)
It works, but I want to export that excel file with 2 more sheets, I've googled a lot but could not customize the code to insert in it, please, help me if you can
Thank you
<?php
$filename = time().".csv";
header ( 'HTTP/1.1 200 OK' );
header ( 'Date: ' . date ( 'D M j G:i:s T Y' ) );
header ( 'Last-Modified: ' . date ( 'D M j G:i:s T Y' ) );
header ( 'Content-Type: application/vnd.ms-excel') ;
header ( 'Content-Disposition: attachment;filename='.$filename);
ExportFile1($main_exce);
function ExportFile1($records) {
$do = false;
if(!empty($records)){
foreach($records as $down){
$heading = true;
foreach($down as $row) {
if($heading) {
// display field/column names as a first row
$heading = false;
if(!$do){
$d = implode("\t", array_values($row)) . "\n";
print chr(255) . chr(254) . mb_convert_encoding($d, 'UTF-16LE', 'UTF-8');
$do = true;
}
}else{
$d = implode("\t", array_values($row)) . "\n";
print chr(255) . chr(254) . mb_convert_encoding($d, 'UTF-16LE', 'UTF-8');
}
}
// $d = "\t\n\t\n\t\n";
// print chr(255) . chr(254) . mb_convert_encoding($d, 'UTF-16LE', 'UTF-8');
}
}
}
exit;
?>
The reason you can't make this work is that you are not creating an Excel export here.
You're creating a plain-text, tab-delimited file. Excel can import files in that format, but it's not a true Excel file. It's a simple flat file, and as such, it doesn't support multiple worksheets, unlike a real Excel file.
If you want to solve this, you need to rewrite this code so that it outputs a file which is in the real Excel file format - it's considerably more complex than a tab-delimited text format. There are various libraries available for PHP which can help you easily create real Excel files in xlsx format. Recommendations are off-topic here but they are not hard to find using a search engine.
I am trying to read the PDF files in each child job and pull out the number of pages in each. My code works perfectly for the first file, but not for the second. My thinking is that the file is not being overwritten by the second PDF that's pulled in.
I am downloading the file to read it, so there's no need to keep it afterwards.
I tried putting in a check to unlink the file after use, but I get a permission denied error.
$CI = &get_instance();
$CI->load->library('Awss3', null, 'S3');
$PdfTranscriptInfo = $CI->MJob->getDOCCSPdfTranscript($copy['jobid']);
$filename = $PdfTranscriptInfo['origfilename'];
$PdfFilename = 'uploads/' . $copy['jobid'] . '/' . $filename;
$localfilename = FCPATH . 'tmp/local.pdf';
$fileData = $CI->S3->readfile($PdfFilename, false, 'bucket');
require_once 'application/libraries/fpdi/fpdf.php';
require_once 'application/libraries/fpdi/fpdi.php';
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile($localfilename);
if ($pageCount == $copy['pagecount']) {
echo '<td>' . $copy['pagecount'] . '</td>';
} else {
echo '<td style="background-color:red;">' . $copy['pagecount'] . '/' . $pageCount . '</td>';
}
if (file_exists($localfilename)) {
unlink(FCPATH . 'tmp/local.pdf');
}
I am not getting any error messages, but the page count for the second file ($pageCount) is the same as the first file, so it's obviously not picking up the second PDF.
Thanks in advance for any help.
EDIT: FCPATH is C:\wamp64\www\companyname\
I have PHP saving an output file all from one very long string. I want this code that is being outputted to be formatted properly, so I am trying to add linebreaks in at certain points.. I have been trying to use "\n", but for some reason that is not doing the trick.. Here's the relevant code:
foreach($headerColumn as $hColumn) {
$outputString .= '<td>' . $hColumn . '</td>' . "\n";
};
Here I have the "\n" added to the end of the string, but for some reason it is being outputted like this:
<thead>
<tr><td>Name</td><td>Ticker</td><td>Buy Date</td><td>Current Yield</td>
</tr>
</thead>
When, in actuallity, I want each on its' own line.. any ideas? in case it has something to do with the content type of the file, here is my file write call:
header('Content-Disposition: attachment; filename="sample.txt"');
header('Content-Type: text/plain'); # Don't use application/force-download - it's not a real MIME type, and the Content-Disposition header is sufficient
header('Content-Length: ' . strlen($outputString));
header('Connection: close');
echo $outputString;
Thanks!
I think what you want to use is PHP_EOL:
PHP_EOL (string)
The correct 'End Of Line' symbol for this platform. Available since PHP 4.3.10 and PHP 5.0.2
So, for your example:
$outputString .= '<td>' . $hColumn . '</td>' . PHP_EOL;
I don't know if this is the best practice... but you can actually put a newline into the code by simply not ending the string quotation until the next line:
foreach($headerColumn as $hColumn) {
$outputString .= '<td>' . $hColumn . '</td>' . "
";
};
I have a form to display which have file attachments at the time of form filling, I succeeded in attaching the file, but at the time of displaying the form it also showing the attached file in some binary/etc form, instead I want it to show just a file name and whenever we click on the file name it has to download the file... Please do help for this
My Code is:
<?php
...
$id = htmlentities($_GET['id'], ENT_QUOTES);
// Get the details of the risk
$risk = get_risk_by_id($id);
$status = htmlentities($risk[0]['status'], ENT_QUOTES);
$subject = $risk[0]['subject'];
//file retrival fields
$filename = $risk[0]['name'];
$mimetype = $risk[0]['type'];
$filedata = $risk[0]['content'];
...
?>
<html>
...
<?php
...
echo "<label>Risk Assessment</label>\n";
echo "<textarea name=\"assessment\" cols=\"50\" rows=\"3\" id=\"assessment\" disabled=\"disabled\">" . htmlentities(stripslashes($assessment), ENT_QUOTES). </textarea>\n";
echo "<label>Additional Notes</label>\n";
echo "<textarea name=\"notes\" cols=\"50\" rows=\"3\" id=\"notes\" disabled=\"disabled\">" . htmlentities(stripslashes($notes), ENT_QUOTES) . "</textarea>\n";
echo "<label>Files attached:\n</label>";
echo $filedata;
...
?>
...
</html>
and the o/p is displaying the file content... :(
you are echoing the $filedata content. you should echoing the $filedata location, not its content.
Here's an example of how to download a pdf file instead of displaying it:
You're Page:
<a href="dl.php?filnam=xyz.pdf>Download the file</a>
dl.php
<?php
$filnam = str_replace(" ", "%20", basename($_GET['filnam']));
header ("content-type: application/pdf");
header ("Content-Disposition: attachment; filename*=UTF-8'en'$filnam");
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($_GET['filnam']));
readfile($_GET['filnam']);
?>
So there are a few steps
Get the name of the file
Get the local path of the file
Respond with a link.
It's been a while since I used PHP but I believe you can get the name of the file with
//basename takes /a/b/c/d.jpg and returns d.jpg
$filename = basename($risk[0]['name']);
$localpath = $risk[0]['tmp_name'];
and then for your output do
echo " . $filename . "" . PHP_EOL
That will create a link with the label of the filename and the url of the local path on the server.
Update: Apparently the file doesn't actually exist yet. If this is the case, then you should write the contents of the file stored in your db. To do this, do
$handle = fopen($filename, "wb");
You can use whatever filename you want. You should add something like the timestamp or user ID or something to make the file unique so it's not overwritten by multiple requests. This will create the file. You can then fill in the file by doing
fwrite($handle, $risk[0]['content']);
finally close the file with
fclose($handle);
to output this new link you can just use the same filename if you like
echo " . $filename . "" . PHP_EOL
If you specified the filename by including some unique value then you can get the original name by using
echo " . $risk[0]['name'] . "" . PHP_EOL
Hope that helped!
I have images inside a folder that contains 'ñ' char like Navideño-4.jpg.
I created a script to print all images but if image has ñ char it just prints Navide%C3%B1o-4.jpg
Here is script:
function show($path){
$files = glob($path.'*');
natcasesort($files);
foreach($files as $file) {
echo ' <br/> <img src="' . $file . '" /> <br/>';
}
}
and call it like:
show('images/navidad/');
How can I show ñ char inside img source in php, I have tried urlencode,urldecode..
But had no success
Try:
header('Content-Type: text/html; charset=utf-8');
$test="Navideño-4.jpg";
echo htmlspecialchars( $test );
You may try
$test = "Navideño-4.jpg";
echo utf8_decode($test);
or
$test = utf8_encode("Navideño-4.jpg");
echo utf8_decode($test);
You can use pathinfo and urlencode to display the image.
function show($path){
$files = glob($path.'*');
natcasesort($files);
foreach($files as $file) {
$path_parts = pathinfo($file);
echo ' <br/> <img src="' . $path_parts['dirname'] . '/' . urlencode($path_parts['basename']) . '" /> <br/>';
}
}
You can use utf8_decode
echo ' <br/> <img src="' . utf8_decode($file) . '" /> <br/>';
I just tried your code with php version 5.4.16 and it was working fine for me.
I think its your php version issue. Please check your php version with phpinfo()