쿠키에서 $ dir으로 값 ($ login)을 전달하고 싶습니다. file.phpPHP 클래스의 쿠키에서 값을 가져 옵니까?
<?php
class File {
public $filename;
public $login;
public $dir;
public function __construct() {
$this->login = $_COOKIE['login'];
this->dir = "userFiles/" . $this->login . "/";
$createDir="./userFiles/".$login;
if (!is_dir($createDir)){
mkdir($createDir,0777,true);
}
$action = isset($_POST['action']) ? $_POST['action'] : false;
$this->filename = isset($_POST['filename']) ? $_POST['filename'] : false;
}
private function save() {
$content = isset($_POST['content']) ? $_POST['content'] : '';
file_put_contents($this->dir.$this->filename, urldecode($content));
}
}
$file = new File();
?>
이 전체 코드입니다. filename과 action 변수는 index.php 페이지에서 ajax에 의해 전달됩니다.
의 index.php이 당신을 위해 작동합니다
<?php
if (! isset($_COOKIE['login'])) {
echo "Need to log in first";
header("refresh:2;url=login.php");
exit;
}
?>
<!doctype html>
<html>
<head>
<title>Editor</title>
</head>
<body>
<textarea id="code" name="code">code goes here</textarea>
<br/><button id="save">save</button>
<input type="text" id="filename" value="test.txt"><br>
<script src="JavaScript/jquery.js"></script>
<script>
var url = 'file.php';
$("#save").click(function() {
$.ajax({
url : url,
type: 'post',
xhrFields: {
withCredentials: true
},
data : {
filename : $("#filename").val(),
action : 'save',
content : encodeURIComponent($('#code').val())
}
});
});
</script>
</body>
</html>
당신은 두 번'$의 login'를 할당하려고하는 이유
이그래서 전체 코드의 모양은? 다음과 같이 login을 선언하면됩니다 :'public $ login' 또한 생성자에서 dir을 지정하십시오! – Rizier123
표 : $ dir을 (를) 지정하는 곳에'$'기호를 잊어 버렸습니다! – Rizier123
@ Rizier123 $ 기호를 추가했습니다. userFiles 폴더를 만들지 만 $ login 폴더를 안에 만들고 파일을 저장하지 않습니다. – zeee9