用户使用 HTML 表单上传文件后,有什么方法可以让 php 脚本为文件选择名称吗?我希望允许用户为他们的帐户上传头像,并希望用他们的用户 ID 来命名,而不是用他们计算机上的任何名称来命名。我使用的是基本的 HTML 上传表单,该表单仅允许 jpeg 和 png 文件,文件大小限制为 10MB,类似于 http://www.w3schools.com/php/php_file_upload.asp 如果您能提供任何帮助,我们将不胜感激。
Is there any way for a php script to choose the name for a file after it's been uploaded by the user using an HTML form? I am wanting to allow users to upload an avatar for their account and would like it named with their userid instead of whatever the name of it is on their computer. I'm using a basic HTML upload form which only allows jpegs and png files with a 10MB file limit, similar to the file upload code give on http://www.w3schools.com/php/php_file_upload.asp Any help you can give will be greatly appreciated.
发布评论
评论(3)
将所需的文件名放在
move_uploaded_file()
。
Put the desired filename in the second argument of
move_uploaded_file()
.您可以在使用 move_uploaded_file() 时指定文件名,否则您可以 rename() 文件。
You can specify the filename when using move_uploaded_file(), otherwise you can rename() the file.
更新:
我认为你不需要提取文件扩展名。
UPDATE:
I think you don't need to extract file extension.