PHP's filesize() throws warning stat - php

I want to print the file size of the songs using the filesize() function in PHP. This is my code:
<?php
class parent_class{
function file_size($filename){
$size=filesize($filename);
if( $size>1023 && $size<1048575){
$size = $size/1024;
$size = " (". $size." kb)";
}
else if(file_exists($filename) && $size>1048575){
$size = $size/1048576;
$size = " (". round($size, 2) ." mb)";
}
else{
$size = " (". $size." b)";
}
return $size;
}
function displaysong(){
$songs = glob('songs/*.mp3');
foreach ($songs as $song){ ?>
<li class="mp3item">
<a href="<?php echo $song; ?>"> <?php echo basename($song); echo $this->file_size($song);
?></a>
</li>
<?php }
}
function display_playlists(){
$playlists = glob('songs/*.txt');
foreach ($playlists as $file){ ?>
<li class="playlistitem">
<?php echo basename($file) ; echo $this->file_size($file);?>
</li>
<?php }
}
function display_indivsong($file){ ?>
<li class="mp3item">
<?php $path = "songs/$file";?>
<?php echo $file; echo $this->file_size($path);?>
</li>
<?php }
} ?>
<body>
<div id="header">
<h1>190M Music Playlist Viewer</h1>
<h2>Search Through Your Playlists and Music</h2>
</div>
<div id="listarea">
<?php $song = NULL?>
<form method="$_REQUEST" >
<ul id="musiclist" name="playlist">
<?php
$song = new parent_class();
if(!empty($_REQUEST["playlist"])){
$playlist = $_REQUEST["playlist"];
$path = pathinfo($playlist, PATHINFO_EXTENSION);
}
if(empty($playlist)){
$song->displaysong();
$song->display_playlists();
}
else if(!empty($playlist) && $path == "txt"){
$list = $playlist;
$content = file("songs/$list");
foreach($content as $indiv_song){
$song->display_indivsong($indiv_song);
}
}
?>
Everything works alright when I display the songs and their sizes. However, when display_indivsong() is called, filesize() throws an error. Note, inside display_indivsong(), the file is used again and it throws an error. But for the first time it is used in the same file, the sizes of the files are returned with no problems.

In display_indivsong() you are appending "songs/" to the filename before calling $this->file_size($path). Instead of passing $path, you should instead pass the original $file:
function display_indivsong($file){ ?>
<li class="mp3item">
<?php $path = "songs/$file";?>
<?php echo $file; echo $this->file_size($file);?>
</li>
<?php
}
This is because the argument passed to $file already contains the path.
Also, you're reading filenames from a textfile and then looping through those filenames:
$content = file("songs/$list");
foreach($content as $indiv_song){
$song->display_indivsong($indiv_song);
}
This will cause problems if you don't trim the trailing EOL. You should change that line to:
$song->display_indivsong(trim($indiv_song));

Related

Script that gets images wont show new images past 7 without a limit

I got a script that gets images from a folder and puts them inside a lightgallery. All is working fine, except when I add more than 7 images (or any new images for that matter). There are 7 images in the folder when I add 8.jpg it shows nothing, not in the gallery and not in the source code. What could be the cause of this?
My script:
<div id="lightgallery">
<a href="<? echo $imagesmall; ?>">
<img class="fullnieuwsimg" src="<? echo $imagesmall; ?>">
</a>
<?
function scan_dir($dir) {
$ignored = array('.', '..', '.svn', '.htaccess','index.html');
$files = array();
foreach (scandir($dir) as $file) {
if (in_array($file, $ignored)) continue;
$files[$file] = filemtime($dir . '/' . $file);
}
ksort($files,SORT_NATURAL);
$files = array_keys($files);
return ($files) ? $files : false;
}
if(is_dir($_SERVER['DOCUMENT_ROOT'].'/_intern/web/cms/images/Projecten/'.$contentcr[0]['alias'].'/') != FALSE){
foreach(scan_dir($_SERVER['DOCUMENT_ROOT'].'/_intern/web/cms/images/Projecten/'.$contentcr[0]['alias'].'/') as $entry) {
$gallery .= '
<a href="/_intern/SNM/cms/images/Projecten/'.$contentcr[0]['alias'].'/'.$entry.'">
<img class="galleryimgs" title="'.$contentcr[0]['alias'].'" alt="'.$contentcr[0]['alias'].'" src="/_intern/web/cms/images/Projecten/'.$contentcr[0]['alias'].'/'.$entry.'" />
</a>';
}
}else{
echo '';
}
echo $gallery;
?>
</div>
The pictures are in the folder like this (barebone joomla cms) :
All work, except 8.jpg or any other new images I add.

