rename parse error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

rename parse error

Post by m2babaey »

Hi
i get parse error. how should the code be to work?
thanks

Code: Select all

<?
 move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
      $newname=md5(time());
      rename('uploads/'. $_FILES["file"]["name"]','uploads/'.'$newname);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
?>
also what happens to the extension when i want to rename a file?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Usually PHP tells you where your parse error is. If you look at the code you posted, it stands out like a sore thumb.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: rename parse error

Post by superdezign »

m2babaey wrote:also what happens to the extension when i want to rename a file?
You have to specify it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You should consider creating more random filenames. As it is if two uploads occur within the same second there will be a filename collision.

microtime() and rand() may be of interest.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

@Jcart: using any kind of random is still vulnerable to collisions.
Using tempnam() is much better.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There are many ways of generating a good filename. uniqid() can be combined with the username, microtime(), user-agent string... all sorts of data... then hashed. The variety is endless. It may also be said to use a longer hash such as sha1() or sha256; even different encoding schemes can be useful (different from simple hex.) In the end, it's always possible to have a collision, so make sure your code can handle such an event and all will be happy.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

feyd wrote:There are many ways of generating a good filename. In the end, it's always possible to have a collision, so make sure your code can handle such an event and all will be happy.
Even with tempnam?
The temporary file is also created to avoid a race condition where the file might appear in the filesystem between the time the string was generated and before the script gets around to creating the file.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Even with tempnam().
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Due to loose coding or because of its nature?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's attempting to avoid a race condition, just like all the other methods.
Post Reply