Page 1 of 1
rename parse error
Posted: Mon Aug 20, 2007 11:14 pm
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?
Posted: Mon Aug 20, 2007 11:29 pm
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.
Re: rename parse error
Posted: Tue Aug 21, 2007 7:04 am
by superdezign
m2babaey wrote:also what happens to the extension when i want to rename a file?
You have to specify it.
Posted: Tue Aug 21, 2007 8:35 am
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.
Posted: Tue Aug 21, 2007 8:45 am
by VladSun
@Jcart: using any kind of random is still vulnerable to collisions.
Using
tempnam() is much better.
Posted: Tue Aug 21, 2007 9:01 am
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.
Posted: Tue Aug 21, 2007 9:12 am
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.
Posted: Tue Aug 21, 2007 9:16 am
by feyd
Even with tempnam().
Posted: Tue Aug 21, 2007 9:18 am
by VladSun
Due to loose coding or because of its nature?
Posted: Tue Aug 21, 2007 9:26 am
by feyd
It's attempting to avoid a race condition, just like all the other methods.