Reputation: 627
I want to rename file and upload it into server as well as in database. I have a code:
$file = $_FILES['file'];
list(,$type) = explode('/', $file['type']);
move_uploaded_file($file['tmp_name'], 'images/'.time().'.'.$type);
$a = time();
$b = ($a.'.'.$type);
It is working, but I'm not exactly sure how it works. How can I change this 'images/'.time().'.'.$type
into name of which I want to, for example: 'images/'myname.time().'.'.$type)
All those dots and quotes are killing me!
Upvotes: 1
Views: 3321
Reputation: 1104
If the dots are killing you, change them into plus ( + ) signs. They get to me as well sometimes, as it is difficult for my mind to understand if that is dot notation when trying to access a member of an object/class or a string appending operation. Here your third line re-imagined:
$newName = 'aDifferentFileName';
$finalFilePath = 'images/' + $newName + time() + '.' + $type;
move_uploaded_file( $file['tmp_name'], $finalFilePath );
Often times I will stay away from concatenating using the period for ease of reading.
Upvotes: 5
Reputation: 33391
The dots are concatenation operations (to connect strings together).
Move upload file looks like this: move_uploaded_file ( string $filename , string $destination )
.
$file
name is the temp name that was uploaded to the temporary directory and $string
is the destination path you are moving it to.
In your case you can just do:
$myname = 'thisismyname';
$filename = 'images/' . $myname . time() . '.' . $type;
move_uploaded_file($file['tmp_name'], $filename); //Move the file
//Save $filename to the database here
Upvotes: 1
Reputation: 3775
move_uploaded_file($file['tmp_name'], 'images/'.$yourName.time().'.'.$type);
if you want a separator between name and time:
move_uploaded_file($file['tmp_name'], 'images/'.$yourName.'-'.time().'.'.$type);
Upvotes: 1
Reputation: 18833
Yes, you can
http://php.net/manual/en/function.move-uploaded-file.php
$new_name = 'yournewname'.time().'.'.$type;
move_uploaded_file($file['tmp_name'], 'images/'.$new_name);
then use $new_name for your database query.
Upvotes: 2
Reputation: 4854
The dots are the PHP string concatenation operator. See here: http://www.phpf1.com/tutorial/php-string-concatenation.html for a quick overview of how it works.
If it makes you more comfortable, you could convert the name generation into a string formatting operation using sprintf: http://php.net/manual/en/function.sprintf.php
Basically, the new name of your file will be the second argument to move_uploaded_file, so you can put anything you want there.
Upvotes: 1
Reputation: 887
move_uploaded_file() takes two arguments - the old name and the new name. The second is the new name so you can specify anything you want.
The dot is the concatenation operator in PHP.
'images/'.time().'.'.$type);
Means "take the static string 'images/', put the return value of time() on the end, then a period, then the contents of the variable $type.
For example, if time() returned "1" and $type contained "example", the result would be "images/1.example"
Which would effectively rename your uploaded file to 1.example in the directory images/
Upvotes: 1