...

View Full Version : header location problems



mattyod
05-03-2006, 05:45 PM
Hi guys,

I've been fiddling with this so long now that my brain has turned to mush. I'm sure it's something daft or simple that I've missed but maybe you guys can find it for me :)

It's a small function that tries to upload a file to folder and then either re-directs to a "Thank you" page, or a "Error - file already exists" page.

The first attempt went a bit like this...


<?php
$organisation = $_POST['organisation'];

$target_path = "uploads/" .$organisation. "/";

mkdir ($target_path);

$target_path=$target_path. basename ( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
header("location:thankyou.html");
}
else
{
header("location:errorimg.html");
}
?>

and would throw out the following error...



Warning: Cannot modify header information - headers already sent by (output started at /home6/summeruni/webs/www.summerunilondon.org/htdocs/partners/uploader.php:6) in /home6/summeruni/webs/www.summerunilondon.org/htdocs/partners/uploader.php on line 12


Which seemed to be saying that the headers where started on the mkdir line.

So I addapted it to...



<?php
$organisation = $_POST['organisation'];

$target_path = "uploads/" .$organisation. "/";

if (!is_dir($target_path))
{
mkdir ($target_path);
}

$target_path=$target_path. basename ( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
header("location:thankyou.html");
}
else
{
header("location:errorimg.html");
}
?>


to check directory existance first but now I ALWAYS go to the "thank you" page, irrespective of whether the file is already existing or not.

Any ideas?

Thanks in advance :)

ralph l mayo
05-04-2006, 08:00 AM
I don't know if this is the problem, but location headers must be fully qualified URLs, not relative paths.

header('Location: http://my.server/directory/file.php');

edit: oh, it's not the problem, the issue is that move_uploaded_file() overwrites existing files without warning. You need to separately check if $target_path exists and direct to the error page there.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum