Search code examples
phpmacosfile-put-contents

file_put_contents on OS X El Capitan


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!


Solution

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

    sudo chmod -R 777 *target_folder*
    

    This should resolve the issue.