Home  >  Article  >  Backend Development  >  How to hide the real address of pictures in php

How to hide the real address of pictures in php

coldplay.xixi
coldplay.xixiOriginal
2020-10-07 15:21:503000browse

php method to hide the real address of the picture: use base64 encoding to hide the real address of the picture, the code is [$file = file_get_contents($file['tmp_name']);$data = base64_encode($file);] .

How to hide the real address of pictures in php

php method to hide the real address of the picture:

PHP can use base64 encoding to hide the real address of the picture. After hiding, the image address in this format will be displayed data:QUFodHRwOi8vd3d3LmJhaWR1. Websites such as Xiang Moubao currently use this method, and it can improve the loading speed. The specific PHP code can be as follows:

<?php
header(&#39;Content-type: text/html; charset=utf-8&#39;);
if (strtolower($_SERVER[&#39;REQUEST_METHOD&#39;]) == &#39;post&#39;) {
if (!isset($_FILES[&#39;file&#39;])) exit(&#39;请上传图片&#39;);
$file = $_FILES[&#39;file&#39;];
$dataType = array(&#39;png&#39;, &#39;jpg&#39;, &#39;jpeg&#39;, &#39;gif&#39;);
$ext = strtolower(substr(strrchr($file[&#39;name&#39;], &#39;.&#39;), 1));
if (!isset($file[&#39;tmp_name&#39;]) || $file[&#39;error&#39;] > 0) exit(&#39;上传失败&#39;);
if (!in_array($ext, $dataType)) exit(&#39;图片格式错误&#39;);
$file = file_get_contents($file[&#39;tmp_name&#39;]);
$data = base64_encode($file);
echo &#39;data:image/&#39;.$ext.&#39;;base64,&#39;.$data;
} else {
?>
<html>
<head><title>图片base64编码</title></head>
<body>
<form method="post">
<input type="file">
<input type="submit" value="提交">
</form>
</body>
</html>
<?php
}
?>

Related free learning recommendations: php programming (video)

The above is the detailed content of How to hide the real address of pictures in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn