Search code examples
phphttpuploading

Header function won't redirect w/ variables


<?php
ini_set('display_errors', 1);
error_reporting(E_ALL|E_STRICT);
include_once("alphaID.php");
include_once("displayimage.php");
//include_once("insertdata.php");
//generate unique id
$key = microtime() + floor(rand()*10000);
$newname = alphaID($key);
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
$ext = explode('.',$_FILES['userfile']['name']);
$extension = '.'.$ext[1];
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$filepath = '/var/www/html/testing/'.$newname.$extension;
move_uploaded_file($_FILES['userfile']['tmp_name'], $filepath));
header('Location: displayimage.php?img='.$newname.);
exit;
?>

I can't seem to get the header function at the bottom to work. I cannot see any output in the code and have tried many things in an attempt to get it to work.


Solution

  • You cannot have any output before you send headers. Since you are outputting in displayimage.php, your location function will fail.

    Here is some great reading on StackOverflow about this issue:

    How to fix "Headers already sent" error in PHP

    EDIT

    You have a syntax error in your header call. You have an extra period. Remove the period after $newname. You should have:

    header('Location: displayimage.php?img='.$newname);
    

    Also, you have too many ( on your move_uploaded_file function. This is causing a syntax error as well. You should have:

    move_uploaded_file($_FILES['userfile']['tmp_name'], $filepath);
    

    Any time you do not see any output whatsoever, you usually have a syntax error. Just one syntax error will stop the entire page from executing.