2

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.

nameless
  • 1,483
  • 5
  • 32
  • 78
  • What is `$filename`? – AbraCadaver Oct 07 '15 at 20:03
  • `$filename = "../userImgs/img1" ` – nameless Oct 07 '15 at 20:05
  • 3
    `error_reporting(E_ALL); ini_set('display_errors', '1');` – AbraCadaver Oct 07 '15 at 20:06
  • @AbraCadaver you mean, just put that code under my `file_put_contents();`? If yes, then this doesn't display any errors either. If not, where else should I put it? – nameless Oct 07 '15 at 20:08
  • Warning: file_put_contents(../userImgs/img1pg): failed to open stream: Permission denied in /var/www/html/test.php on line 37... but what do I have to change then? **Edit** Now, with 777 all it works, but this is very unsafe as I know....Which permissions should I give but have it still working? – nameless Oct 07 '15 at 20:12
  • 1
    `/var/www/html/test.php` is this the correct path? and you double checked if `www-data` (the apache user) can write there? – mloureiro Oct 07 '15 at 20:14
  • @XicoXperto that seems to be the problem, that the apache user can't write there, is there anyway to just allow this user writing, and have all others just reading? – nameless Oct 07 '15 at 20:15
  • Normally you wouldn't had the code there, but you can always do a `$ sudo chgrp www-data /var/www/html/` (still I advise you to make a folder for that kind of things and not the root of the project) – mloureiro Oct 07 '15 at 20:21
  • another topic is that regex doesn't look well formated, ill make a full response – mloureiro Oct 07 '15 at 20:24
  • Okay, but this doesn't change the privilieges of the user, does it? – nameless Oct 07 '15 at 20:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91659/discussion-between-user3375021-and-xicoxperto). – nameless Oct 07 '15 at 20:25
  • @user3375021 no, there you just change the group, take a look at my response – mloureiro Oct 07 '15 at 20:31

1 Answers1

4

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

mloureiro
  • 957
  • 1
  • 12
  • 34