Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,486 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,698 people online right now. Registration is fast and FREE... Join Now!




Renaming files on file upload?

 
Reply to this topicStart new topic

Renaming files on file upload?, Wanting to replace all spaces in files to "_" during file up

jeansymolanza
25 Feb, 2008 - 09:20 AM
Post #1

New D.I.C Head
*

Joined: 20 Feb, 2008
Posts: 34


My Contributions
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
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Renaming Files On File Upload?
25 Feb, 2008 - 10:01 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,212



Thanked: 216 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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.

php

$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!" decap.gif
User is online!Profile CardPM
+Quote Post

SpaceMan
RE: Renaming Files On File Upload?
25 Feb, 2008 - 10:01 AM
Post #3

D.I.C Regular
Group Icon

Joined: 20 Feb, 2003
Posts: 270

ya, what he said, lol

This post has been edited by SpaceMan: 25 Feb, 2008 - 10:03 AM
User is offlineProfile CardPM
+Quote Post

jeansymolanza
RE: Renaming Files On File Upload?
25 Feb, 2008 - 10:25 AM
Post #4

New D.I.C Head
*

Joined: 20 Feb, 2008
Posts: 34


My Contributions
Thanks a lot guys! I'm one step closer to completing the site now!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:21PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month