上传后重定向到页面并使用会话显示消息?

时间:2013-02-02 17:54:34

标签: php redirect

有人可以请帮助我有这个图像上传脚本我正在使用哪个工作正常,但我试图将这部分代码添加到底部:

$_SESSION['dashboard_intro']="<div class=\"user_settings_box_home\"><strong>Welcome to your new profile</strong> - This is your Dashborad.</div><div class=\"infobox-close4\"></div>"; 
header('Location: dashboard.php');

这个想法是,在用户上传图像并运行上传脚本后,用户将被重定向到另一个页面,并使用会话显示一条消息。

然而,图片上传后没有发生任何事情,页面没有重定向,也没有显示任何消息,请有人告诉我哪里出错了。

<?php
    session_start()
    ?>
    <?


    // LOG
    $log = '=== ' . @date('Y-m-d H:i:s') . ' ===============================' . "\n"
            . 'FILES:' . print_r($_FILES, 1) . "\n"
            . 'POST:' . print_r($_POST, 1) . "\n";
    $fp = fopen('upload-log.txt', 'a');
    fwrite($fp, $log);
    fclose($fp);


    // Result object
    $r = new stdClass();
    // Result content type
    header('content-type: application/json');


    // Maximum file size
    $maxsize = 10; //Mb
    // File size control
    if ($_FILES['xfile']['size'] > ($maxsize * 1048576)) {
        $r->error = "Max file size: $maxsize Kb";
    }


    // Uploading folder
    $folder = '../'. '../'. 'data/'. 'photos/'. $_SESSION['user_id'] . '/';
    if (!is_dir($folder))
        mkdir($folder);

    // If specifics folder 
    $folder .= $_POST['folder'] ? $_POST['folder'] . '/' : '';
    if (!is_dir($folder))
        mkdir($folder);

    // PASS USER_ID HERE
    $folder2 = '../'. '../'. 'data/'. 'photos/'. $_SESSION['user_id'] . '/';
    if (!is_dir($folder2))
        mkdir($folder2);

    // New directory with 'files/USER_SESSION_ID/'
    $folder = $newDir . $folder2;


    // If the file is an image
    if (preg_match('/image/i', $_FILES['xfile']['type'])) {

        $filename = $_POST['value'] ? $_POST['value'] :
                $folder . '_default.jpg';
    } else {

        $tld = split(',', $_FILES['xfile']['name']);
        $tld = $tld[count($tld) - 1];
        $filename = $_POST['value'] ? $_POST['value'] :
                $folder . sha1(@microtime() . '-' . $_FILES['xfile']['name']) . $tld;
    }


    // Supporting image file types
    $types = Array('image/png', 'image/gif', 'image/jpeg');
    // File type control
    if (in_array($_FILES['xfile']['type'], $types)) {
        // Create an unique file name    
        // Uploaded file source
        $source = file_get_contents($_FILES["xfile"]["tmp_name"]);
        // Image resize
        imageresize($source, $filename, $_POST['width'], $_POST['height'], $_POST['crop'], $_POST['quality']);
    } else
    // If the file is not an image
    if (in_array($_FILES['xfile']['type'], $types)) 
        move_uploaded_file($_FILES["xfile"]["tmp_name"], $filename);



    // File path
    $path = str_replace('welcome_upload.php', '', $_SERVER['SCRIPT_NAME']);

    // Result data
    $r->filename = $filename;
    $r->path = $path;
    $r->img = '<img src="' . $path . $filename . '" alt="image" />';

    // Return to JSON
    echo json_encode($r);

    // Image resize function with php + gd2 lib
    function imageresize($source, $destination, $width = 0, $height = 0, $crop = false, $quality = 80) {
        $quality = $quality ? $quality : 80;
        $image = imagecreatefromstring($source);
        if ($image) {
            // Get dimensions
            $w = imagesx($image);
            $h = imagesy($image);
            if (($width && $w > $width) || ($height && $h > $height)) {
                $ratio = $w / $h;
                if (($ratio >= 1 || $height == 0) && $width && !$crop) {
                    $new_height = $width / $ratio;
                    $new_width = $width;
                } elseif ($crop && $ratio <= ($width / $height)) {
                    $new_height = $width / $ratio;
                    $new_width = $width;
                } else {
                    $new_width = $height * $ratio;
                    $new_height = $height;
                }
            } else {
                $new_width = $w;
                $new_height = $h;
            }
            $x_mid = $new_width * .5;  //horizontal middle
            $y_mid = $new_height * .5; //vertical middle
            // Resample
            error_log('height: ' . $new_height . ' - width: ' . $new_width);
            $new = imagecreatetruecolor(round($new_width), round($new_height));
            imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
            // Crop
            if ($crop) {
                $crop = imagecreatetruecolor($width ? $width : $new_width, $height ? $height : $new_height);
                imagecopyresampled($crop, $new, 0, 0, ($x_mid - ($width * .5)), 0, $width, $height, $width, $height);
                //($y_mid - ($height * .5))
            }
            // Output
            // Enable interlancing [for progressive JPEG]
            imageinterlace($crop ? $crop : $new, true);

            $dext = strtolower(pathinfo($destination, PATHINFO_EXTENSION));
            if ($dext == '') {
                $dext = $ext;
                $destination .= '.' . $ext;
            }
            switch ($dext) {
                case 'jpeg':
                case 'jpg':
                    imagejpeg($crop ? $crop : $new, $destination, $quality);
                    break;
                case 'png':
                    $pngQuality = ($quality - 100) / 11.111111;
                    $pngQuality = round(abs($pngQuality));
                    imagepng($crop ? $crop : $new, $destination, $pngQuality);
                    break;
                case 'gif':
                    imagegif($crop ? $crop : $new, $destination);
                    break;
            }
            @imagedestroy($image);
            @imagedestroy($new);
            @imagedestroy($crop);

            $_SESSION['dashboard_intro']="<div class=\"user_settings_box_home\"><strong>Welcome to your new profile</strong> - This is your Dashborad.</div><div class=\"infobox-close4\"></div>"; 
    header('Location: dashboard.php');
        }
    }


    ?>

3 个答案:

答案 0 :(得分:1)

你需要把exit();在标题重定向之后,否则您刚刚将两页内容加载到一页中。

还要确保你有session_start();在所有脚本的顶部。

如果它解决然后确定其他也尝试这个或你可能需要两者的组合,但很少努力。

在dashboard.php中创建一个div,用于显示

等消息
session_start();
<?php if(isset($_SESSION['dashboard_intro'])){?>
    <div class="user_settings_box_home"><strong><?php echo $_SESSION['dashboard_intro']?></strong> -  - This is your Dashborad.</div><div class="infobox-close4"></div>
<?php } ?>

并设置$_SESSION['dashboard_intro'] = "Welcome to your new profile";

成功上传文件并重定向到dashboard.php

header('Location: dashboard.php');exit();

答案 1 :(得分:0)

在标题之前你不能回复任何像空格,换行符等等。它会在你的代码上停止标题工作

?>
<?

答案 2 :(得分:-1)

<script type="text/javascript">
  var url = "<?php echo $link; ?>";
  window.location.href = url;
</script>
<noscript>
    JavaScript is turn off. Try redirect <a href="<?php echo $link; ?>">manyaly</a>.
</noscript>