0

i am new to Mac OS X. Currently, i want to place a localhost on my brother OS X and host php files for him. I made a php that can take a picture and save it directly to the folder. The code is working on my windows 7 PC and i can use it normally. However, it is not working in the OS X as it always failed to store the photo in the folder locally. My code to save the picture is:

<?php
require('includes/connect.php'); // Connect to database

$rmid = $_GET['id']; // Get product ID

// Fetch photo only if product id is not empty
if (!empty($rmid)) {
    $rawData = $_POST['imgBase64'];
    echo "<img src='".$rawData."' />"; // Show photo

    list($type, $rawData) = explode(';', $rawData);
    list(, $rawData)      = explode(',', $rawData);
    $unencoded = base64_decode($rawData);

    $filename = date('dmYHi').'_'.rand(1111,9999).'_'.$rmid.'.jpg'; // Set a filename
    file_put_contents("/foto/$filename", base64_decode($rawData)); // Save photo to folder

    // Update database with the new filename
    mysqli_query($conn,"INSERT INTO foto(rmid,namafoto) VALUES ('$rmid','".$filename."')"); 
}  else {
    die('Rekam Medis ID is missing!');
}

I have already created the 'foto' folder inside the php folder. And i have given whole Read & Write permission to the folder. I am not sure why the photo is not stored inside the foto folder. But the query is working to store the filename to the database.

Added information: i am using XAMPP with PHP 7.0

Thanks for your help!

efraim
  • 87
  • 1
  • 2
  • 9
  • Hello efraim, do you have read and write permissions on the target folder? – Techno May 03 '16 at 12:50
  • @Rob Biermann yes i have already give read & write in the 'foto' folder as i wrote above, but still not working yet.. – efraim May 03 '16 at 12:52
  • If you use terminal, and cd to the location where the folder is located, then perform `ls -l`, you should see the permissions, OS X is a linux, which means the rights work differently then a windows machine. Best practice for test environments would be to perform the command: `sudo chmod -R 777 foldername`. 777 will set all the permissions to read and write(fyi: right click->show info is usually nearly, but not totally the same) for more info about what i mean: https://www.linux.com/learn/understanding-linux-file-permissions – Techno May 03 '16 at 12:58
  • @Rob Biermann which folder should i change for the permission? the whole folder of XAMPP, the project folder, or only the 'foto' folder where the file stored? – efraim May 03 '16 at 14:53
  • Just the target folder for your pictures. – Techno May 03 '16 at 15:08
  • 1
    @RobBiermann Thanks! It's working now, thanks for the big help. – efraim May 03 '16 at 16:25
  • If you could upvote my answer, that would be nice! :) – Techno May 03 '16 at 16:37

1 Answers1

2

Please try to change the permissions of your target folder like this:

sudo chmod -R 777 *target_folder*

This should resolve the issue.

Techno
  • 1,668
  • 1
  • 9
  • 19
  • Though 0777 permission shouldn't be a solution, get the user PHP/Apache runs on and check the permissions on the target folder, e.g 0755 should be sufficient for the folder if all is met. – dbf May 03 '16 at 16:45