在html中访问php变量

I am using the below php code:

<?php 
$str = time().'_'.$_FILES["file1"]["name"];
$fileName = $_FILES["file1"]["name"]; 
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; 
$fileType = $_FILES["file1"]["type"]; 
$fileSize = $_FILES["file1"]["size"]; 
$fileErrorMsg = $_FILES["file1"]["error"]; 

if (!$fileTmpLoc) { 
    echo 'ERROR: Please browse for a file before clicking the upload button.'; 
    exit(); 
} 

if(move_uploaded_file($fileTmpLoc, "upload/" . $str)){ 
    echo "$fileName upload is complete"; 
} else { 
    echo 'move_uploaded_file function failed'; 
}
?>

to upload a file on a server. I am altering the filename and keeping it in $str. My question is:

When the upload is completed I want to be able in my html file to print a message like

You uploaded <?php echo $str; ?>

How can I do that? How can I access $str ? Shall I use sessions? Any hint? Thank you a lot in advance...

Simply store the return value of move_uploaded_file

$is_uploaded = move_uploaded_file($fileTmpLoc, "upload/" . $str);
if($is_uploaded ){ 
   echo "$fileName upload is complete"; 
} else { 
   echo "move_uploaded_file function failed"; 
} 

in HTML

if($is_uploaded ){ 
   echo "<p>You uploaded ".$str."<p>"; 
} 

Local php variables will get deleted or "flushed" when the web browser REFRESHED. To solve this,

  • you must use AJAX and return a JSON response to your html file for the successful upload

or

  • Store the success or error message on a Session then unset that session after you showed it for example

    <?php
       @session_start();
    
        //file upload code
        //if success
            $_SESSION['message'] = 'File successfully uploaded';
        //else
            $_SESSION['message'] = 'File upload error';
    ?>
    

then into your HTML

     <?php @session_start() ?>

     <body>
         <?php if(isset($_SESSION['message']): ?>
                <p><?php echo $_SESSION['message']; ?></p>
                <?php unset($_SESSION['message']) ?>
         <?php endif; ?>
     </body>