gpt4 book ai didi

php - 我正在尝试刷新 PHP 变量

转载 作者:行者123 更新时间:2023-11-28 15:59:27 24 4
gpt4 key购买 nike

所以我正在为客户制作一个网站,该客户有大量他们在 80 年代和 90 年代拍摄的不同乐队的照片,他们想尝试出售这些照片。

我尝试制作一个页面,使用 Javascript/PHP 在单击该带的文本时将图像目录更改为该带,而不是像上一个站点那样为每个带(有超过 100 个)创建一个页面.

到目前为止,我可以使用 PHP 函数在幻灯片文件夹中查找照片,但我无法更新此函数以搜索幻灯片文件夹中的子目录。 (例如,当单击“Metallica”时,我清空#imageGal,然后我想将 Metallica 文件夹中的所有新 Metallica 图像附加到图库中)。

我的 PHP 代码是:

<?php
$imagesDir = '';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
echo json_encode($images);
?>

这段 PHP 代码看起来效果很好。

我使用此 JQuery 代码获取图像:

$('#imageGal').empty();
$.ajax({
url: "slideshow/getimages.php",
dataType: 'json',
success: function(json){
for(var i=0;i<json.length;i++){
$('#imageGal').append('<img src="slideshow/' + json[i] + '">');
}
}, failure: function(json){
alert('Something went wrong. Please try again.');
}
});

当用户单击乐队(即 Metallica)时,就会执行此代码。

$('.options').mousedown(function() {
var name = $(this).attr('id');
//$('#output').html(name);

$.ajax({
type: "POST",
url: "slideshow/getimages.php",
data: {
imageDir: name
}, success: function(msg){
alert( "Data Saved: " + msg );
}
});
$('#imageGal').empty();
$.ajax({
url: "slideshow/getimages.php",
dataType: 'json',
success: function(json){
for(var i=0;i<json.length;i++){
$('#imageGal').append('<img src="slideshow/' + json[i] + '">');
}
}, failure: function(json){
alert('Something went wrong. Please try again.');
}
});
});

我无法更改 $imagesDir 变量,但如果我在 $imagesDir = "Metallica"变量中手动输入“Metallica”,它将完美加载这些图像。

有人可以提供任何帮助/建议吗?我已经在这上面呆了好几个小时了。谢谢你的一切!

最佳答案

除非您启用了 register_globals,否则您需要通过全局 $_POST 数组引用该变量。 $_POST['imagesDir'] 而不是 $imagesDir

但是,我想说的是,以当前的形式,简单地替换它是一个非常糟糕的主意,因为有人可能会尝试利用您的代码列出服务器上的任何目录。

您应该附加父目录以防止被利用。像这样的事情:

编辑您必须 chdir() 到路径,然后 glob 才能工作。我更新了下面的代码。

<?php
$imagesDir = $_SERVER['DOCUMENT_ROOT']; // this is the root of your web directory
$images = array();

// and this line ensures that the variable is set and no one can backtrack to some other
// directory
if( isset($_POST['imagesDir']) && strpos($_POST['imagesDir'], "..") === false) {
$imagesDir .= "/" . $_POST['imagesDir'];
chdir($imagesDir);

$images = glob('*.{jpg,jpeg,png,gif}', GLOB_BRACE);
}
echo json_encode($images);
?>

关于php - 我正在尝试刷新 PHP 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17498351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com