1
votes

Currently using macbook + apache + phpmyadmin + php + xampp. (running on localhost)

I'm trying to upload a photo into one of the folders through php but i am receiving this warning.

Is there any solution to this issue through macbook file setting?

Warning: file_put_contents(image/img_post720151014060113101414.jpg): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/CS2102/signup.php on line 32 couldn't execute query.

Codes:

include("db.php");

$userEmail = $_POST["userEmail"];
$firstName= $_POST["firstName"];
$lastName = $_POST["lastName"];
$userPassword = $_POST["userPassword"];
$nationality = $_POST["nationality"];
$birthday = $_POST["birthday"];
$gender = $_POST["gender"];

// prepare the image for insertion
$imgData =addslashes (file_get_contents($_FILES['test']['tmp_name']));
$bio = $_POST["bio"];

$filename = "img_post" . rand(1, 9) . date("YmdHis") . rand(25, 125) . rand(256, 850);
$allowed  = array(
    "image/jpeg",
    "image/pjpeg",
    "image/jpg",
    "image/png",
    "image/JPEG",
    "image/bmp",
    "image/PNG"
);

$result= UploadFile("test", $allowed, 10000000);
            if ($result[0] == 0) {
                // Put Photo uploaded into sever folder
                file_put_contents("image/" . $filename . ".jpg", $result[2]);
                $date  = (String) date("Y-M-d-H:i:s");

                // Query String
                $query = "INSERT INTO `user` (`userEmail`,`password`, `firstName`,  `lastName`,  `nationality`,`birthday`,`gender`,`picSrc`,`bio`) VALUES ('$userEmail','$password','$firstName','$lastName','$nationality','$birthday','$gender', '{$imgData}', '$bio')";  
                // Execute Query
                mysqli_query($mysqli,$query) or die("couldn't execute query.");
                echo '<script language="javascript">';
                echo 'alert("Photo Uploaded");';
                echo 'window.location.href="loginreg.php";';
            } else {
                if ($result[0] == "-1")
                    echo "wrong";
                if ($result[0] == "-2")
                    echo "wrong file type";
                if ($result[0] == "-3")
                    echo "MAximum length exceeded";
                if ($result[0] > 0)
                    echo "Error due to $result";
                echo $result[0];
                echo $result;
                echo "<br/>File Upload Error!";

            }


function UploadFile($name, $filetype, $maxlen)
{
    if (!isset($_FILES[$name]['name']))
        return array(
            -1,
            NULL,
            NULL
        );
    if (!isset($_FILES[$name]['type'], $filetype))
        return array(
            -2,
            NULL,
            NULL
        );
    if ($_FILES[$name]['size'] > $maxlen)
        return array(
            -3,
            NULL,
            NULL
        );
    if ($_FILES[$name]['error'] > 0)
        return array(
            $_FILES[$name]['error'],
            NULL,
            NULL
        );

    $temp = file_get_contents($_FILES[$name]['tmp_name']);
    return array(
        0,
        $_FILES[$name]['type'],
        $temp
    );
}


?>
3
It might help if you include line 32 (and maybe more) of signup.php.Bennett Brown
please add your code.Niranjan N Raju

3 Answers

1
votes

You need to specify a writable directory.

In this case:

// set BASE_DIR constant for later convenience
define('BASE_DIR', dirname(__FILE__).'/');

// specify filedir/filename you want to write to
$file = BASE_DIR.'image/'.$filename.'.jpg';

// write content to file
file_put_contents($file, $result[2]);

If that doesn't work, you'll have to go to the command-line and run:

chmod 0777 <path-to-web-folder>/image/
1
votes

I kind of solved the issue by changing the setting of the folder that i want to upload to to everyone read + write. I'm sure it's not the safest way of doing so. But i'm doing on localhost testing. So i guess that's good enough to solve the issue so I can move on to the next part.

But nevertheless, Thanks everyone for your help ^^ =).

1
votes

With the terminal go to your application directory and find the "image" directory.

Then try this command on the image directory (you need to be in the directory which owns image directory) :

chown -R _www image
chmod 755 image

_www is the default apache user on mac os