Posted: Wed Sep 13, 2006 12:56 pm
there is no 'type' key in the array $files... try 'ext' instead
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
switch($_GET['action']){
case: 'sort_by_type':
usort($files, 'sort_by_type');
break;
case: 'sort_by_size':
usort($files, 'sort_by_size');
break;
default:
echo "No sorting";
}When you have a 'Parse error:' message PHP is telling you that there is a problem with the way you wrote your code. It tells you what the problem is. It also tells you exactly where in your code the problem is. Try looking at the error message on your screen again then check the script and see if you can figure out what is happening.JustinMs66 wrote:huh. well i tried putting in that code, and it's saying:
Parse error: syntax error, unexpected ':' in /3.php
and for that, would i use the "include" thing for this?
and also... just for the listing and Not for the actual sorting, i can't get the "type" code to work with my "index.php"i just used that code we made for sorting... and it dosn't work. it says something like "invalid dir" and "invalid string".
Code: Select all
<?php
//php code for listing the title of all files in the directory.
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.php" && $file != "upload.php" && $file != "index.html" && !is_dir($file)) {
echo "<a href='" . $file . "'>" . $file . "</a><br/>";
}
}
closedir($handle);
}
?>Code: Select all
<?php
//php code for switching:
switch($_GET['action']){
case: 'sort_by_type':
usort($files, 'sort_by_type');
break;
case: 'sort_by_size':
usort($files, 'sort_by_size');
break;
default:
echo "No sorting";
}
?>Code: Select all
<?php
//tje php code that we made for sorting by file type.
//i thought it would work with this
$directory = "./uploads";
// Function to sort files by type
function sort_by_ext($a, $b){
// Compare the two files to see which is larger (you may refer to the usort() documentation to understand how this works)
if ($a['ext'] == $b['ext']) {
return 0;
}
return ($a['ext'] < $b['ext']) ? -1 : 1;
}
$dir = dir($directory);
$files = array();
while (($filename = $dir->read()) !== false) {
$file_info = array();
if(is_file($dir->path . "/" . $filename)){
// Gather file info and store it into an array
$file_info['name'] = $filename;
$file_info['path'] = realpath($dir->path . "/" . $filename);
$file_info['size'] = filesize($file_info['path']);
$apart = explode(".", $filename);
$key = count($apart)-1;
$ext = $apart[$key];
$file_info['ext'] = $ext;
$files[] = $file_info;
}
}
usort($files, 'sort_by_ext');
// This is just to show that it has been sorted
foreach($files as $file){
echo $file['name'] . "<br>";
echo "Extension:" . $file['ext'] . "<br><br>";
}
?>Code: Select all
<?php
//php code for listing the file size of the directories
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.php" && $file != "upload.php" && $file != "index.html" && !is_dir($file)) {
// $fruits = array($file);
// sort($fruits);
echo filesize($file) . ' bytes' . "<br/>";
}
}
closedir($handle);
}
?>then why did you put it in fourok, all of this code is inside the index.php file, exactly like this:
Code: Select all
blcoks here?
The first block echos the filenames immediatly. Is it unconditionally executed before the other three blocks?
The last block immediatly echos the filesize of all files in the current directory.
And the two in the middle do not make much sense ..at least in this order.
Sorry, it's all pellmell. Please describe in your own words what each block does and what you have and need before and what you have after it is executed.Code: Select all
blocks in because i have 4 different sections of PHP in my index.php file, so i was just trying to show that.
and i explain what each block is with a //comment at the top of each block. they are all from different files, and i am trying to put them all together into 1 file. but the sorting ones, i only want to be there, because they wont be used unless i call them with http://www.myurl.com/index.php?action=sort_by_size or something liek that.
so can i plz know how to do this?