Home > Article > Backend Development > How to hide the real address of pictures in php
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);] .
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('Content-type: text/html; charset=utf-8'); if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') { if (!isset($_FILES['file'])) exit('请上传图片'); $file = $_FILES['file']; $dataType = array('png', 'jpg', 'jpeg', 'gif'); $ext = strtolower(substr(strrchr($file['name'], '.'), 1)); if (!isset($file['tmp_name']) || $file['error'] > 0) exit('上传失败'); if (!in_array($ext, $dataType)) exit('图片格式错误'); $file = file_get_contents($file['tmp_name']); $data = base64_encode($file); echo 'data:image/'.$ext.';base64,'.$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!