上传文件后刷新一个php页面 [英] Refresh a php page after uploading a file

查看:164
本文介绍了上传文件后刷新一个php页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用PHP和HTML表单将文件上传到服务器。



我使用了w3school的示例:这是非常有用的,并帮助了我很多。



当然,为了解决我的问题,我需要调整这些代码,所以情况就是这样:

1) fileform.php

 <! -  fileform.php  - > 
< form action =upload.phpmethod =postenctype =multipart / form-data>
< h3>上传档案:< / h3>
< input type =filename =fileToUploadid =fileToUpload>
< input type =submitvalue =Uploadname =submit>
< / form>

2)这是我的 upload.php

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

$ targetDir =/ path / to / upload / dir;
$ targetFile = $ targetDir。基名($ _ FILES [ fileToUpload] [ 名称]);
$ upload = 1;
$ fileType = pathinfo($ targetFile,PATHINFO_EXTENSION);

if(isset($ _ POST [submit]))
{
/ *检查文件大小* /
if($ _ FILES [fileToUpload] [size]> 50000000000)
{
echo对不起,您的档案太大了。
$ upload = 0;

ob_end_flush();
include(fileform.php);

允许某些文件格式如果($ fileType!=data)
{
echo对不起,无效的文件类型。
$ upload = 0;

ob_end_flush();
include(fileform.php);

$ b检查$ uploadOk是否被错误设置为0 * /
if($ upload == 0)
{
echo 对不起,你的文件没有上传。
ob_end_flush();
include(fileform.php);
}
else
{
if(move_uploaded_file($ _ FILES [fileToUpload] [tmp_name],$ targetFile))
{
echo 文件 。 basename($ _FILES [fileToUpload] [name])。 已上传。
}
else
{
echo抱歉,上传文件时发生错误。
}

ob_end_flush();
include(fileform.php);






我可以正确地上传文件,但无法重新加载正确的PHP页面。
它只出现一个白色背景的页面显示:

文件file.data已被上传。

$警告:session_start()[function.session-start]:不能发送会话缓存限制器 - 已经发送的头文件(输出在/var/www/upload.php:40开始)在/var/www/login.php第4行



带有PHP页面的一些图标...



我很确定我在做错误的 upload.php 文件,但我不知道究竟是什么错误,所以如何解决它。 b
$ b

如何更改我的代码以便正确地重新载入PHP页面?



预先感谢您的帮助。

解决方案

Headers already sent意味着您的PHP脚本已经发送了HTTP头文件,因此无法进行修改

在调用 session_start 。更好的做法是,只需要在你的PHP文件中做第一件事(就是把它放在绝对的开头,在所有的HTML之前)。

$ b $(code> session_start b

<?php

  ob_start(); 

您可以像这样使用:

 <?php 
ob_start();
session_start();

包含'你的php文件';

...
...


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.

解决方案

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

...
...

这篇关于上传文件后刷新一个php页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