简体   繁体   中英

PHP how to rename uploaded file?

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!

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.

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.

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/

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.

move_uploaded_file($file['tmp_name'], 'images/'.$yourName.time().'.'.$type);

如果要在名称和时间之间使用分隔符:

move_uploaded_file($file['tmp_name'], 'images/'.$yourName.'-'.time().'.'.$type);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM