Asked  7 Months ago    Answers:  5   Viewed   22 times

I have a form with text inputs and file inputs; the text fields are being validated. Is there a way to have the form remember which files the user has already selected if they hit submit but need to go back because one of the text fields didn't validate?



You can't "pre-fill" the contents of a file upload field for security reasons. Also, that would mean the file would get re-uploaded every time the form is submitted, which would not be good.

Instead, do this:

  • Create a file upload field with name file_upload.
  • On the server-side, process the upload in any case, even if the rest of the form validation fails.
  • If the form validation failed, but the file was uploaded, insert a hidden input into the form with name file containing the name of the just uploaded file.
  • Display a user-visible indication that the file is okay. If it's an image, display a thumbnail version of it. If it's any other file, display its filename and/or icon.
  • If the user chooses to upload a different file in the file_upload field, process the upload and store the new value in file.


    $file = null;
    if (!empty($_POST['file'])) {
        $file = $_POST['file'];
    if (!empty($_FILES['file_upload'])) {

        // process upload, save file somewhere

        $file = $nameOfSavedFile;

    // validate form

<input type="file" name="file_upload" />
<input type="hidden" name="file" value="<?php echo $file; ?>" />
    if (!empty($file)) {
        echo "File: $file";

Important note

This mechanism can allow any user to claim other user's files as their own, by including a file name that they guessed exists on your server. You will want to ensure that uploaded files are clearly associated with a specific user to avoid this issue.

Wednesday, March 31, 2021
answered 7 Months ago

file_get_contents() will get the content of a file, not execute it as a PHP script. If you want this piece of code to be executed, you need to either include it, or process it (through an HTTP request to Apache, for instance).

If you include this file, it'll be processed as PHP code, and of course, print your HTML tags (include* can take any kind of file).

If you need to work with its content before you print it, use ob_* functions to manipulate the PHP output buffer. See :

ob_start(); // Start output buffer capture.
include("yourtemplate.php"); // Include your template.
$output = ob_get_contents(); // This contains the output of yourtemplate.php
// Manipulate $output...
ob_end_clean(); // Clear the buffer.
echo $output; // Print everything.

By the way, such mechanism sounds heavy for a template engine. Basically, templates should not contain PHP code. If you want such behavior, have a look at Twig :

Wednesday, March 31, 2021
answered 7 Months ago

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario:

Related usability ticket:

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
answered 7 Months ago

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
answered 5 Months ago

$cache_disqus is defined outside your function, and is therefore not accessible within the function.

Check out the PHP documentation on variable scope.

Saturday, May 29, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :