简体   繁体   中英

Php code to check if file without and extention is really a directory or file

I want to check the file extension of in a root directory and also check if any folder or image but i have a problem when user upload a file that has not extension an show that it is a folder and file will not open because is actually not a folder.

Example user uploaded a file named Makefile and inside the make file it contain coffee script code like this

WORKER_JS_TARGET = build/worker.js WORKER_JS_SOURCES = \\ src/js/json2.js \\ src/js/worker.coffee.js \\ //and more.....

And in the same directory he also have folder named Loader and file box.php so now Loader show as folder and is okay, box.php show as file and is okay but Makefile show as folder but is not a folder what can i do?

<?php
$filename = 'datafile'
$extn=pathinfo($filename, PATHINFO_EXTENSION);

// Prettifies file type
switch ($extn){

case "txt": $extn="Text File"; break;
case "log": $extn="Log File"; break;
case "htm": $extn="HTML File"; break;
case "html": $extn="HTML File"; break;
case "xhtml": $extn="HTML File"; break;
case "shtml": $extn="HTML File"; break;
case "php": $extn="PHP Script"; break;
case "js": $extn="Javascript File"; break;
case "css": $extn="Stylesheet"; break;

default: if($extn!=""){
$extn = strtoupper($extn)." File";
} else if(!is_dir($name)){
$extn = "Other"; // After i pass this condition to check if is dir it show both Makefile and Loader as Other
}else if(is_dir($name)){
$extn = "Directory";
} else{
$extn = "Unknown";
} break;
}
?>

Any idea on how to do this work fine?

In this piece of code you checking the variable $name instead of $filename from what I can get from the given context.

} else if(!is_dir($name)){ //should be $filename
$extn = "Other"; 
}else if(is_dir($name)){ //should be $filename
$extn = "Directory";
} else{

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM