Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将文件上载转换为二进制文件,发布到服务器并保存文件_Php_Html_Image_File Upload_Base64 - Fatal编程技术网

使用PHP将文件上载转换为二进制文件,发布到服务器并保存文件

使用PHP将文件上载转换为二进制文件,发布到服务器并保存文件,php,html,image,file-upload,base64,Php,Html,Image,File Upload,Base64,我正在使用一个无法将文件发布到服务器的应用程序。因此,我选择将其作为字符串发送到服务器,并使用PHP将其重新制作为文件。下面是我用来将图像转换为字符串的javascript代码 var file = document.getElementById("fileForUpload").files[0]; if (file) { var reader = new FileReader(); reader.readAsText(file); reader.onload = fun

我正在使用一个无法将文件发布到服务器的应用程序。因此,我选择将其作为字符串发送到服务器,并使用PHP将其重新制作为文件。下面是我用来将图像转换为字符串的javascript代码

var file = document.getElementById("fileForUpload").files[0];
if (file) {
    var reader = new FileReader();
    reader.readAsText(file);
    reader.onload = function (evt) {
        document.getElementById("fileContents").value = utf8_to_b64(evt.target.result);
    }
    reader.onerror = function (evt) {
        document.getElementById("fileContents").value = "error reading file";
    }
}

function utf8_to_b64(str) {
    return window.btoa(unescape(encodeURIComponent(str)));
}
在服务器端,我正在这样做

header("Content-type: image/png");
$data = preg_replace('/\s+/', '', $data);
echo base64_decode($data);
exit;
但它说,图像无法显示,因为它包含错误

你能告诉我我做错了什么吗?我正在向服务器正确接收Base64编码字符串

编辑

请注意,我试图通过HTML表单发布字符串。

简单:

$imagedata = file_get_contents("/path/to/image.jpg");
             // alternatively specify an URL, if PHP settings allow
$base64 = base64_encode($imagedata);
bear in mind that this will enlarge the data by 33%, and you'll have problems with files whose size exceed your memory_limit.

这是在解决完全不同的问题。请把问题再读一遍。