Renaming files on file upload?

Wanting to replace all spaces in files to "_" during file up

Page 1 of 1

3 Replies - 4204 Views - Last Post: 25 February 2008 - 11:25 AM Rate Topic: -----

#1 jeansymolanza   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 20-February 08

Renaming files on file upload?

Posted 25 February 2008 - 10:20 AM

<?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//
$[email protected]_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 February 2008 - 10:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Renaming files on file upload?

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: Renaming files on file upload?

Posted 25 February 2008 - 11:01 AM

Well where you store your $fileName variable you can simply do a str_replace() on the name before putting it in the variable. Then when you go to move the file (using move_uploaded_file()) you can use the variable you just created to move the file and rename it in one swoop.

$fileName = str_replace(" ", "_", basename($_FILES['userfile']['name']));

// Insert the name into the database as you are

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . $fileName;

// Move current temp file to destination that has spaces replaced with underscores
move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile);



The code above should give you a rough idea of how to do this. Essentially take the name, replace the spaces with underscores, create a destination path with it, then move the temp file to the destination which also renames it to the original name (with underscores now).

I hope this makes sense. Enjoy!

"At DIC we be php file upload moving code ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 SpaceMan   User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: Renaming files on file upload?

Posted 25 February 2008 - 11:01 AM

ya, what he said, lol

This post has been edited by SpaceMan: 25 February 2008 - 11:03 AM

Was This Post Helpful? 0
  • +
  • -

#4 jeansymolanza   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 20-February 08

Re: Renaming files on file upload?

Posted 25 February 2008 - 11:25 AM

Thanks a lot guys! I'm one step closer to completing the site now!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1