Hi,

I need to pass full content of $_FILES to third page but fail to do so. File is selected once in first page. It might look wierd but It has to work this way.

If I use session $_SESSION['csv_file'] = $_FILES; in page 2, I cannot read the content of the file in page 3.

Thanks in advance

1.php

<form action="2.php" method="POST" enctype="multipart/form-data">
   Please select CSV file : <input type="file" name="uni_file" /><br />
   <input type="submit" name="submit_button" value="Upload" />
</form>

2.php

if (! isset($_FILES['uni_file'])) { echo 'Fail'; exit; }

//Prints all nicely
echo '<pre>'; print_r($_FILES); echo '</pre>';

<form action="3.php" method="POST" enctype="multipart/form-data">
   <input type="hidden" name="hidden_csv_file_content" value="<?php echo serialize($_FILES); ?>" /><br />
   <input type="submit" name="submit_button" value="Upload" />
</form>

3.php

//Gives error: unserialize() Error at offset 5 of 9 bytes
$csv_file = unserialize($_POST['hidden_csv_file_content']);

echo '<pre>'; print_r($csv_file); echo '</pre>';

Dani AI

Generated

: the symptom (unserialize() error) and the approach in the thread point to two separate problems that commonly trip people up.

First, serializing $_FILES into a raw HTML value="..." string corrupts the data: PHP serialize output contains quotes and other characters that will break an attribute unless escaped or encoded, which explains the unserialize error (serialized text was truncated/malformed). Second, $_FILES only holds upload metadata and a tmp_name pointing to a temporary file that PHP will remove after the request finishes—so saving the $_FILES array alone (in session or a hidden field) does not preserve the actual file contents for page 3. ’s suggestion to read the file on page 2 is correct, but the file bytes must be explicitly preserved.

Two practical fixes (pick one):

  1. Move the uploaded file to a permanent location on page 2 and pass the saved path (e.g. in $_SESSION) to page 3:

    session_start();
    $destDir = __DIR__.'/uploads';
    if (!is_dir($destDir)) mkdir($destDir,0755,true);
    $dest = $destDir . '/' . basename($_FILES['uni_file']['name']);
    if (move_uploaded_file($_FILES['uni_file']['tmp_name'],$dest)) {
     $_SESSION['uploaded_csv_path'] = $dest;
    }
  2. Read the file contents on page 2 and store the bytes (session or POST). Encode to avoid binary/quote problems:

    session_start();
    $bytes = file_get_contents($_FILES['uni_file']['tmp_name']);
    $_SESSION['csv_base64'] = base64_encode($bytes);
    /* On page 3: $csv = base64_decode($_SESSION['csv_base64']); */

Notes and cautions: do not store very large files in session or hidden fields (memory/post limits). Check post_max_size/upload_max_filesize and always validate/clean uploaded filenames and contents before saving or parsing. Inspect the generated HTML (view source) if serialized values look truncated—improper escaping is usually the culprit.

Recommended Answers

All 2 Replies

I said at the begining " It might look wierd but It has to work this way...." so no need to question why page 2 because I don't know too.

I did hidden fields too just like in my example above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.