PHP not echoing Ż correctly - php

I have a problem with PHP. It's not printing out name of a folder I've grabbed with readdir() properly.
The problem is only with a single character that is used in Polish.
<?php
//var with directory, put in the directory containing albums here
$main = "C:/Users/Jakub/Desktop/devving/gallery/images/";
$counter = 0;
$albumdata = [];
//gets all albums inside of the folder and stores inside of an array
if($opendir = opendir($main)){
while(($file = readdir($opendir)) !== false){
if($file == "." or $file == "..")continue;
$albumdata[$counter] = $file;
$counter += 1;
}
closedir($opendir);
$counter = 0;
}
//gets all files inside of albums and stores them inside of arrays with the names of the albums
for($i = 0;$i < count($albumdata); $i++){
if($opendir = opendir($main . $albumdata[$i])){
while(($file = readdir($opendir)) !== false){
if($file == "." or $file == "..") continue;
${$albumdata[$i]}[$counter] = $file;
$counter +=1;
}
closedir($opendir);
$counter = 0;
}
}
//echoes albums
for($i = 0;$i < count($albumdata); $i++){
$counter += 1;
if($counter == 1){
echo "<div class = 'firstimg'><a href = '"."images/".$albumdata[$i]."'><img src = '"."images/".$albumdata[$i]."/".${$albumdata[$i]}[0]."'><p>".$albumdata[$i]."</p></a></div>";
}else if($counter == 2){
echo "<div class = 'secondimg'><a href = '"."images/".$albumdata[$i]."'><img src = '"."images/".$albumdata[$i]."/".${$albumdata[$i]}[0]."'><p>".$albumdata[$i]."</p></a></div>";
}else{
echo "<div class = 'lastimg'><a href = '"."images/".$albumdata[$i]."'><img src = '"."images/".$albumdata[$i]."/".${$albumdata[$i]}[0]."'><p>".$albumdata[$i]."</p></a></div>";
$counter = 0;
}
}
?>
Here's the code. The albums in the $main directory are called Koty, Krajobrazy
Ptaki and Żaby. The problem is with the last one, instead of echoing Żaby in the paragraph
and the source it echoes �aby.
edit: Forgot to mention, I can echo Ż alone.

Related

Display uploaded file or image in grid in php

I have successfully uploaded some images to the server. Im thinking to display them in my admin page in grid view.. currently it displayed vertically.
Can anyone help?
Thanks in advance for you kind assistance.
<?php
$dir_path = "../../img/gallery/";
$extensions_array = array('jpg','png','jpeg');
if(is_dir($dir_path))
{
$files = scandir($dir_path);
for($i = 0; $i < count($files); $i++)
{
if($files[$i] !='.' && $files[$i] !='..')
{
// get file name
echo "File Name: $files[$i]<br>";
// get file extension
$file = pathinfo($files[$i]);
$extension = $file['extension'];
{
// show image
echo "<img src='$dir_path$files[$i]' style='width:60px;height:80px;'><br>
</br>";
}
}
}
}
?>
This code works fine but I just don't know to display it in grid or table..
You can achieve that by echoing each table part inside the loop:
<?php
$dir_path = "../../img/gallery/";
$extensions_array = array('jpg','png','jpeg');
$numCol = 3;
if(is_dir($dir_path))
{
$files = scandir($dir_path);
for($i = 0; $i < count($files); $i++)
{
$n = 0;
echo '<table>';
echo '<tr>';
if($files[$i] !='.' && $files[$i] !='..')
{
$n ++;
// get file name
echo "<td>File Name: $files[$i]<br>";
// get file extension
$file = pathinfo($files[$i]);
$extension = $file['extension'];
// show image
echo "<img src='$dir_path$files[$i]' style='width:60px;height:80px;'></td>";
if($n % $numCol == 0) echo "</tr><tr>";
}
echo '</tr>';
echo '</table>';
}
}
?>
$numCol defines how many column must have every table's row.
Also, I removed these {} that were useless:
$extension = $file['extension'];
{
// show image
echo "<img src='$dir_path$files[$i]' style='width:60px;height:80px;'><br>
</br>";
}

how to make an array of files which are part of a .zip file

