1

我正在开发一个网站,让用户只需保存一个便笺,以便以后访问。然而,我在实际保存笔记时遇到了一些麻烦。我首先将它设置为在每次按键时自动保存,但如果我让用户按下按钮来保存文件会更好。您还将在代码中看到该注释被保存为用户 IP 地址,当用户再次访问该站点时,他将看到相同的注释(如果他再次具有相同的 IP)。

单击保存按钮时我现在得到的错误是:

PHP Warning:  file_put_contents() [<a href='function.file-put-contents'>function.file-put-contents</a>]: Filename cannot be empty in /home/martmart/public_html/index.php on line 41

我的 index.php:

<?php

$note_name = 'note.txt';
$uniqueNotePerIP = true;

if($uniqueNotePerIP){

// Use the user's IP as the name of the note.
// This is useful when you have many people
// using the app simultaneously.

if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
$note_name = 'notes/'.$_SERVER['HTTP_X_FORWARDED_FOR'].'.txt';
}
else{
$note_name = 'notes/'.$_SERVER['REMOTE_ADDR'].'.txt';
}
}


if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
// This is an AJAX request

if(isset($_POST['note'])){
// Write the file to disk
file_put_contents($note_name, $_POST['note']);
echo '{"saved":1}';
}

exit;
}

$note_content = 'Write something here :D';

if(file_exists($note_name) ){
$note_content = htmlspecialchars( file_get_contents($note_name) );
}

function saveNow() {
// Write the file to disk
file_put_contents($note_name, $_GET['note']);
echo '{"saved":1}';
}

?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Marty Testweb</title>
<!-- Our stylesheet -->
<link rel="stylesheet" href="assets/css/styles.css" />
<!-- A custom google handwriting font -->
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Courgette" />
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="assets/audiojs/audio.min.js"></script>
<script>
audiojs.events.ready(function() {
var as = audiojs.createAll();
});
</script>
</head>

<body>

<div id="pad">
<h2>Note</h2>
<textarea id="note"><?php echo $note_content ?></textarea>
</div>

<!-- Initialise scripts. -->

<script>
function saveNow()
{
alert("<?php saveNow(); ?>");
}
</script>


<button id="save" onclick="saveNow()">Save Note</button>

<!-- JavaScript includes. -->

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="assets/js/script.js"></script>
</body>

<div id="footer">
<footer>
<a href="">
<div id="footer_right">
Version 0.1.2
</div id="footer_right">
</a>
<audio src="assets/audiojs/music.mp3" preload="auto"></audio>
<div id="footer_left">
Save function not working yet
</div id="footer_left">
</footer>
</div id="footer">
</html>

脚本.js:

$(function(){

var note = $('#note');

var saveTimer,
lineHeight = parseInt(note.css('line-height')),
minHeight = parseInt(note.css('min-height')),
lastHeight = minHeight,
newHeight = 0,
newLines = 0;

var countLinesRegex = new RegExp('\n','g');

// The input event is triggered on key press-es,
// cut/paste and even on undo/redo.

note.on('input',function(e){

// Count the number of new lines
newLines = note.val().match(countLinesRegex);

if(!newLines){
newLines = [];
}

// Increase the height of the note (if needed)
newHeight = Math.max((newLines.length + 1)*lineHeight, minHeight);

// This will increase/decrease the height only once per change
if(newHeight != lastHeight){
note.height(newHeight);
lastHeight = newHeight;
}
}).trigger('input');    // This line will resize the note on page load

function ajaxSaveNote(){

// Trigger an AJAX POST request to save the note
$.post('index.php', { 'note' : note.val() });
}
});

我真的不知道如何解决这个问题,所以非常感谢任何帮助。当他单击按钮时,我只想让文件以与用户的 IP 地址相同的名称保存。请记住,我仍然是拥有这些更高级功能的大新手,所以请指出我做错的任何事情(但也请简单解释一下:))。

谢谢阅读,

市场。

4

1 回答 1

1

首先,我建议考虑将文件名作为 IP 地址是否是一个好主意...许多工作场所和其他组设置共享相同的 IP 地址,因此像这样的工作场所的任何用户都会看到任何其他人留下的注释同一工作场所的用户。

至于您的错误,我认为问题可能是您没有$note_name在函数中声明为全局变量。尝试将其更改为:

function saveNow() {
    global $note_name;
    // Write the file to disk
    file_put_contents($note_name, $_GET['note']);
    echo '{"saved":1}';
}

在 PHP 中,如果要在函数中使用全局变量(未在函数或类中声明的变量),则始终必须使用global如上所示的关键字。如果您的代码结构稍有不同,则可以完全避免使用全局变量,但这是另一个话题。

我想知道为什么你没有收到关于它没有被定义的通知……当你还在开发时,你可能想把它放在代码的顶部:

error_reporting(E_ALL);

PS 尽管您的代码不这样做也可以工作,但出于安全原因,最好在从 PHP 输出 JSON 之前指定 JSON MIME 类型,例如:

function saveNow() {
    global $note_name;
    // Write the file to disk
    file_put_contents($note_name, $_GET['note']);
    header('Content-type: application/json');
    echo '{"saved":1}';
    exit;
}
于 2013-05-11T02:41:16.070 回答