CODE
<?php
//connect to database//
$db_name="ilovephysics";
$connection = @mysql_connect("localhost","root","password") or die(mysql_error());
$db = @mysql_select_db($db_name,$connection) or die(mysql_error());
//collect data from previous page and assign to variables//
$date=$_POST['date'];
$time=$_POST['time'];
$title=$_POST['title'];
$category=$_POST['category'];
$description=$_POST['description'];
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
//assign SQL query to a variable//
$sql="INSERT INTO tbl_files (`date`, `time`, `title`, `category`, `description`, `type`, `name`, `size`, `content`) VALUES
('$date', '$time', '$title', '$category', '$description', '$fileType', '$fileName', '$fileSize', '$content')";
//run the query//
$result=@mysql_query($sql) or die(mysql_error());
?>
I'm not sure how to go about renaming a file (replacing spaces with "_").
At the moment when I try to download a file from my database, lets say for e.g. "Computing at Surrey.pdf", I end up with a file called Computing which does not have an extension.
Whereas if I have no spaces in the filename, "Computingatsurrey.pdf", I will get "Computingatsurrey.pdf" upon downloading.
The problem is a lot of users will be uploading their files onto my database and I would like for the files to be renamed automatically (eg Computing at Surrey.pdf to Computing_at_Surrey.pdf) during upload. How can this be done?
Thanking you in advance.
This post has been edited by jeansymolanza: 25 Feb, 2008 - 09:26 AM