i am trying to make an array of files which are part of a .zip file.
In the .zip file are 2 files: image1.jpg and image2.jpg
$zip = new ZipArchive;
if ($zip->open($_POST['extractfile']) === TRUE) {
$unzipped = 0;
$fails = 0;
$total = 0;
for ($i = 0; $i < $zip->numFiles; $i++) {
$path_info = pathinfo($zip->getNameIndex($i));
$ext = $path_info['extension'];
$total ++;
echo $zip->getNameIndex($i);
The echo outputs only the first file: image1.jpg
How can i make an array of the files which are in the .zip file so that i can use a foreach loop like below:
foreach($extractfiles as $extractfile) {
echo $extractfile;
}
To the second part
<?php
$zip = new ZipArchive;
$extractfiles = [];
if ($zip->open($_POST['extractfile']) === TRUE) {
$unzipped = 0;
$fails = 0;
$total = 0;
for ($i = 0; $i < $zip->numFiles; $i++) {
$path_info = pathinfo($zip->getNameIndex($i));
$ext = $path_info['extension'];
$total ++;
echo $zip->getNameIndex($i);
$extractfiles[] = $zip->getNameIndex($i);
}
}
foreach($extractfiles as $extractfile) {
echo $extractfile . "<br>" . PHP_EOL;
}

endless directory list php

i have this php code to list all files in a directory and output filesize and a download link.
<?
function human_filesize($bytes, $decimals = 2) {
$size = array(' B',' kB',' MB',' GB',' TB',' PB',' EB',' ZB',' YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . #$size[$factor];
}
$excludedFiles = array('.','..');
$excludedExtensions = array ('html','htm','php');
// Convert to lower case so we are not case-sensitive
for ($i = 0; isset($excludedFiles[$i]); $i++) $excludedFiles[$i] = strtolower(ltrim($excludedFiles[$i],'.'));
for ($i = 0; isset($excludedExtensions[$i]); $i++) $excludedExtensions[$i] = strtolower($excludedExtensions[$i]);
// Define the full path to your folder from root
$dir = "./";
// Open the folder
$dir_handle = #opendir($dir) or die("Unable to open $dir");
// Loop through the files
while ($file = readdir($dir_handle)) {
$extn = explode('.',$file);
$extn = array_pop($extn);
if (!in_array(strtolower($file),$excludedFiles) && !in_array(strtolower($extn),$excludedExtensions)){
if($file == "." || $file == ".." )
continue;
echo "<tr>
<td>
<a class='Testo' href=\"$file\" download>$file</a></td>
<td><font class='TestoPiccoloBo'>[" . human_filesize(filesize($file)) . "]</font></td>
</tr>";
}
}
// Close
closedir($dir_handle);
?>
i wanted the list to be alphabetically ordered so i added
$files = scandir($dir);
after the $dir line and
foreach ($files as $file){
after the while ($file = readdir($dir_handle)) { line
and a
}
before the closedir($dir_handle); line
now the files list in alphabetical order, but the list is endless. the list starts over and over, like a loop.
What am I doing wrong? Is this the correct way to accomplish this?
Any help would be much appreciated.
Thanks!
You can put your folder in an array and sort it with the php sort function. Then print them :
<?
function human_filesize($bytes, $decimals = 2) {
$size = array(' B',' kB',' MB',' GB',' TB',' PB',' EB',' ZB',' YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . #$size[$factor];
}
$excludedFiles = array('.','..');
$arrayFiles = array();
$excludedExtensions = array ('html','htm','php');
// Convert to lower case so we are not case-sensitive
for ($i = 0; isset($excludedFiles[$i]); $i++) $excludedFiles[$i] = strtolower(ltrim($excludedFiles[$i],'.'));
for ($i = 0; isset($excludedExtensions[$i]); $i++) $excludedExtensions[$i] = strtolower($excludedExtensions[$i]);
// Define the full path to your folder from root
$dir = "./";
// Open the folder
$dir_handle = #opendir($dir) or die("Unable to open $dir");
// Loop through the files
while ($file = readdir($dir_handle)) {
$extn = explode('.',$file);
$extn = array_pop($extn);
if (!in_array(strtolower($file),$excludedFiles) && !in_array(strtolower($extn),$excludedExtensions)){
if($file == "." || $file == ".." )
continue;
$arrayFiles[] = $file;
}
} // dunno what are these } so i put my loop after
// Close
closedir($dir_handle);
sort($arrayFiles);
foreach ($arrayFiles as $file) {
echo "<tr>
<td>
<a class='Testo' href=\"$file\" download>$file</a></td>
<td><font class='TestoPiccoloBo'>[" . human_filesize(filesize($file)) . "]</font></td>
</tr>";
}
?>

echo 15 random lines of html files

I have to do a php assignment for college.
How would I echo 15 random lines of a html file if there is a movie script text and a image with the same in the same folder?
What I have tried so far:
$folder = 'Filmnoir/';
$hitchcock ='Hitchcock/';
$shakespear ='Shakespear/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$file2 = glob ($hitchcock.$filetype);
$file3 = glob ($shakespear.$filetype);
$count = count($files);
$count1 = count($file2);
$count2 = count($file3);
$scripttype ='*.html';
$Scripts = glob($ScriptFilmNoir.$scripttype);
$Scripts=file_get_contents("DoubleIndemnity.html");
if($_POST['radio1']=="0"){
if(($i %1)==0){
for ($i = 0; $i < $count; $i++)
{
echo '<div class="image2">';
echo '<img src="'.$files[$i].'" />';
echo '</div>';
if (condition) {
include 'DoubleIndemnity.html'; }
echo '</td></tr>';
}
echo '<div class="Passwordtext">';
echo 'Type in password to view full Script';
echo '</div>';
echo "<label><div class=\"password\"><input type='password' name='code' value='code'/></div></label>";
echo "<form method='POST' action='ca1_result.php'>";
echo "<div class=\"SubmitIt\"><input type='submit' name='submitScript' value='Submit Password'/></div>";
echo '</form>';
}
}
if($_POST['radio1']=="1"){
echo '<div class="Sorrytext">';
echo 'We apologize. No script available for this movie.';
echo '</div>';
}
if($_POST['radio1']=="2"){
echo '<div class="Sorrytext">';
echo 'We apologize. No script available for this movie.';
echo '</div>';
}
I want to achieve that when there is an image (e.g DoubleIndemnity.png) and movie script (e.g DoubleIndemnity.html) in the same folder that I get 15 random lines of the movie script text plus the image. Could I use the glob function and when yes how would I achieve that?
Can I ask another thing? When I submit the password how to I get then the full movie script?
I tried:
foreach($files as $file2) {
if($_POST['submitPassword']){
if($file2 === '.' OR $file2 === '..' OR $file2 === 'thumbs.db' OR !is_dir($folder.'/'.$file2)) {continue;}
if(file_exists($folder.'/'.$file2.'/doubleindemnity.gif') AND file_exists($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm')) {
echo '<div class="Container">';
echo "<div class='image2'><img src='$folder/$file/doubleindemnity.gif'>";
$lines4 = file($folder.'/'.$file2.'/DOUBLEINDEMNITY.htm');
$count = count($lines4);
for($a = 0;$a < $count;$a++) {
echo substr($lines4[$a],strlen($folder),strpos($lines4[$a], '.')-strlen($folder));
}
echo "</div>";
}
echo "</div>";
}
}
?>
With that code I just get a couple of lines from the html file. I don't want that. I want the full text. And how can I use the string replace function to get rid of the code and just receive the text from the paragraphs?
Cheers:)
This piece of code will loop trough folders in a pre-defined folder (eg: /movies). It will look if whatever is inside is a folder, and if that folder has 2 files: image.png and lines.html. If it has those two files, it'll first place the image, then it'll read the HTML file, and writing down 15 random lines from that file.
<?php
$folder = "movies";
$files = scandir($folder);
foreach($files as $file) {
if($file === '.' OR $file === '..' OR $file === 'thumbs.db' OR !is_dir($folder.'/'.$file)) {continue;}
if(file_exists($folder.'/'.$file.'/image.png') AND file_exists($folder.'/'.$file.'/lines.html')) {
echo "<div class='image2'><img src='$folder/$file/image.png'>";
$lines = file($folder.'/'.$file.'/lines.html');
for($x = 1;$x<=15;$x++) {
echo $lines[rand(0, count($lines)-1)]."<br>";
}
echo "</div>";
}
}
Loops through files in $directory and then outputs 15 random lines into a html variable and if the image exists it adds it to the image html variable. If they are both added it returns the result. (untested but should work)
<?php
//I dont know where you have got some of these variables from
$directory = "Filmnoir";
$ScriptFilmNoir = "DoubleIndemnity";
$scripttype ='html';
$supportedImages = Array("png", "jpg", "jpeg");
$html = "";
$imagehtml = "";
$allFiles = scandir($folder);
$imageFound = 0;
$htmlFound = 0;
foreach($files as $file) {
if($file === '.' || $file === '..' || $file === 'thumbs.db' || is_dir($file)) {
continue;
}
$nameWithoutExtension = substr($file, 0 , (strrpos($file, ".")));
$fileExtension = $id = substr($file, strrpos($file, '.') + 1);
if($nameWithoutExtension == $ScriptFilmNoir){
if($fileExtension == $scripttype){
$htmlFound = 1;
$lines = file($directory . "/" . $ScriptFilmNoir . "." . $scripttype);//file in to an array
$range = range(1, count($lines));
$range = array_flip($range);
$range = array_rand($range, 15);
foreach($range as $line){
$html .= $lines[$value] . PHP_EOL;
}
}elseif(in_array ( $fileExtension , $supportedImages)){
$imageFound = 1;
$imagehtml = "<img src='{$directory}/{$ScriptFilmNoir}.{$fileExtension}' /><br />";
}
}
}
if($imageFound == 1 && $htmlFound == 1){
echo $imagehtml . $html;
}
I think what owen is looking for is a random extract of 15 lines rather than 15 random lines.
Using #ThijmenDF's example, just change
for($x = 1;$x<=15;$x++) {
echo $lines[rand(0, count($lines)-1)]."<br>";
}
to
$random = rand(0, count($lines)-1);
for($x = 1;$x<=15;$x++) {
echo $lines[$random + $x]."<br/>";
}

Php dynamic page pagination

So i have this page which lists '.txt' file contents on the website. I tried to paginate it, but it doesn't work. I'd like to have only one story per page (one story is data[0] . data[1])
The page is called to the browser via ajax.
Here is my code:
<?php
$dataArray = array();
//Number of chars for the string
$num = 500;
$dir = '../php/biralas_tortenetek/';
$willcount = readdir(opendir($dir));
$totfiles = count(readdir(opendir($dir)));
//Check if </div>DIR e</div>xists
if ($handle = opendir($dir)) {
//Loop over the directory
while (false !== ($file = readdir($handle))) {
//Strip out the . and .. files
if ($file != "." && $entry != "..") {
//Store file contents
$filecontent = file_get_contents($dir . $file);
//Split the content and store in array
$length = strlen($filecontent);
$dataArray[] = array(substr($filecontent, 0, $num), substr($filecontent, $num, $length ));
}
}
//close the dir
closedir($handle);
}
?><?php
$page = isset($_GET['page']) ? $_GET['page']-1 : 0;
echo "<br/>";
for($x=$page*1; $x < $totfiles && $x < ($page+1)*12; $x++)
{
foreach($dataArray as $data) { ?>
<div class="visible">
<?php echo $data[0] . $data[1]; ?>
</div><?php } ?>
</div>
<?php }
for($page=1; ($page-1)*12 < $totfiles; $page++)
{
echo "<div class='lapozo'><a onclick='story_changepage($page);' href='../html/blog.php#tortenetek?page=$page'>$page</a></div>";
}
?>
So again, the goal is to have only one story per page.
Thanks!
1st solution:
$willcount = readdir(opendir($dir));
$i = 0;
//Check if </div>DIR e</div>xists
if ($handle = opendir($dir)) {
//Loop over the directory
while (false !== ($file = readdir($handle))) {
//Strip out the . and .. files
if ($file != "." && $entry != "..") {
//Store file contents
$filecontent = file_get_contents($dir . $file);
//Split the content and store in array
$length = strlen($filecontent);
// store file as indexed item of array
$dataArray[$i++] = array(substr($filecontent, 0, $num), substr($filecontent, $num, $length ));
}
}
//close the dir
closedir($handle);
}
// store total files in dir
$totfiles = $i;
$page = isset($_GET['page']) ? $_GET['page']-1 : 0;
echo "<br/>";
for($x=$page*12; $x < $totfiles && $x < ($page+1)*12; $x++) {
$data = $dataArray[$x];
?>
<div class="visible">
<?php echo $data[0] . $data[1]; ?>
</div>
<?php
}
CUT 2nd Solution: // prefered It's use less memory than 1st solution
$willcount = readdir(opendir($dir));
$i = 0;
// get page
$page = isset($_GET['page']) ? $_GET['page']-1 : 0;
//Check if </div>DIR e</div>xists
if ($handle = opendir($dir)) {
//Loop over the directory
while (false !== ($file = readdir($handle))) {
//Strip out the . and .. files
if ($file != "." && $entry != "..") {
$i ++;
// if our page not first, skip add
if ($i <= $page * 12) continue;
// if we reach end of the page, break
if ($i > ($page + 1)* 12) break;
//Store file contents
$filecontent = file_get_contents($dir . $file);
//Split the content and store in array
$length = strlen($filecontent);
$dataArray[] = array(substr($filecontent, 0, $num), substr($filecontent, $num, $length ));
}
}
//close the dir
closedir($handle);
}
// store total files in dir
$totfiles = $i;
echo "<br/>";
foreach($dataArray as $data) {
?>
<div class="visible">
<?php echo $data[0] . $data[1]; ?>
</div>
<?php
}

Categories