thats right it would.
Here’s a script that should take care of it you might need to tweak it slightly to fit your needs.
You’ll need a database table to store the file paths in. Create a table such as:
CREATE TABLE IF NOT EXISTS `files` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(255) NOT NULL,
`file` varchar(255) NOT NULL,
`size` varchar(255) NOT NULL,
`type` varchar(255) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
This table will store a title the path to the file its size and type the id will auto increment on each entry.
In this file I’ve placed a connection to a database you will need to enter the right username and passwords etc also some functions the script will use ideally these would be in a separate file I’ve placed them together for simplicity.
For this to work there would be a folder on the server called files that has write permissions.
I commented the file to try to make it as clear as possbile
[php]
<?php
//connct to database
define('DB_SERVER', 'localhost');
define('DB_USER', 'database username');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'database name');
@$conn = mysql_connect (DB_SERVER, DB_USER, DB_PASSWORD);
mysql_select_db (DB_NAME,$conn);
if(!$conn){
die( "Sorry! There seems to be a problem connecting to our database. Please give us a few minutes to remedy the problem. Thank you.");
}
// function the script will use ideally would be in a functions.php also the database connection would really in in its own file to such as config.php
function getFileType($extension)
{
$images = array('jpg', 'gif', 'png', 'bmp');
$docs = array('txt', 'rtf', 'doc', 'pdf');
$apps = array('zip', 'rar', 'exe', 'html');
$video = array('mpg', 'wmv', 'avi');
$audio = array('wav', 'mp3');
$db = array('sql', 'csv', 'xls');
if(in_array($extension, $images)) return "Image";
if(in_array($extension, $docs)) return "Document";
if(in_array($extension, $apps)) return "Application";
if(in_array($extension, $video)) return "Video";
if(in_array($extension, $audio)) return "Audio";
if(in_array($extension, $db)) return "Database/Spreadsheet";
return "Other";
}
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];
}
function errors($error){
if (!empty($error))
{
$i = 0;
while ($i < count($error)){
$showError.= '
'.$error[$i].'
';
$i ++;}
return $showError;
}// close if empty errors
} // close function
//form been submissed
if (isset($_POST['upfile'])){
// check feilds are not empty
$title = trim($_POST['title']);
if (empty($title)){
$error[] = 'Please enter a Title.';
}
$sql = "SELECT id FROM files WHERE title = '$title' ";
$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
if (mysql_num_rows($result) == 1) {//thee's a match
$error[] = 'File title already exists please choose another title.';
}
//if no errors carry on
if (!$error){
//where to move upload file to
$target = "files/" . $_FILES['uploaded']['name'];
//is an uploaded file
if ($_FILES["uploaded"]["type"])
{
move_uploaded_file($_FILES["uploaded"]["tmp_name"],$target);
$file = $_FILES["uploaded"]["name"];
$size = $_FILES["uploaded"]["size"];
$type = $_FILES["uploaded"]["type"];
//insert into database
mysql_query(" INSERT INTO files (title, file, size, type) VALUES ('$title', '$file', '$size', '$type'")or die('Error : ' . mysql_error());
//show a result
echo "
".$_FILES["uploaded"]["name"]." uploaded
";
} else {
echo "File not uploaded.";
}
}// end no error
}//close if isset upfile
?>
Upload File
<?php
//show any errors
echo errors($error);
?>
Title: File:
Documents
|
Filename |
Type |
Size |
Action |
<?php
//pull all files from databsase and loop through them
$q = mysql_query("SELECT * FROM files ORDER BY id");
while($row=mysql_fetch_object($q)) {
//get the size of the file
$size = formatBytes(filesize("files/".$row->file));
//get file type
$filetype = getFileType(substr($row->file, strlen($row->file) - 3));
echo "
".$row->id." |
".$row->title." |
".$filetype." |
".$size." |
Download |
";
}
?>
[/php]