file_put_contents无法正常工作 [英] file_put_contents not working
问题描述
我尝试使用以下代码通过file_put_contents(转换后的base64字符串为.jpg)将某些内容上传到我的ubuntu服务器:
I try to upload something to my ubuntu server by file_put_contents (a converted base64-string as .jpg) with the following code :
file_put_contents($filename, base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data)));
是的,所有参数都正确,我仔细检查了一下.我想知道为什么它不起作用:
And yes, all parameters are right, I double checked them. And I'm wondering why it is not working:
顺便说一句:我尝试将其上传到比该文件夹更高一级的文件夹,该文件夹可以通过url访问(但即使直接将其放在Internet文件夹中,也无法使用).
By the way: I try to upload it to a folder, one level higher then the folder, that is reachable by the url (but even when putting it directly in internet folder, it doesn't work either).
我考虑过错误的权限,但是即使将权限更改为777(我知道这是非常不安全的),它也不起作用.
I thought about bad permissions, but even when changing permissions to 777 (which I know is very unsafe), it doesn't work.
我在控制台上也没有任何错误.
I also don't get any errors in console.
有人知道为什么它不起作用吗?
Does anybody have an idea why this is not working?
谢谢.
推荐答案
文件夹权限
关于要保存的文件夹(/var/www/html
)的权限,您可以更改文件夹的组并更改权限,以便该组可以写为:
Folder Permissions
About the permissions for the folder where you're trying to save (/var/www/html
) you can change the group of the folder and change the permissions so that the group can write as:
$ sudo chgrp www-data /var/www/html/
$ sudo chmod 775 /var/www/html
正则表达式
preg_replace('#^data:image/\w+;base64,#i', '', $data)
AFAIK模式必须带有开始和结束斜杠,我认为您已经将/
与#
混淆了,所以看起来像
AFAIK the pattern must have the start and end slashes, I think you've confused the /
with #
, so it would look like
/^data:image/\w+;base64,/i
在 image 之后仍然使用斜杠在某些版本中会给您带来一些问题,因此请使用反斜杠
Still that slash after image will give you some problems in some versions, so escape it with a backslash
/^data:image\/\w+;base64,/i
我认为这会做的:)
这篇关于file_put_contents无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!