file_put_contents not working

杀马特。学长 韩版系。学妹 提交于 2020-01-15 03:55:12

问题


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:

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).

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?

Thanks.


回答1:


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

Regex

preg_replace('#^data:image/\w+;base64,#i', '', $data)

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

Still that slash after image will give you some problems in some versions, so escape it with a backslash

/^data:image\/\w+;base64,/i

I think this will do :)



来源:https://stackoverflow.com/questions/33001363/file-put-contents-not-working

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