Jump to content

Upload form help


undertaker

Recommended Posts

What i have here is a code for uploading files to specified folder.

I have a problem which i cant found in code. All uploaded files get a new name with many numbers.

I dont want this because i cant collect files for gallery because filenames are different as uploaded ones.

 

Example:

I have a file biggreen.gif on my computer. I upload it trough this form, and when i check that file in folder i uploaded to, the name is 1299616037_biggreen.gif.

 

Help me please. :) :)

 

<?php 
//Load the settings
require_once("settings.php");

$message = "";
//Has the user uploaded something?
if(isset($_FILES['file']))
{
$target_path = Settings::$uploadFolder;
$target_path = $target_path . time() . '_' . basename( $_FILES['file']['name']); 

//Check the password to verify legal upload
if($_POST['password'] != Settings::$password)
{
	$message = "Invalid Password!";
}
else
{
	//Try to move the uploaded file into the designated folder
	if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
	    $message = "The file ".  basename( $_FILES['file']['name']). 
	    " has been uploaded";
	} else{
	    $message = "There was an error uploading the file, please try again!";
	}
}

//Clear the array
unset($_FILES['file']);
}

if(strlen($message) > 0)
{
$message = '<p class="error">' . $message . '</p>';
}

/** LIST UPLOADED FILES **/
$uploaded_files = "";

//Open directory for reading
$dh = opendir(Settings::$uploadFolder);

//LOOP through the files
while (($file = readdir($dh)) !== false) 
{
if($file != '.' && $file != '..')
{
	$filename = Settings::$uploadFolder . $file;
	$parts = explode("_", $file);
	$size = formatBytes(filesize($filename));
	$added = date("m/d/Y", $parts[0]);
	$origName = $parts[1];
	$filetype = getFileType(substr($file, strlen($file) - 3));
        $uploaded_files .= "<ul class=\"list\"><li class=\"$filetype\"><a href=\"$filename\">$origName</a> $size - $added</li></ul>\n";
}
}
closedir($dh);

if(strlen($uploaded_files) == 0)
{
$uploaded_files = "<ul class=\"list\"><li><em>No files found</em></li></ul>";
}

function getFileType($extension)
{
$images = array('jpg', 'gif', 'png', 'bmp');
$docs 	= array('txt', 'rtf', 'doc');
$apps 	= array('zip', 'rar', 'exe');

if(in_array($extension, $images)) return "Images";
if(in_array($extension, $docs)) return "Documents";
if(in_array($extension, $apps)) return "Applications";
return "";
}

function formatBytes($bytes, $precision = 2) { 
    $units = array('B', 'KB', 'MB', 'GB', 'TB'); 
   
    $bytes = max($bytes, 0); 
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
    $pow = min($pow, count($units) - 1); 
   
    $bytes /= pow(1024, $pow); 
   
    return round($bytes, $precision) . ' ' . $units[$pow]; 
} 
?>

Link to comment
https://forums.phpfreaks.com/topic/230026-upload-form-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.