wp_upload_bits()利用二进制信息新建一个文件

目录

描述

译文

用所给内容在上传文件夹中新建一个文件。

如果发生错误,错误信息中会出现关键字'key'。如果文件新建成功,则关键字'file'会被赋予唯一的文件路径,而'url'关键字则被赋予新文件的链接。'error'键值被设为false。

该函数不会将已经上传的文件再次上传到上传文件夹中。该函数会用$bits参数中的内容新建一个文件。如果用户移动上传文件,读取已上传文件中的内容,就可以将文件名和文件内容赋值给该函数,函数再将文件名和内容添加到上传文件夹。

该函数会自动设置新文件的访问权限。

原文

Create a file in the upload folder with given content.

If there is an error, then the key 'error' will exist with the error message. If success, then the key 'file' will have the unique file path, the 'url' key will have the link to the new file. and the 'error' key will be set to false.

This function will not move an uploaded file to the upload folder. It will create a new file with the content in $bits parameter. If you move the upload file, read the content of the uploaded file, and then you can give the filename and content to this function, which will add it to the upload folder.

The permissions will be set on the new file automatically by this function.

wp_upload_bits() 描述

用法

<?php wp_upload_bits$name$deprecated$bits$time ?>

wp_upload_bits() 用法

参数

$name

(string) (必填)

默认值: None

$deprecated

(null) (必填) Not used. Set to null.

默认值: None

$bits

(mixed) (必填) File content

默认值: None

$time

(string) (可选) Time formatted in 'yyyy/mm'.

默认值: null

wp_upload_bits() 参数

返回值

The function returns an array with the following keys:

file

The file system path to the uploaded file (e.g. /var/www/wordpress/wp-content/uploads/2010/03/example.pdf)

url

The URL to the uploaded file (e.g. http://example.com/wp-content/uploads/2010/03/example.pdf)

error

When something goes wrong with the upload, the error message is stored in this key. Otherwise set to false

wp_upload_bits() 返回值

示例

Here's a simple example assuming the request was made from a form with a file field called field1:

The function attempts to save a copy of the uploaded file to the upload directory set in the WordPress settings. It also performs security checks (file type, size, etc) and returns errors if any (see Return Values above). You might want to remove the tmp file after uploading.

wp_upload_bits() 示例

历史

添加于 版本: 2.0.0

wp_upload_bits() 历史

源文件

wp_upload_bits() 函数的代码位于 wp-includes/functions.php.

wp_upload_bits() 源文件