Manaal Sarraf Manaal Sarraf - 3 months ago 11
PHP Question

My random file php script isn't working right

So I have this php script that will by random display files from a certain directory and for me it's not working :/ the page just shows up blank even tho I uploaded html files to the directory, here's the script I used:

<?php
error_reporting(E_ALL);


function is_image($string){
return ((file_exists($string))&&(preg_match('#(\.jpg|\.jpeg|\.gif|\.png|\.bmp)$#i',$string))) ? true : false ;
}

function is_text($string){
return ((file_exists($string))&&(preg_match('#(\.txt|\.php|\.html)$#i',$string))) ? true : false ;
}

define('OUTPUT_TYPE','html');
define('RANDOM_FILES_FOLDER','randomstuff/');

$my_array = Array();
$my_dir = RANDOM_FILES_FOLDER ;

if ($dir = @opendir("$my_dir")) {

while (($file = readdir($dir)) !== false) {
if ($file != "." && $file != ".." && !is_dir($my_dir.$file))
{
switch(OUTPUT_TYPE):

case'image':
if(is_image($my_dir.$file)){
$my_array[] = $file;
}
break;

case'text':
if(is_text($my_dir.$file)){
$my_array[] = $file;
}
break;

default:
break;

endswitch;

}
}
closedir($dir);
}


if(count($my_array)>0){

$random_number = rand(0, count($my_array)-1);
$random_file = $my_array[$random_number];

switch(OUTPUT_TYPE):

case'image':
echo "<img src=\"".$my_dir.$random_file."\" border=\"0\" alt=\"\">";
break;

case'text':
include($my_dir.$random_file);
break;

default:
break;

endswitch;

}
?>


your help will be appreciated

EDIT: my .htaccess file is causing the problem but I don't know how to fix it :( here's my .htaccess file:

# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC]

Answer

should do what first switch(OUTPUT_TYPE):???

check file type before it

Comments