gpt4 book ai didi

php - Node.js fs.readFile() 与 PHP 的 file_get_contents() 一样工作吗?

转载 作者:太空宇宙 更新时间:2023-11-03 23:03:49 26 4
gpt4 key购买 nike

我有以下脚本来读取图像并将其打印为字符串:

PHP:

<?php
echo file_get_contents("/path/to/small.png");

Node.js:

var fs = require('fs')
var file = fs.readFileSync('/path/to/small.png', 'utf8');
console.log(file)

但是两个脚本输出的字符串之间有一点差异。我使用以下 Go 代码尝试了同样的操作,输出与 PHP 的输出相同:

package main

import (
"fmt"
"io/ioutil"
)

func main() {
buf, err := ioutil.ReadFile("/path/to/small.png")
if err != nil {
panic(err)
}

content := string(buf)
fmt.Println(content)
}

那么,有人知道为什么 fs.readFile() 的行为不同吗?

最佳答案

var fs = require('fs')
var file = fs.readFileSync('/path/to/small.png', 'binary');
console.log(file)

引用文献:

https://nodejs.org/dist/latest-v7.x/docs/api/fs.html#fs_fs_readfilesync_file_options

https://github.com/nodejs/node/blob/master/lib/buffer.js#L432

关于php - Node.js fs.readFile() 与 PHP 的 file_get_contents() 一样工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41122243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com