PHP的base 64_decode不会将base 64字符串转换为实际可用的图像文件 [英] PHPs base 64_decode is not converting base 64 string to a real workable image file

查看:111
本文介绍了PHP的base 64_decode不会将base 64字符串转换为实际可用的图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我成功找到了一种方法,该方法声称可以将文件输入文件转换为JavaScript中的base 64字符串,因此我成功发送了base 64

Hello guys I successfully found a method that claims to make a file input file into a base 64 string in JavaScript so I successfully sent that base 64

通过AJAX通过JSON进行字符串转换,并且基数为64的编码字符串看起来是通过JSON方法"photo"发送的:"data:image/jpeg; base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wB等..."

string by JSON via AJAX and the base 64 encoded string looks like this sent in the JSON method "photo":"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wB etc...."

因此,当基本64字符串到达​​PHP文件时. PHP发挥了神奇的作用,并成功地将文件存储在我要放置文件的目标文件夹中,因此当我在该文件夹中查找文件时,

So when the base 64 string arrives in the PHP file. The PHP does it's magic and successfully store a file in the targeted folder where I want the file to be at so when I look in that folder there is a file but

当我尝试打开照片文件进行查看时,照片查看器应用程序会显示诸如image.jpg之类的字样,看来我们不支持此文件格式,而在其他照片查看器应用程序中它将显示某些内容

when I try to open the photo file to view it the photo viewer app says something like, image.jpg it looks like we don't support this file format and in other photo viewer apps it will say something

与此类似,所以我做错了什么?

similar to that so what have I done wrong?

这是我的代码

index.php

<style>

#photo-input{
display: block;
margin-bottom: 50px;
}

</style>

<script>

document.addEventListener('DOMContentLoaded',function(){

document.querySelector('#submit').addEventListener('click',function(){

var photo_input= document.querySelector('#photo-input').files[0];

//Convert #photo-input content into a base 64 string
var reader = new FileReader();
reader.readAsDataURL(photo_input);

reader.onload = function (){
var photo_input_result= reader.result;
sendUploadInfo(photo_input_result);
}
//

});

function sendUploadInfo(photo_input_result){

var photo= photo_input_result;

//<JSON data>

var upload_info = {
    first_name: "John",
    last_name: "Smith",
    photo: photo
};

//</JSON data>

var upload_info_json_object= 'upload_info_json_object='+JSON.stringify(upload_info); 

//<AJAX>
var xhr= new XMLHttpRequest();
xhr.onreadystatechange= function(){

if(xhr.readyState == 4){

document.querySelector('#output').innerHTML= xhr.responseText;

}
}

xhr.open('POST','x');
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(upload_info_json_object);
//</AJAX>
}

});

</script>

<input type='file' id='photo-input'>

<button id='submit'>Send JSON data</button>

<div id='output'></div>

x.php

<?php

$upload_info_json_object = json_decode($_POST['upload_info_json_object']);

$first_name= $upload_info_json_object->first_name;
$last_name= $upload_info_json_object->last_name;


//Photo upload section

$photo= $upload_info_json_object->photo;

base64_decode($photo); 

$path= $_SERVER['DOCUMENT_ROOT'].'/send-json-data-by-ajax/object-based/with-file/2/images/image.jpg';

file_put_contents($path, $photo);

//

?>

<h1><?php echo $first_name.' '.$last_name.' just uploaded a photo.'; ?></h1>

推荐答案

我认为您应该完全放弃当前的方法,并用以下方法代替:

I think you should abandon your current methodology entirely and replace it with this:

<form action="x.php" method="post" enctype="multipart/form-data">
  <input type="hidden" name="first_name" value="John" />
  <input type="hidden" name="last_name" value="Smith" />
  <input type="file" name="photo" accept="image/*" />
  <input type="submit" value="Upload Photo" />
</form>

然后,在服务器端代码上,如下检查结果:

Then, on your server-side code, inspect the results like so:

<?php
  print_r($_POST); // All of your post fields
  print_r($_FILES); // All of the file uploads

在您访问 move_uploaded_file() 时,知道要放在哪里.

Look into move_uploaded_file() when you know where you want to put it.

有很多好处:

  • 真正的二进制文件上传,没有浪费和33%的base-64开销,也没有两端的CPU来处理
  • 仅接受图像(accept="image/*")的图像输入
  • 表单可以由屏幕阅读器和其他浏览器控件提交,而不仅仅是某些没有上下文的按钮
  • 完全不需要JavaScript!
  • 标准上传,以减少服务器上的内存使用量.
  • 不需要对潜在的巨大JSON blob进行编码/解码.
  • A real, binary file upload without the waste and overhead of 33% base-64, nor the CPU on each side to deal with it
  • An image input that accepts just images (accept="image/*")
  • Form can be submitted by screen readers and other browser controls, rather than just some button without context
  • No need for JavaScript at all!
  • Standard streaming uploads, for less memory usage on your server.
  • No need for the encoding/decoding of a potentially huge JSON blob.

这篇关于PHP的base 64_decode不会将base 64字符串转换为实际可用的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