Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts

    header location problems

    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...
    Code:
    <?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...

    Code:
    <?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

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •