PHP post变量未呈现

时间:2013-09-29 10:14:53

标签: javascript php html apache

我有一个PHP程序,它接收图像名称并加载图像并在页面上显示名称和图像。

javascrip中的变量写为

var latest_image_name = '<?=$post_img_name?>';

PHP代码是

<?php
        foreach($files_assoc_array_keys as $file_name){
            if($file_name==$post_img_name){
            ?>
            <label class="lbl_image_name active"><?=$file_name?></label>
            <?php
            }else{
            ?>
            <label class="lbl_image_name"><?=$file_name?></label>
            <?php
            }
        }
        ?>

html输出正在呈现为

<div id="image_list_wrapper">
                    <label class="lbl_image_name"><?=$file_name?></label>
                    </div>

正如您所看到的,PHP似乎没有用已发布的图像名称替换标记。

代码在它开发的原始服务器上运行,当我将它迁移到另一台服务器时它不起作用,我已经尝试了两个其他服务器,包括带有apache和PHP的Centos 6.4。我不确定原始服务器的设置是什么样的。

完整代码如下所示

<?php
header('Refresh: 5; URL=display.php');
print_r($_POST['post_img_name']);
$target_directory = "uploaded_images";

if(!file_exists($target_directory)){
    mkdir($target_directory);
}



if(isset($_POST['del_image'])) {
    $del_image_name = $_POST['del_img_name'];
    if(file_exists($target_directory."/".$del_image_name.".jpg")){
        unlink($target_directory."/".$del_image_name.".jpg");
    }
if(is_dir_empty($target_directory)){
        die("Last image delete. No images exist now.");
}
    $post_img_name = basename(get_latest_file_name($target_directory), '.jpg');

}else if(isset($_POST['post_img_name'])){
    $post_img_name=$_POST['post_img_name'];
    $post_img_temp_name = $_FILES['post_img_file']['tmp_name'];
}else{
    $post_img_name = basename(get_latest_file_name($target_directory), '.jpg');
}

$files_array = new DirectoryIterator($target_directory);
$total_number_of_files = iterator_count($files_array) - 2;
$files_assoc_array = array();


$already_exists = "false";
if($total_number_of_files != 0){
    foreach ($files_array as $file_info){
        $info = pathinfo( $file_info->getFilename() );
        $filename = $info['filename'];
        if ($filename==$post_img_name) {
            $already_exists = "true";
        }
    }
}

if(!isset($_POST['del_image']) && isset($_POST['post_img_name'])){
    $target_file = "$target_directory"."/".$post_img_name.".jpg";
    $source_file = $post_img_temp_name;



    if($already_exists == "true"){
        unlink($target_file);
    }
    move_uploaded_file($source_file, $target_file);
}



foreach ($files_array as $file_info){
    $info = pathinfo( $file_info->getFilename() );
    $filename = $info['filename'];
    if(!$file_info->isDot()){
        $files_assoc_array[$filename] =  $target_directory."/".$file_info->getFilename();
    }
}

$files_assoc_array_keys = array_keys($files_assoc_array);

function get_latest_file_name($target_directory){
    $files_array = new DirectoryIterator($target_directory);
    $total_number_of_files = iterator_count($files_array) - 2;
    $timestamps_array = array();

    if($total_number_of_files!=0){
        foreach($files_array as $file){
            if(!$file->isDot()){
                $timestamps_array[filemtime($target_directory."/".$file)] = $file->getFilename();
            }
        }
    }
    $max_timestamp = max(array_keys($timestamps_array));
    return $timestamps_array[$max_timestamp];

}
function is_dir_empty($dir) {
    if (!is_readable($dir))
        return NULL;
    $handle = opendir($dir);
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            return FALSE;
        }
    }
    return TRUE;
}

?><!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
    <link rel="stylesheet" href="css/style.css"/>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        var files_array_text = '<?php echo implode(", ", $files_assoc_array)?>';
        var files_array_keys_text = '<?php echo implode(", ", $files_assoc_array_keys)?>';

        var files_array = files_array_text.split(", ");
        var files_array_keys = files_array_keys_text.split(", ");

        var files_assoc_array = createAssociativeArray(files_array_keys, files_array);

        var latest_image_name = '<?=$post_img_name?>';
        display_image(latest_image_name);


        $('.lbl_image_name').click(function(){
            $('#img_loading').show();
            $('#img_display').hide();

            var image_name = $(this).text();
            $('.active').removeClass('active');
            $(this).addClass('active');

            display_image(image_name);
        });

        function createAssociativeArray(arr1, arr2) {
            var arr = {};
            for(var i = 0, ii = arr1.length; i<ii; i++) {
                arr[arr1[i]] = arr2[i];
            }
            return arr;
        }

        function display_image(image_name){
            var image_path = files_assoc_array[image_name];
            $('#img_display').attr('src', image_path);
            $('#img_display').load(image_path, function(){
                $('#img_loading').hide();
                $('#img_display').show();
            })

        }
    });
    </script>
</head>
<body>
    <div id="container">
        <div id="image_list_wrapper">
            <?php
            foreach($files_assoc_array_keys as $file_name){
                if($file_name==$post_img_name){
                    ?>
                    <label class="lbl_image_name active"><?=$file_name?></label>
                    <?php
                }else{
                    ?>
                    <label class="lbl_image_name"><?=$file_name?></label>
                    <?php
                }
            }
            ?>
        </div>
        <div class="separator"></div>
        <div id="image_display_wrapper">
            <div id="img_loading_wrapper">
                <img src="images/loading.gif" id="img_loading"/>
            </div>

            <img src="" id="img_display"/>
        </div>
        <div style="clear: both">

        </div>
    </div>
    <a href="index.php">Go Back</a>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

正如仲裁者指出我的服务器不支持<?= ... ?>后,我改为<?php print $variable_name ?>