hex2bin ($ var) ошибка: входная строка должна быть шестнадцатеричной строкой

Здравствуйте, это небольшой код для преобразования строки в шестнадцатеричное и обратное.

При этом у нас нет строки ошибки в шестнадцатеричном виде:

<?php
if( isset($_POST['upload']) ) // si formulaire soumis
{
$content_dir = 'tmpcipher/'; // dossier où sera déplacé le fichier
$tmp_file = $_FILES['fichier']['tmp_name'];
if( !is_uploaded_file($tmp_file) )
{

}
// on vérifie maintenant l'extension
$type_file = $_FILES['fichier']['type'];
// on copie le fichier dans le dossier de destination
$name_file = $_FILES['fichier']['name'];

if( !move_uploaded_file($tmp_file, $content_dir . $name_file) )
{

}
$name_file2 = $_FILES['fichier']['name'];
$hex = bin2hex(file_get_contents($content_dir . $name_file2));
unlink($content_dir . $name_file2);
$myfile2 = fopen($content_dir . $name_file2 . "2" , "w");
$myfileurl = $content_dir . $name_file2 . "2";
fwrite($myfile2, $str);
fclose($myfile2);
$myfile3 = $content_dir . $name_file2 . "2";
if (file_exists($myfile3)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($myfile3));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($myfile3));
readfile($myfile3);
}
}
?>

Обратно, с этим у нас есть ошибка:

<?php
if( isset($_POST['upload']) ) // si formulaire soumis
{
$content_dir = 'tmpcipher/'; // dossier où sera déplacé le fichier
$tmp_file = $_FILES['fichier']['tmp_name'];
if( !is_uploaded_file($tmp_file) )
{

}
// on vérifie maintenant l'extension
$type_file = $_FILES['fichier']['type'];
// on copie le fichier dans le dossier de destination
$name_file = $_FILES['fichier']['name'];

if( !move_uploaded_file($tmp_file, $content_dir . $name_file) )
{

}
$str0 = file_get_contents($content_dir . $name_file); //HEXADECIMAL CONTENT
$hexstr1 = hex2bin("73616c7574"); //WORK
$hexstr2 = hex2bin($str0); //ERROR
$name_file = substr($name_file,0,-1);
$myfile2 = fopen($content_dir . $name_file, "w");
fwrite($myfile2, $str0);
fclose($myfile2);
$filectohex = file_get_contents($content_dir . $name_file);
$myfile3 = $content_dir . $name_file;
if (file_exists($myfile3)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($myfile3));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($myfile3));
readfile($myfile3);
}
}
?>

php_error_log: [25-Jan-2015 00:34:25 Европа / Берлин] PHP Предупреждение: hex2bin (): входная строка должна быть шестнадцатеричной строкой в ​​C: \ xampp \ htdocs \ dec.php в строке 47

Когда я беру содержимое строки $ str0 и использую hex2bin, это работает.
Я не знаю, считает ли PHP, что моя переменная целочисленная, пожалуйста, дайте мне решение 🙂

Спасибо

[Решено]: Напишите переменную, которую вы хотите преобразовать в ascii (шестнадцатеричное содержимое, а затем преобразуйте в ascii содержимое этого файла (должно быть шестнадцатеричным).

$hex2bin($str0); --->
$myfile2 = fopen($content_dir . $name_file, "w");
fwrite($myfile2, $str0);
fclose($myfile2);
$myfile2path = $content_dir . $name_file;
$bin = hex2bin(file_get_contents($myfile2path));

0

Решение

Ты пишешь $str в файл, а не $hex, Файл пуст. Вы читаете пустую строку, и hex2bin('') выдает ошибку. Именно в этом куске кода:

// below you define $hex
$hex = bin2hex(file_get_contents($content_dir . $name_file2));
unlink($content_dir . $name_file2);
$myfile2 = fopen($content_dir . $name_file2 . "2" , "w");
$myfileurl = $content_dir . $name_file2 . "2";
// but here you write $str
fwrite($myfile2, $str);

Вы могли бы найти это, просто повторяя содержание $str0:

echo «Содержимое str0 равно [$ str0] \ n»

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector