How to save image server side when send from javascript

随声附和 提交于 2019-12-06 11:37:52

Assuming you have managed to get the raw file from the PHP input, it's likely going to be base64 encoded. A simple example would be to do this:

<?php
//decode file
$image = base64_decode($file);

// write it
$filename = "myfile.png";
$fh = fopen($filename, 'w') or die("can't open file");
fwrite($fh, $image);
fclose($fh);

Edit See comments, the file wasn't encoded as a result of the request.

Jason

move_uploaded_file is not what you want. If you had sent the file with a normal POST request (rather than through Ajax), that's when you'd use move_uploaded_file.

$file contains the raw binary data for the image. All you have to do is write that data into a file (taking note to properly handle linebreaks) and you're good to go. Start with fopen and see how far you get.

I'm assuming that $file has been successfully populated with the binary data and that you've tested this. If not, then you've got another problem on the javascript side.

Edit: you may also find this helpful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!