无法打开流:权限被拒绝[上传照片]

时间:2015-10-14 04:08:47

标签: php apache xampp

目前使用macbook + apache + phpmyadmin + php + xampp。 (在localhost上运行)

我正在尝试通过php将照片上传到其中一个文件夹,但我收到此警告。

通过macbook文件设置是否有解决此问题的方法?

  

警告:file_put_contents(image / img_post720151014060113101414.jpg):无法打开流:第32行/Applications/XAMPP/xamppfiles/htdocs/CS2102/signup.php中的权限被拒绝   无法执行查询。

代码:

  

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 个答案:

答案 0 :(得分:1)

您需要指定一个可写目录。

在这种情况下:

// 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/

答案 1 :(得分:1)

我通过将要上传的文件夹的设置更改为每个人读取+写入来解决问题。我确定这不是最安全的方式。但我正在进行localhost测试。所以我认为这足以解决问题所以我可以继续下一部分。

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

答案 2 :(得分:1)

终端转到您的应用程序目录并找到&#34;图像&#34; 。目录

然后在图像目录上尝试此命令(您需要位于拥有图像目录的目录中):

chown -R _www image
chmod 755 image

_www是mac os上的默认apache用户