user3049377
user3049377

Reputation:

Refresh a php page after uploading a file

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.

Upvotes: 0

Views: 6000

Answers (6)

robbmj
robbmj

Reputation: 16496

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.

Upvotes: 0

user254153
user254153

Reputation: 1883

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

<?php
redirect(your_url);
?>

Upvotes: 0

Roh&#236;t J&#237;ndal
Roh&#236;t J&#237;ndal

Reputation: 27192

"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';

...
...

Upvotes: 1

symcbean
symcbean

Reputation: 48357

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.

Upvotes: 0

Nikita Melnikov
Nikita Melnikov

Reputation: 209

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)

Upvotes: 0

Anthony
Anthony

Reputation: 107

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.

Upvotes: 0

Related Questions