Count filled SQL array fields PHP

I´m fetching an array from a mysql query like this.
$viewerSQL = mysql_query("SELECT * FROM `this` WHERE id = ".
mysql_real_escape_string($_GET[id]) ."");
$pageData = mysql_fetch_array($pageSql);
I get an array with like 40 entries, i want to check seven of them by name if they are filled.
What i´ve tried and what I thought about so far:
$oioi = 0;
while($oioi<7){
$oioi++;
$pic = "pic".$oioi;
$active="";
$path = "./upload/objectoffers/$viewerData[id]/$pic";
$picDesc = "pic".$oi."Desc";
$picArray = '$viewerData['.$pic.']';
echo $pic."<br>";
echo $picArray."<p>";
if(!empty($path)){
if($oioi==1){$active = "active";}?>
<div class="item <?=$active;?>">
<img style="width: 100%;" src="./images/<?=$viewerData[$pic];?>" alt="<?=$viewerData[$picDesc]?>">
</div>
<?
}}
?>
But this doesn´t work because: $path is NEVER empty...I wanted to avoid to write "if(!empty)" for every single $viewerData[pic1] to $viewerData[pic7] array manual...
that´s why i´m here...
maybe you can help me improving this or offer some tips.
Thanks!
edit: This is what I get as "output":
pic1
$viewerData[pic1]
pic2
$viewerData[pic2]
pic3
$viewerData[pic3]
pic4
$viewerData[pic4]
pic5
$viewerData[pic5]
pic6
$viewerData[pic6]
pic7
$viewerData[pic7]
I think you should use file_exists instead !empty:
$oioi = 0;
while ($oioi < 7) {
$oioi++;
$pic = "pic" . $oioi;
$active = "";
$path = "./upload/objectoffers/$viewerData[id]/$pic";
$picDesc = "pic" . $oi . "Desc";
$picArray = '$viewerData[' . $pic . ']';
echo $pic . "<br>";
echo $picArray . "<p>";
if (file_exists($path)) {
if ($oioi == 1) {
$active = "active";
}
?>
<div class="item <?= $active; ?>">
<img style="width: 100%;" src="./images/<?= $viewerData[$pic]; ?>" alt="<?= $viewerData[$picDesc] ?>">
</div>
<?php
}
}

Displaying menu names from CMS

