0

I need to upload a file into the server using PHP and an HTML form.

I used the w3school's example: PHP 5 File Upload which is very helpful and helped me a lot.

Of course I need to adapt that code in order to solve my problem, so this is the situation:

1) the HTML form is placed into fileform.php:

<!-- fileform.php -->
<form action="upload.php" method="post" enctype="multipart/form-data">
<h3>Upload a file:</h3>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="submit">
</form>

2) this is my upload.php:

<?php
/* upload.php */
set_time_limit(0);

$targetDir = "/path/to/upload/dir";
$targetFile = $targetDir . basename($_FILES["fileToUpload"]["name"]);
$upload = 1;
$fileType = pathinfo($targetFile, PATHINFO_EXTENSION);

if(isset($_POST["submit"]))
{
    /* Check file size */
    if($_FILES["fileToUpload"]["size"] > 50000000000)
    {
        echo "Sorry, your file is too large.";
        $upload = 0;

        ob_end_flush();
        include("fileform.php");
    }
    /* Allow certain file formats */
    if($fileType != "data" )
    {
        echo "Sorry, non valid filetype.";
        $upload = 0;

        ob_end_flush();
        include("fileform.php");

    }
    /* Check if $uploadOk is set to 0 by an error */
    if($upload == 0)
    {
        echo "Sorry, your file was not uploaded.";
        ob_end_flush();
        include("fileform.php");
    } 
    else
    {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile))
        {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        }
        else
        {
            echo "Sorry, there was an error uploading your file.";
        }

        ob_end_flush();
        include("fileform.php");
    }
}

I can upload correctly the file, but I cannot reload correctly the PHP page. It only appears a page with white backgroung showing:

The file file.data has been uploaded.

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/upload.php:40) in /var/www/login.php on line 4

with some icons of the PHP page...

I am quiet sure I am doing something wrong into the upload.php file but I don't know what exactly is wrong and so how to fix it.

How do I have to change my code in order to reload the PHP page correctly?

Thank you in advance for your help.

  • Nominated for re-opening - where does it say session_start() in the above? – symcbean Dec 04 '14 at 23:33
  • 3
    @symcbean Errr... it's in the question? `Warning: session_start() [function.session-start]: Cannot send session cache limiter......` in **bold** letters. Question should have remained closed. It's even in the answers below. – Funk Forty Niner Dec 05 '14 at 01:19
  • After all these answer it's mandatory for me to thank you guys for your constructive answers and help. However I modified the code as **Rohit Jindal** suggested (which seems to be the most explicit code example) and it works. Well, the page is not reloaded so good as should do but maybe it's just another problem to solve. –  Dec 05 '14 at 08:09
  • @Fred: It only appears in the error message - not in the code. While it is possible to have sessions without explicitly calling session start (if you set session.auto_start in the .ini file) but then you won't get the error quoted. The code shown here did not produce the error shown here. – symcbean Dec 05 '14 at 11:04

6 Answers6

1

"Headers already sent" means that your PHP script already sent the HTTP headers, and as such it can't make modifications to them now.

Check that you don't send ANY content before calling session_start. Better yet, just make session_start the first thing you do in your PHP file (so put it at the absolute beginning, before all HTML etc).

Use this on the top just after <?php

ob_start();

you can use like this:

<?php
ob_start();
session_start();

include 'your php file';

...
...
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
0

PHP is a server side language, therefore you should use javascript to reload the page.

<script>
function myFunction() {
    location.reload();
}
</script>

This will reload the current page.

Anthony
  • 107
  • 5
0

If you want to refresh page on server side: In plain php you may use header function (http://php.net/manual/ru/function.header.php) to refresh current page.

header('Location: /'.$_SERVER["REQUEST_URI"]);

if you want to refresh page at client side you may use:

location.reload()

(javascript)

Nikita Melnikov
  • 209
  • 1
  • 8
0

It only appears a page with white backgroung showing: The file file.data has been uploaded.

It is your code which is doing this:

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile))
    {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    }
    else
    {
        echo "Sorry, there was an error uploading your file.";
    }

    ob_end_flush();
    include("fileform.php");

The reason you are subsequently getting an error is that you must be calling session_start() somewhere after the ob_end_flush() - but this does not appear in any of the code you've shown us. i.e. the code you have shown us will not produce the error you've quoted.

Coincidentally, despite the code above being peppered with ob_end_flush() (for no apparent reason) there is no ob_start(). This might appear in the code you've not shown us.

symcbean
  • 47,736
  • 6
  • 59
  • 94
0

I am doing something wrong into the upload.php file but I don't know what exactly is wrong

The problem you are running into is a classic.

In your fileform.php script you are calling session_start() (via login.php). This does not work after the file upload because, in the upload script you have written to the HTTP response body via echo:

session_start() needs to write HTTP headers. since the headers have already been sent session_start() can't do its job.

That is what this error message is trying to say:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/upload.php:40) in /var/www/login.php on line 4

-

how to fix it

You will need to refactor your upload script so that you include('fileform.php') before sending any other content to the response body.

<?php
// .. do not echo anything
include('fileform.php');
// .. do the file upload stuff.

Note: if you try to redirect after an echo you will run into the same problem as session_start() as an HTTP redirect requires writing the response headers.

robbmj
  • 16,085
  • 8
  • 38
  • 63
0

You can reload the same page by redirecting to same url.

<?php
redirect(your_url);
?>
user254153
  • 1,855
  • 4
  • 41
  • 84