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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.