0

I am currently working on moving my site from an old Godaddy dedicated server to an AWS Ubuntu instance. So this is a fresh install. Everything, as is works fine on my old server so I am suspecting this, is a php.ini issue but not sure. Now I can't get for the life of me file_put_contents to work on my new box.

Just to test I made this, directory and here directories 777 but still no luck. I tried to output errors, I get none. Checked apache error log...nothing. File uploads in on in php.ini. What am I missing?

$file = '../../../this/directory/here/123.jpg';
$data = base64_decode($base64);

if (file_put_contents($file, $data)) {

}

Update

I have discovered that if I change my url to

$file = '../../../this/directory/here/123.txt';

instead of .jpg it works and uploads a txt file???

Cesar Bielich
  • 4,754
  • 9
  • 39
  • 81
  • If you suspect the problem is with `file_put_contents()` then take uploads, base64 conversion and anything else out of the equation. Just try a simple `file_put_contents('foo.txt', 'bar');`. – Mitya Dec 12 '19 at 17:51
  • already tried something like that and it failed. Just tried your example just in case and it fails – Cesar Bielich Dec 12 '19 at 17:52
  • @CesarBielich What is the error message you get? You might need to enable error logging, see https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php – Progman Dec 12 '19 at 17:54
  • Are you absolutely sure the path to here is the same on both servers – RiggsFolly Dec 12 '19 at 17:55
  • Sounds like a perms issue. If you're sure the target dir is set high enough in the CHMOD, I'd check which user PHP is executing as, via something like `echo \`whoami\``, and ensure it has write perms. – Mitya Dec 12 '19 at 17:55
  • `var_dump(realpath($file));` – AbraCadaver Dec 12 '19 at 17:56
  • 1
    Make sure that the directory at `../../../this/directory/here/` exists. Anything using that many upper-level identifies makes things suspect as far as current directory vs expected directory. Try using a path relative to `$_SERVER['DOCUMENT_ROOT']` instead of using a fully relative path. – aynber Dec 12 '19 at 17:58
  • @Utkanos so weird. If I put in `.txt` instead of `.jpg` it works – Cesar Bielich Dec 12 '19 at 18:16
  • run ```var_dump(shell_exec("namei -l ".escapeshellarg($file)." 2>&1"));```, what do you get? – hanshenrik Dec 12 '19 at 18:25
  • Try print `realpath($file)` to make sure you're writing to the correct directory. – symcbean Dec 12 '19 at 18:27
  • @symcbean it is the correct dir. Like I mentioned if I change it from `.jpg` to `.txt` it works and uploads. It's something with `jpg` – Cesar Bielich Dec 12 '19 at 18:29
  • @hanshenrik https://www.dropbox.com/s/4kpj59pr4pcfhdc/screenshot.jpg?dl=0 – Cesar Bielich Dec 12 '19 at 18:32
  • Looks like the jpg is only writeable by the owner. If the webserver process is not the owner, it won't be able to modify/overwrite it. – aynber Dec 12 '19 at 18:36
  • @aynber you got it! I did not realize that the user group permissions had changed from the transfer to a different username. So since the file existed it could not overwrite the file so it was failing. Make this an answer and I will hook you up. – Cesar Bielich Dec 12 '19 at 20:23

0 Answers0