开发者

In the next page $_FILES["myfile"]["tmp_name"] = NULL

I had upload file with POST and it seems to works fine, but after I go to the next page I can't use the file with the location in the global varible: $_FILES["myfile"]["tmp_name"] because it's null. I don't know why, I used this code before and it worked fine...

Here is the code:

www/step1.php

if (isset($_POST["check_if_press"]) && $_POST["check_if_press"] == "Upload") 

{

if (!empty($_FILES["myfile"]["tmp_name"])) 

{

  header("Location: ./step2_1.php");

  }else     echo "Please select a file";

}

<form action="<?php echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data">

 Upload file: <input type='file' name='myfile' /><br />

 <input type='Submit' name='check_if_press' value='Upload'  />

</form>

www/step2_1.php

echo  $_FILES["myfile"]["tmp_name"];

Now I get NULL printed on the screen.

When I used POST without the arguments :

action="<?php echo $PHP_SELF

But开发者_如何学编程 with:

action="./step2.php"

Instead, It work, but than I cant use the upload check .

Thanks,

Yonatan.


The file is only present in $_FILES within the scope of the post. What's happening here is:

  1. User is posting a file to step1
  2. step1 is doing something with the file, then telling the user to go to step2 (by means of the location header)
  3. In response to the location header, user is making a GET request to step2, no posted file is associated

You're going to need to either post the file directly to step2 (which you said worked, just didn't have the logic of step1) and put your server-side file checking logic there, or in step1 store the file somewhere on the server (either save it to the file system, store it in a database, maybe store it in session which I don't recommend, etc.) where it can be accessed by step2.

step2 is a completely separate request from step1 and doesn't have access to anything within the scope of step1.


The temporary file is deleted at the end of your post script, so you'll need to copy it to another location before you redirect:

if (!empty($_FILES["myfile"]["tmp_name"])) 

{
  //COPY THE FILE TO ANOTHER LOCATION HERE...

  header("Location: ./step2_1.php");

  }else     echo "Please select a file";

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