从 PHP 上传的文件中读取单个字节

发布于 2024-10-31 14:53:49 字数 839 浏览 0 评论 0 原文

我对 PHP 很陌生,所以请原谅我的无知。

我有用于上传的 HTML:

<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /><br />
<input type="submit" name="submit" value="Submit" />
</form>

我的 PHP 是(它不知道,但希望它显示我想要的内容):

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  $content = fread($_FILES["file"],10);
  echo $content;
  echo "byte10 is: " ???
  }
?>

我想要:从这个上传的文件中,我想要读取单个字节(比如字节 10)。我想打印出该字节的 accii (HEX) 代码。我该怎么做?我需要将文件保存到服务器吗?

最终目标是加密文件并将加密的文件发送回用户。所以我想(1)上传文件(2)读取每个单独的字节(2)在字节级别执行加密( 3)保存文件并发回给用户)

I am very new to PHP, so forgive me my ignorance.

I have HTML for for upload:

<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /><br />
<input type="submit" name="submit" value="Submit" />
</form>

My PHP is (It doens't know, but hopefully it shows what I want):

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  $content = fread($_FILES["file"],10);
  echo $content;
  echo "byte10 is: " ???
  }
?>

I want to: From this uploaded file, I want to read a single byte (say byte 10). I want to print out the acscii (HEX) code for this byte. How do I do that? Do I need to save the file to the server?

(The untimate goal is to encrypt the file and send th eencrypted file back to the user. So I want (1) upload file (2) read each individual byte (2) perform encryption on byte level (3) save file and send it back to the user)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妖妓 2024-11-07 14:53:49

多种实现,我将给你一个可重用的实现,使用 fseek

<?php
function getByte($f,$n){
    fseek($f,$n);
    return fread($f,1);
}

$f=fopen('your_file_path');
echo getByte($f,10);
?>

我强烈建议在使用任何 $_FILES 之前使用 move_uploaded_file

PS 如果您不小心,这可能会变得非常非常昂贵,我给您的建议是读取更大的块,然后对它们执行操作。

Multiple implementations, I'm gonna give you a reusable one, using fseek.

<?php
function getByte($f,$n){
    fseek($f,$n);
    return fread($f,1);
}

$f=fopen('your_file_path');
echo getByte($f,10);
?>

I strongly suggest using move_uploaded_file before using anything $_FILES.

P.S. This can get very very expensive if you're not careful, my suggestion to you, would be to read bigger chunks and then perform operations on them.

傲影 2024-11-07 14:53:49

非常感谢您的“最终目标”——它将帮助我们确保您不会无意中做出愚蠢的事情。

加密是一个非常复杂的话题。您不应该编写自己的加密例程。使用完整的预构建解决方案。

Mcrypt 是 PHP 最好的加密扩展。 Mcrypt 支持多种常见密码,包括 AES(“Rijndael”)。 加密非常简单 -- 使用 “CBC”模式


维吉尼亚密码是一个有 460 多年历史的替代密码,基于一个简单的查找表。

这解释了整个一次一个字符的事情。

虽然您可以一次读取整个文件一个字节,但您可能会发现使用字符串更方便。您可以将整个文件读入变量(通过 file_get_contents 或其他方式),然后使用 substr方括号子字符串语法 一次读取一个字节的字符串。请记住,PHP 字符串只是简单的字节列表。

鉴于 $data 是文件中的完整数据,您可以这样处理每个字节:

$length = strlen($data);
for($i = 0; $i < $length; $i++) {
    $byte = $data[$i];
// Or
    $byte = substr($data, $i, 1);
// Process the byte here.
}

这仅适用于小文件(小于几兆字节),因为它需要将整个文件加载到内存中。

我想打印出该字节的 acscii (HEX) 代码

您可以使用 ord,然后使用 dechex

$hex_value = dechex(ord($byte));

替换后,您可以反转它 - 使用 hexdec 从十六进制转换为十进制,然后chr 从十进制转换为字节:

$converted_byte = chr(hexdec($ciphered_hex));

Thank you very much for the "ultimate goal" -- it will help us make sure that you don't do something silly by accident.

Encryption is a very complex topic. You should not write your own encryption routine. Use a complete, pre-built solution.

Mcrypt is PHP's best encryption extension. Mcrypt supports multiple common ciphers including AES ("Rijndael"). Encryption is pretty darn easy -- use "CBC" mode for your file.


The Vigenère cipher is a 460-ish year old substitution cipher based on a simple lookup table.

This explains the whole one-character-at-a-time thing.

While you can read the whole file one byte at a time, you might find it more convenient to work with a string. You can read the entire file into a variable (through file_get_contents or whatever), and then use substr or the square bracket substring syntax to read the string one byte at a time. Remember, PHP strings are just simple lists of bytes.

Given $data is the complete data in the file, you can process every byte thusly:

$length = strlen($data);
for($i = 0; $i < $length; $i++) {
    $byte = $data[$i];
// Or
    $byte = substr($data, $i, 1);
// Process the byte here.
}

This is only practical for small files (smaller than a few megabytes), as it requires loading the whole file into memory.

I want to print out the acscii (HEX) code for this byte

You can get the decimal value using ord, then convert that to a hexadecimal string using dechex:

$hex_value = dechex(ord($byte));

Post-substitution, you can reverse it -- use hexdec to go from hex to decimal, then chr to go from decimal to byte:

$converted_byte = chr(hexdec($ciphered_hex));
怕倦 2024-11-07 14:53:49

在 PHP 中,字符串基本上是字节数组,因此您可以使用 fread() 函数将一些字符读入字符串,然后使用数组表示法访问各个字节。例如。

$content = fread($_FILES["file"],10);
echo $content;
echo "byte10 is: ".$content[9]; // 9 because offset starts from 0

In PHP, a string is basically an array of bytes, so you can use the fread() function to read some characters in to a string, and then access individual bytes using array notation. Eg.

$content = fread($_FILES["file"],10);
echo $content;
echo "byte10 is: ".$content[9]; // 9 because offset starts from 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文