>  기사  >  백엔드 개발  >  PHP 파일을 base64로 변환하는 방법

PHP 파일을 base64로 변환하는 방법

藏色散人
藏色散人원래의
2021-07-23 10:47:257906검색

PHP 파일을 base64로 변환하는 방법: 먼저 PHP 샘플 파일을 만든 다음 "fileToBase64($file){...}" 함수를 사용하여 파일을 base64로 변환하고 출력합니다.

PHP 파일을 base64로 변환하는 방법

이 기사의 운영 환경: Windows 7 시스템, PHP 버전 7.1, DELL G3 컴퓨터

php 파일을 base64로 변환하는 방법은 무엇입니까?

base64 출력으로 파일 변환

데이터 URI 체계는 RFC2397에 정의되어 있습니다. 그 목적은 일부 작은 데이터를 웹 페이지에 직접 삽입하여 외부 파일에서 로드할 필요가 없도록 하는 것입니다.

장점: http 연결 수를 줄입니다.

단점: 이 형식의 파일은 브라우저에서 캐시되지 않습니다.

파일이 너무 크면 역효과가 나기 때문에 "20KB를 초과하는" 파일을 인코딩하는 것은 권장되지 않습니다.

data: URI의 기본 사용 형식은 다음과 같습니다.

data:[<MIME-type>][;base64|charset=some_charset],<data>

mime-type은 포함된 데이터의 MIME 유형입니다. 예를 들어 png 이미지는 image/png입니다.

다음에 base64가 오면 다음 데이터가 base64로 인코딩된다는 의미입니다.

데이터 URI 체계는 다음 유형을 지원합니다.

data:,文本数据
data:text/plain,文本数据
data:text/html,HTML代码
data:text/html;base64,base64编码的HTML代码
data:text/css,CSS代码
data:text/css;base64,base64编码的CSS代码
data:text/javascript,Javascript代码
data:text/javascript;base64,base64编码的Javascript代码
data:image/gif;base64,base64编码的gif图片数据
data:image/png;base64,base64编码的png图片数据
data:image/jpeg;base64,base64编码的jpeg图片数据
data:image/x-icon;base64,base64编码的icon图片数据

예: 8c77f693ab85be382df9de8219c5a9b5 데이터 URI로 변환한 후

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTg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Ibyzk5gnjBO5fcdQexB70cl9JGaurdz6JjtbZYw1uqkMAVfOcjsahks7aXcsqmGQ90zg/wCNcd4K8VGXQLTTZWCXtshiMcn32VejAHtj+Vb82oSSgh5Hx6YxWfItiEpX3KOp50yRSssbAAsWJ+UdskfjXnt94oNtfqJpv9GkuVHJ6Jg7mH47a7DXXRrHKxu6qwZ+p4ry7W7i2haP7ZaLPEXwwDYIPt+v5VUYpWS2OlW9m29zZk8Z2MNk14b64vCjLGtu0Wxlzk7jnrwDVC88ex2Mtij6bDeQ3GnI8m85dWbOAPT61h6vF4bVCbW4lid1B/dZcDI7g5xWXpWlLczxzi789ExtBXPA6DrWqSSuc3K5SsjtNL11bzTWC2ckNy3yQqjlt5PHPA9a7ez8J3iWcSzeekoXDKh4B9BXE6RHMusWEdqAJxMu3jIGP/rV7HG0GwebeKH7gNXNXdWSXs7L1OpRUHZ6niGpW9slwUtp/NQqBuxjJx6Vkjdaz+YA4BBVwjlCR9RVuBQYdx5bcOfwFTTxoU+7XZujJocttLbaemqabazTLGT5t4r7ngzxhlGCM+p496v6b4516xj2peR3UechLlNxHtnrisizd7dmlhdo3QgqVYj/APWPat7VNLsU1JAlsirJZNOwXgb/ACycgduecDiosQ9Nye2+JmqiYi/0+0uYCW3xopQkHoAeeBXHa1qck8wksYXht0l3rG/z5XPQ+uKqRyO7ruYnAx+FPWRhkBjgqCfrRszRL3SjqOpwXDyS/YrYAk/LsIP5jH8q6XRb0XGh28YRFhgykYKjePbd1I9B2rnbuCKVJGdASsZYHpyK623gigCQxIFjVQQopvVWIpJqTZ0/gbQjquo3N7OzJFbKEUDjczD+i/8AoVenQaFYLAo8rPuTWB4HRU8C6ZKow8u+SRu7MZCMn8AB+Fdraoptk47etZtRcrWFKrNK9z//2Q==" />

php 변환 방법:

/** 文件转base64输出
* @param  String $file 文件路径
* @return String base64 string
*/
function fileToBase64($file){
    $base64_file = &#39;&#39;;
    if(file_exists($file)){
        $mime_type= mime_content_type($file);
        $base64_data = base64_encode(file_get_contents($file));
        $base64_file = &#39;data:&#39;.$mime_type.&#39;;base64,&#39;.$base64_data;
    }
    return $base64_file;
}
 
/** base64转文件输出
* @param  String $base64_data base64数据
* @param  String $file        要保存的文件路径
* @return boolean
*/
function base64ToFile($base64_data, $file){
    if(!$base64_data || !$file){
        return false;
    }
    return file_put_contents($file, base64_decode($base64_data), true);
}

demo:

//file to base64
<img src="<?=fileToBase64(&#39;1.jpg&#39;) ?>" />
 
//base64 to file
$file = "test.jpg";
$data = &#39;/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABLAEsDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk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QkHoAeeBXHa1qck8wksYXht0l3rG/z5XPQ+uKqRyO7ruYnAx+FPWRhkBjgqCfrRszRL3SjqOpwXDyS/YrYAk/LsIP5jH8q6XRb0XGh28YRFhgykYKjePbd1I9B2rnbuCKVJGdASsZYHpyK623gigCQxIFjVQQopvVWIpJqTZ0/gbQjquo3N7OzJFbKEUDjczD+i/8AoVenQaFYLAo8rPuTWB4HRU8C6ZKow8u+SRu7MZCMn8AB+Fdraoptk47etZtRcrWFKrNK9z//2Q==&#39;;
 
if(base64ToFile($data, $file)){
    echo &#39;<img src="&#39;.$file.&#39;" />&#39;;
}

추천 학습: "PHP 비디오 튜토리얼 "

위 내용은 PHP 파일을 base64로 변환하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.