File Upload Issue

Discussion in 'PHP' started by RNK Concepts, Jul 29, 2008.

  1. #1
    Hey all of you PHP guys out there ... can anyone take a look at this code snippet and let me know if you see some sort of error that would just make the page refresh and not upload the file?

    Here's the code:

    There is some code right above that is not necessary to be seen here ... The below code is where the actual script starts minus a few defined variables.

    if(!function_exists('imagecreatetruecolor')) $showthumbnails = false;
    $leadon = $startdir;
    if($leadon=='.') $leadon = '';
    if((substr($leadon, -1, 1)!='/') && $leadon!='') $leadon = $leadon . '/';
    $startdir = $leadon;
    
    if($_GET['dir']) {
    	//check this is okay.
    
    	if(substr($_GET['dir'], -1, 1)!='/') {
    		$_GET['dir'] = $_GET['dir'] . '/';
    	}
    
    	$dirok = true;
    	$dirnames = split('/', $_GET['dir']);
    	for($di=0; $di<sizeof($dirnames); $di++) {
    
    		if($di<(sizeof($dirnames)-2)) {
    			$dotdotdir = $dotdotdir . $dirnames[$di] . '/';
    		}
    
    		if($dirnames[$di] == '..') {
    			$dirok = false;
    		}
    	}
    
    	if(substr($_GET['dir'], 0, 1)=='/') {
    		$dirok = false;
    	}
    
    	if($dirok) {
    		 $leadon = $leadon . $_GET['dir'];
    	}
    }
    
    if($_GET['download'] && $forcedownloads) {
    	$file = str_replace('/', '', $_GET['download']);
    	$file = str_replace('..', '', $file);
    
    	if(file_exists($leadon . $file)) {
    		header("Content-type: application/x-download");
    		header("Content-Length: ".filesize($leadon . $file));
    		header('Content-Disposition: attachment; filename="'.$file.'"');
    		readfile($leadon . $file);
    		die();
    	}
    }
    
    if($allowuploads && $_FILES['file']) {
    	$upload = true;
    	if(!$overwrite) {
    		if(file_exists($leadon.$_FILES['file']['name'])) {
    			$upload = false;
    		}
    	}
    
    	if($upload) {
    		move_uploaded_file($_FILES['file']['tmp_name'], $leadon . $_FILES['file']['name']);
    	}
    }
    
    $opendir = $leadon;
    if(!$leadon) $opendir = '.';
    if(!file_exists($opendir)) {
    	$opendir = '.';
    	$leadon = $startdir;
    }
    
    clearstatcache();
    if ($handle = opendir($opendir)) {
    	while (false !== ($file = readdir($handle))) {
    		//first see if this file is required in the listing
    		if ($file == "." || $file == "..")  continue;
    		$discard = false;
    		for($hi=0;$hi<sizeof($hide);$hi++) {
    			if(strpos($file, $hide[$hi])!==false) {
    				$discard = true;
    			}
    		}
    
    		if($discard) continue;
    		if (@filetype($leadon.$file) == "dir") {
    			if(!$showdirs) continue;
    
    			$n++;
    			if($_GET['sort']=="date") {
    				$key = @filemtime($leadon.$file) . ".$n";
    			}
    			else {
    				$key = $n;
    			}
    			$dirs[$key] = $file . "/";
    		}
    		else {
    			$n++;
    			if($_GET['sort']=="date") {
    				$key = @filemtime($leadon.$file) . ".$n";
    			}
    			elseif($_GET['sort']=="size") {
    				$key = @filesize($leadon.$file) . ".$n";
    			}
    			else {
    				$key = $n;
    			}
    			$files[$key] = $file;
    
    			if($displayindex) {
    				if(in_array(strtolower($file), $indexfiles)) {
    					header("Location: $file");
    					die();
    				}
    			}
    		}
    	}
    	closedir($handle);
    }
    
    //sort our files
    if($_GET['sort']=="date") {
    	@ksort($dirs, SORT_NUMERIC);
    	@ksort($files, SORT_NUMERIC);
    }
    elseif($_GET['sort']=="size") {
    	@natcasesort($dirs);
    	@ksort($files, SORT_NUMERIC);
    }
    else {
    	@natcasesort($dirs);
    	@natcasesort($files);
    }
    
    //order correctly
    if($_GET['order']=="desc" && $_GET['sort']!="size") {$dirs = @array_reverse($dirs);}
    if($_GET['order']=="desc") {$files = @array_reverse($files);}
    $dirs = @array_values($dirs); $files = @array_values($files);
    
    
    ?>
    Code (markup):
        <?
        if($allowuploads) {
            $phpallowuploads = (bool) ini_get('file_uploads');
            $phpmaxsize = ini_get('upload_max_filesize');
            $phpmaxsize = trim($phpmaxsize);
            $last = strtolower($phpmaxsize{strlen($phpmaxsize)-1});
            switch($last) {
                case 'g':
                    $phpmaxsize *= 1024;
                case 'm':
                    $phpmaxsize *= 1024;
            }
    
        ?>
        <div id="upload">
            <div id="uploadtitle"><strong>File Upload</strong> (Max Filesize: <?=$phpmaxsize;?>KB)</div>
            <div id="uploadcontent">
                <?
                if($phpallowuploads) {
                ?>
                <form method="post" action="<?=$_SERVER['PHP_SELF'];?>?dir=<?=urlencode($leadon);?>" enctype="multipart/form-data">
                <input type="file" name="file" /> <input type="submit" value="Upload" />
                </form>
                <?
                }
                else {
                ?>
                File uploads are disabled in your php.ini file. Please enable them.
                <?
                }
                ?>
            </div>
    
        </div>
        <?
        }
        ?>
    Code (markup):
    Any help is much appreciated ... Thanks!
     
    RNK Concepts, Jul 29, 2008 IP
  2. RNK Concepts

    RNK Concepts Peon

    Messages:
    74
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Problem solved.
     
    RNK Concepts, Jul 29, 2008 IP