Nicodemus
07-20-2012, 10:47 AM
Hi everyone
I did some HTML and PHP several years ago but then moved towards network security. I'm trying to code an application now and have found some simple code that demonstrates what I'm trying to do (but failing miserably!).
give.php
<html>
<!-- http://www.htmlgoodies.com/beyond/php/article.php/3472551/PHP-Tutorial-Uploading-Files.htm -->
<!-- http://www.htmlgoodies.com/beyond/php/article.php/3472561 -->
<head>
<title>File Upload Form</title>
</head>
<body>
This form allows you to upload a file to the server.<br>
<form action="take.php" method="post" enctype="multipart/form-data"><br>
Type (or select) Filename: <input type="file" name="uploadFile">
<input type="submit" value="Upload the file and your name">
</form>
<form action="" method="post">
What is your name?<br>
<input type="text" name="name">
<input type="submit" value="Confirm your name">
<?php
if (isset($_POST['name'])) {
echo 'You said your name is: '. $_POST['name'];
}
?>
</form>
</body>
</html>
take.php
<html>
<!-- http://www.htmlgoodies.com/beyond/php/article.php/3472551/PHP-Tutorial-Uploading-Files.htm -->
<!-- http://www.htmlgoodies.com/beyond/php/article.php/3472561 -->
<head>
</head>
<body>
<form action="take.php" method="post" enctype="multipart/form-data"><br>
<input type="hidden" name="MAX_FILE_SIZE" value="25000" />
</form>
</body>
</html>
and now the PHP page to handle the form:
<html>
<head>
<title>Process Uploaded File</title>
</head>
<body>
<?php
if ( move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'], "./images/{$_FILES['uploadFile'] ['name']}") ) {
echo '<p> The file has been successfully uploaded </p>';
} else {
switch ($_FILES['uploadFile'] ['error']) {
case 1:
print '<p> The file is bigger than this PHP installation allows</p>';
break;
case 2:
print '<p> The file is bigger than this form allows</p>';
break;
case 3:
print '<p> Only part of the file was uploaded</p>';
break;
case 4:
print '<p> No file was uploaded</p>';
break;
}
}
echo '<br><br>On the first page, you said your name is ' . 'WHAT GOES HERE?';
?>
</body>
</html>
When I load give.php, I can select a file and upload it via the "Upload the file and your name" button.
When I load give.php, I can enter my name and click "Confirm your name" which displays my name as it should.
What I would like to do is:
1. Select a file (but not click the "Upload the file and your name" button).
2. Enter my name and confirm it by clicking the "Confirm your name" button.
3. Click the "Upload the file and your name" and have the file upload and display my name on the second page.
When I follow this sequence, the selected file (after step 1) is cleared when I click the button in step 2 to display my name. Is it possible to arrange for everything to happen in the sequence that I would like? How would I pass the name variable from give.php to take.php?
As I said, it's quite a while since I did any PHP and don't know if what I am trying to do is possible.
Thanks for your time and patience!
I did some HTML and PHP several years ago but then moved towards network security. I'm trying to code an application now and have found some simple code that demonstrates what I'm trying to do (but failing miserably!).
give.php
<html>
<!-- http://www.htmlgoodies.com/beyond/php/article.php/3472551/PHP-Tutorial-Uploading-Files.htm -->
<!-- http://www.htmlgoodies.com/beyond/php/article.php/3472561 -->
<head>
<title>File Upload Form</title>
</head>
<body>
This form allows you to upload a file to the server.<br>
<form action="take.php" method="post" enctype="multipart/form-data"><br>
Type (or select) Filename: <input type="file" name="uploadFile">
<input type="submit" value="Upload the file and your name">
</form>
<form action="" method="post">
What is your name?<br>
<input type="text" name="name">
<input type="submit" value="Confirm your name">
<?php
if (isset($_POST['name'])) {
echo 'You said your name is: '. $_POST['name'];
}
?>
</form>
</body>
</html>
take.php
<html>
<!-- http://www.htmlgoodies.com/beyond/php/article.php/3472551/PHP-Tutorial-Uploading-Files.htm -->
<!-- http://www.htmlgoodies.com/beyond/php/article.php/3472561 -->
<head>
</head>
<body>
<form action="take.php" method="post" enctype="multipart/form-data"><br>
<input type="hidden" name="MAX_FILE_SIZE" value="25000" />
</form>
</body>
</html>
and now the PHP page to handle the form:
<html>
<head>
<title>Process Uploaded File</title>
</head>
<body>
<?php
if ( move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'], "./images/{$_FILES['uploadFile'] ['name']}") ) {
echo '<p> The file has been successfully uploaded </p>';
} else {
switch ($_FILES['uploadFile'] ['error']) {
case 1:
print '<p> The file is bigger than this PHP installation allows</p>';
break;
case 2:
print '<p> The file is bigger than this form allows</p>';
break;
case 3:
print '<p> Only part of the file was uploaded</p>';
break;
case 4:
print '<p> No file was uploaded</p>';
break;
}
}
echo '<br><br>On the first page, you said your name is ' . 'WHAT GOES HERE?';
?>
</body>
</html>
When I load give.php, I can select a file and upload it via the "Upload the file and your name" button.
When I load give.php, I can enter my name and click "Confirm your name" which displays my name as it should.
What I would like to do is:
1. Select a file (but not click the "Upload the file and your name" button).
2. Enter my name and confirm it by clicking the "Confirm your name" button.
3. Click the "Upload the file and your name" and have the file upload and display my name on the second page.
When I follow this sequence, the selected file (after step 1) is cleared when I click the button in step 2 to display my name. Is it possible to arrange for everything to happen in the sequence that I would like? How would I pass the name variable from give.php to take.php?
As I said, it's quite a while since I did any PHP and don't know if what I am trying to do is possible.
Thanks for your time and patience!