1

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...

2
  • Remove space between <? php Commented Oct 11, 2014 at 3:22
  • Thanks Raptor, I removed it. Commented Oct 11, 2014 at 12:25

2 Answers 2

3

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>"; 
} 
Sign up to request clarification or add additional context in comments.

9 Comments

When I add your $is_uploaded... code in my php, the upload breaks and does not work.
oh sorry i forgot semi colon move_uploaded_file($fileTmpLoc, "upload/" . $str);
Now the upload works (I am sorry too that I didn't notice the absence of semi colon), however now the if part does not seem to work. For example a paste in my html of <?php if($is_uploaded ){ echo "<p>You uploaded ".$str."<p>"; } ?> gives me the message: "You uploaded ".$str." "; } ?> " .
What do you mean i dont understand? Can u show ur html
|
0

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>

5 Comments

I used your direction but with no luck... I am pretty much convinced though that I should use sessions and ajax...
I just showed you how the structure, not the full code, you have to create the full code yourself
I did... I will try again... Thanks!
Good luck, if you can't see a message it means the $_SESSION['message'] is empty
have fun experimenting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.