打开流失败:权限被拒绝[上传照片]


failed to open stream: Permission denied [upload photo]

目前使用macbook + apache + phpmyadmin + php + xampp。(在本地主机上运行)

我试图通过php上传照片到其中一个文件夹,但我收到这个警告。

有没有办法通过macbook文件设置来解决这个问题?

警告:file_put_contents(image/img_post720151014060113101414.jpg): failed to open stream: Permission denied in/Applications/XAMPP/xamppfiles/htdocs/CS2102/login .php第32行无法执行查询。

代码:

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
    );
}

?>

需要指定一个可写目录

在本例中:

// 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]);

如果这不起作用,您必须转到命令行并运行:

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

我通过改变我想上传到每个人读+写的文件夹的设置来解决这个问题。我敢肯定这不是最安全的方法。但我正在做本地主机测试。所以我想这足以解决这个问题,所以我可以继续下一部分。

但是,谢谢大家的帮助^^ =)。

使用终端进入应用程序目录并找到"image"目录

然后在image目录上尝试这个命令(你需要在拥有image目录的目录下):

chown -R _www image
chmod 755 image

_www是MAC os上默认的apache用户