Page 1 of 1

Show uploaded files in one web page as the submit button

Posted: Sun Apr 08, 2012 10:53 pm
by adhi91
Hi,

I have two files which is index.php and upload.php
In index I have a browse button and a submit button. If I click the submit button, it will direct to a new web page and show the uploaded file name and size.
All of this works.
Now I want to show the uploaded file name and size on the same web page as the browse and submit button.

Code: Select all

print('<table>');
print('<tr><td width=100px>File Name</td><td>File Size</td></tr>');
print('<tr><td>'. $_FILES['uploadedfile']['name'] . '</td><td>'. round($_FILES['uploadedfile']['size']/1024,4) . ' Kb</td></tr>');
print('</table>');
I have tried to insert the upload.php code in a new html

Code: Select all

 <form name="upload" method="POST">
            <input type=hidden name=MAX_FILE_SIZE value=500000>
            <input type=file name=uploadedfile>
            <input type=submit name="upload" value=Upload>
        </form>
and insert the upload.php code right below head

Code: Select all

<?php
if(!empty($_POST['upload']))
{
//All the code for upload file including the table containing uploadedfile name and size
}
?>
What is missing? or are there any better way to do this?

Re: Show uploaded files in one web page as the submit button

Posted: Mon Apr 09, 2012 7:34 am
by social_experiment
adhi91 wrote:What is missing? or are there any better way to do this?
If you want to display the information on the same page that displays your submission form you have to call the page on itself;

Code: Select all

 // you can do this, leave the action attributes' value blank
 <form action="" method="post" enctype="multipart/form-data" >
// or use $_SERVER['PHP_SELF']
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" >
You would also have to test if the button has been submitted

Code: Select all

<?php
 if (isset($_POST['upload']) && $_POST['upload'] == 'Upload')) {
 // button has been clicked, continue to process form.
 }
Depending on where you want the information to be displayed, above or below the form, you can move the snippet of code above to the appropriate position.

Re: Show uploaded files in one web page as the submit button

Posted: Mon Apr 09, 2012 8:27 pm
by adhi91
Thanks. I use the first one, it works.

if I add another download button, where I only wanted to download the uploaded file.
I try in the sequence

Code: Select all

echo"<input type=file name=uploadedfile onchange=this.form.submit()>";
//the process for upload
echo"<form action='' method=POST><input type=submit value=Download name=download></form>";
//the process for download
It works if I use it separately.
I also attempt to insert the download process in the upload process using if statement

Code: Select all

//if upload successful
//download process is next using isset to check if download is click
However, this method not working.

How can I tell user that download only works if I already upload the file?

Re: Show uploaded files in one web page as the submit button

Posted: Tue Apr 10, 2012 12:07 am
by social_experiment
adhi91 wrote:How can I tell user that download only works if I already upload the file?
You could use javascript to enable the button once the upload has been completed; or in your if statement to check if a file has been uploaded, you could print the button to the browser.