我需要将php数组传递给jquery来完成某些任务.为SESSION创建php数组,然后为jquery创建json_encoded.之后,我将该变量存储到window js命名空间,以便在我的jquery脚本中使用该数组.
if(isset($_SESSION['mySession'])){
$json_array = json_encode($php_array);
}
<script>
<?php echo "window.json_from_php = ".$json_array; ?>
</script>
Run Code Online (Sandbox Code Playgroud)
我需要做的是在做任何事之前检查这个数组是否存在/是空/未定义.check子句对'undefined'操作数成功,但对其余操作不成功.如何在jquery中检查数组是否为空?
这是初始化后json_encoded数组的输出,当$ php_array中没有任何元素时:
string '[""]' (length=4)
Run Code Online (Sandbox Code Playgroud)
这是jquery:
$(function() {
var results = $('#results');
var js_array = window.json_from_php;
var count = js_array.length;
if ((typeof window.json_from_php === 'undefined') || (js_array === null) || (jQuery.isEmptyObject(js_array)) || (count === 0)) {
$('<p>Array is empty.</p>').appendTo(results);
}
else {
jQuery.each(js_array,function(key,val){
$('<ul><li id="'+key+'">'+val+'</li></ul>').appendTo(results);
});
}
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试为 webroot 之外的用户发送文件(附件)。我制作了强制下载脚本,它在标题中发送文件并在流中输出它。这很好用,直到我调用 readfile (也可以是标题设置),它输出一个文件,其中包含我的 html 源(此特定页面的)的一半。我做了 file_get_contents() 并且文件包含正确的字符串:“test”。这里有什么问题?
<?php
//The path where the files are stored
$file_path = "";
if(isset($_GET['file'])) {
$file_path = $_GET['file'];
}
else {
header('HTTP/1.0 404 Not Found');
die('File not found!');
}
$file_name = basename($_GET['file']);
//Make sure the file exists
if(!file_exists($file_path) && !is_file($file_name)) {
header('HTTP/1.0 404 Not Found');
die('File not found!');
}
//Initialize the actual file.
$file = $file_path;
//Notice: Remember to set fifo extension in php.ini
//Windows users must include the bundled php_fileinfo.dll DLL file …
Run Code Online (Sandbox Code Playgroud)