donglu9445 2013-07-23 06:26
浏览 107
已采纳

我可以在$ _FILES数组中添加base64图像吗?

Is there a way to add a base64 image string to the $_FILES array (the array that you get when you upload a file)? Basically, I want to fake a file upload from a base64 string. The base64 string comes from an email attachment. I don’t wish to use file_get_contents(), because I want to save the image in a database as a binary image. I don’t need to upload it, as such, but I do need it be a part of the $_FILES array.

  • 写回答

1条回答 默认 最新

  • douxi1738 2013-07-23 06:42
    关注

    Uploading is a way of sending the contents of a file from a client (usually a web browser) to a server. It sounds like you've already got the data you need on the server (in PHP), so you don't need to upload it.

    1. You can use base64_decode() to convert it from base64 to the straight file contents.

    2. If you need it as a file on your server, you can use something like file_put_contents() to save it as a new file.

    3. If you're trying to do something else with the uploaded file in the $_FILE array, please provide more details. Whatever it is, you don't need to actually upload the file.


    The $_FILES array doesn't actually contain the contents of the file - it contains a filename where the contents have been saved.

    If you must use the $_FILES array (in order to re-use an existing class, for example), you'll need to use file_put_contents() to save a new temporary file, and then manually add an entry the the $_FILES array pointing to that temporary file (Just like modifying any other array). Don't forget to delete your temporary file when you're done - this is normally handled automatically by PHP, but if you create the file manually, you'll have to delete it manually too.

    I'd highly recommend refactoring or providing an alternate interface to your class instead though. You shouldn't have to pretend that the file was uploaded just to save it to the database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,