I have been trying to display a menu name from my CMS database that I created myself, I got it displayed perfectly but there is a really weird error, I spent many hours to discover the solution but I couldn't.
The problem is that I want to display the menu name as page H1 in line 50 exactly so whenever you click on the menu name its also displaying as H1 title.
The menu name is displaying correctly, but the sub menu(pages) displaying N instead of the page name, I don't know where is that N is coming from.
NOTE: There is no problem with the database connection or retrieving data.
This is my content page code:
<?php include("includes/header.php"); ?>
<?php require_once 'includes/db_connection.php'; ?>
<?php require_once 'includes/b_functions.php'; ?>
<?php require_once 'includes/cms_constants.php'; ?>
<?php db_connect();?>
<?php
if (isset ( $_GET ['subj'] )) {
$sel_subject = get_subject_by_id ( $_GET ['subj'] );
$sel_page = "NULL";
} elseif (isset ( $_GET ['pages'] )) {
$sel_subject = false;
$sel_page = get_page_by_id ( $_GET ['pages'] );
} else {
$sel_subject = "NULL";
$sel_page = "NULL";
}
?>
<table id="structure">
<tr>
<td id="navigation">
<ul class="subjects">
<?php
$subject_set = get_all_subjects();
while ($subject = mysql_fetch_array($subject_set)) {
echo "<li";
if ($subject["id"] == $sel_subject["id"]) { echo " class=\"selected\""; }
echo "><a href=\"contents.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
$page_set = get_pages_for_subject($subject["id"]);
echo "<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
echo "<li";
if ($page["id"] == $sel_page["id"]) { echo " class=\"selected\""; }
echo "><a href=\"contents.php?pages=" . urlencode($page["id"]) .
"\">{$page["menu_name"]}</a></li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="page">
<?php if (!is_null($sel_subject)) { // subject selected ?>
<h2><?php echo $sel_subject['menu_name']; ?></h2>
<?php } elseif (!is_null($sel_page)) { // page selected ?>
<h2><?php echo $sel_page['menu_name']; ?></h2>
<div class="page-content">
<?php echo $sel_page['content']; ?>
</div>
<?php } else { // nothing selected ?>
<h2>Select a subject or page to edit</h2>
<?php } ?> </td>
</tr>
</table>
<?php require_once 'includes/footer.php';?>
Your 'N' is coming from the "NULL" string. I believe you meant to set your variables with actual NULL and not a string. Like:
$var = NULL;
As opposed to:
$var = "NULL";
When you access a variable that is set to "NULL" as an array, you will get the 'N' (as the string is treated as an array). For instance;
$var = "NULL";
echo $var[0]; // 'N'
Furthermore, if you test for NULL on a string it will return false (the variable is non-null);
$var = "NULL";
if($var) {
echo "var is: " . $var;
}
else {
echo "var is NULL!";
}
The above should output "var is: NULL".
I say this from experience (I did this when I started coding PHP). However, the root of your problem likely doesn't stop there as your variables should be set in subsequent conditionals. I would go through your while statements, and if statements and ensure your variables are being set appropriately. (var_dump($var) or print_r($var) through suspect blocks).

HTML Iframe get src with php

I have an webpage where I want to have another html page displayed. I used iframes. The page gets to know what to load is via the get procedure. But there is an fault in this coding I think...
<iframe src="
<?
$file = ($_GET['ti'])
if ($title = '')
echo "information.html";
else echo "$file";
?>
"></iframe>
The url the page would recieve looks like this:
http://www.website.com/reference.html?ti=unlimited.html
http://www.w3schools.com/php/php_if_else.asp
It's your if / else syntax and over all php code. It's not very well written.
<?php
$file = $_GET['ti'];
if ($title = '') {
echo "information.html";
} else {
echo "$file";
}
?>
Need semicolon:
$file = ($_GET['ti']);
Use empty(), like this:
<iframe src="
<?
$file = ($_GET['ti'])
if (empty($title))
echo "information.html";
else echo $file;
?>
"></iframe>
<?php
$possible = array("information.html", "home.html", "test.html");
$file = isset($_GET['ti']) &&
in_array($_GET['ti'], $possible)? $_GET['ti'] : "information.html";
?>
<iframe src="<?php echo $file;?>"></iframe>

How to optimize menu navigation

I'm building a menu options, having issue in last option, The Anchor method doesn't work as a link popup a new window. Besides, in option 1 and 2, I repeat those codes which is not look great.
Is there a better way to optimize those codes? make it cleaner.
In my controller:
public function loadPage($name, $pageID) {
$data['title'] = $this->tabPageData;
$data['tabMenu'] = $this->model->getAllMenuItems();
if ($name == 'portfolio-1') {
// load portfolio 1, get the page content (photos) and its name
$data['tabPageContent'] = $this->model->getPageContentByPageID($pageID);
$data['pageName'] = $this->model->getPageNameByID($pageID);
} elseif ($name == 'portfolio-2') {
$data['tabPageContent'] = $this->model->getPageContentByPageID($pageID);
$data['pageName'] = $this->model->getPageNameByID($pageID);
} elseif ($name == 'contact') {
// load Contact page
$data['tabContact'] = $this->model->getContactByPageID($pageID);
} else {
// load a Blog site
echo anchor('http://mysite.tumblr.com', 'target=_blank');
}
$this->load->view('content', $data);
}
In my View:
<div id="menu">
<ul>
<?php foreach ($tabMenu as $item) : ?>
<?php
$url = "<li><a href='" . base_url();
$url .= str_replace("+", "-", urlencode(strtolower($item->name))) . "/". ($item->cat_id) . "'>";
$url .= strtoupper($item->name) . "</a></li>";
echo $url;
?>
<?php endforeach; ?>
</ul>
</div> <!-- end of Menu -->
I would suggest that you clean up your view by creating a helper method that generates a list item for your navigation.
Put the following code in a file named navigation_helper.php in application/helpers/.
if (!defined('BASEPATH')) exit('No direct script access allowed');
if (!function_exists('build_list_item'))
{
function build_list_item ($item) {
$url_item_name = str_replace('+', '-', urlencode(strtolower($item->name)));
$url = base_url() . $url_item_name . "/". $item->cat_id;
return '<li>' . strtoupper($item->name) . '</li>';
}
}
Make sure you are loading the helper in your controller or autoloading it if you use it often.
$this->load->helper('navigation_helper');
Then in your view you could do this:
<div id="menu">
<ul>
<?php foreach ($tabMenu as $item): ?>
<?php echo build_list_item($item); ?>
<?php endforeach; ?>
</ul>
</div>

Categories