我对 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)
发布评论
评论(3)
多种实现,我将给你一个可重用的实现,使用 fseek 。
我强烈建议在使用任何 $_FILES 之前使用 move_uploaded_file 。
PS 如果您不小心,这可能会变得非常非常昂贵,我给您的建议是读取更大的块,然后对它们执行操作。
Multiple implementations, I'm gonna give you a reusable one, using fseek.
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.
非常感谢您的“最终目标”——它将帮助我们确保您不会无意中做出愚蠢的事情。
加密是一个非常复杂的话题。您不应该编写自己的加密例程。使用完整的预构建解决方案。
Mcrypt 是 PHP 最好的加密扩展。 Mcrypt 支持多种常见密码,包括 AES(“Rijndael”)。 加密非常简单 -- 使用 “CBC”模式。
维吉尼亚密码是一个有 460 多年历史的替代密码,基于一个简单的查找表。
这解释了整个一次一个字符的事情。
虽然您可以一次读取整个文件一个字节,但您可能会发现使用字符串更方便。您可以将整个文件读入变量(通过
file_get_contents
或其他方式),然后使用substr
或 方括号子字符串语法 一次读取一个字节的字符串。请记住,PHP 字符串只是简单的字节列表。鉴于
$data
是文件中的完整数据,您可以这样处理每个字节:这仅适用于小文件(小于几兆字节),因为它需要将整个文件加载到内存中。
您可以使用
ord
,然后使用dechex
:替换后,您可以反转它 - 使用
hexdec
从十六进制转换为十进制,然后chr
从十进制转换为字节: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 usesubstr
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:This is only practical for small files (smaller than a few megabytes), as it requires loading the whole file into memory.
You can get the decimal value using
ord
, then convert that to a hexadecimal string usingdechex
:Post-substitution, you can reverse it -- use
hexdec
to go from hex to decimal, thenchr
to go from decimal to byte:在 PHP 中,字符串基本上是字节数组,因此您可以使用 fread() 函数将一些字符读入字符串,然后使用数组表示法访问各个字节。例如。
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.