Jump to content

my form works once and then stops


karatekid36

Recommended Posts

in this form, i am uploading files to a folder on my server and at the same time logging it into a database so that i can create a page that i can view the files on with some more information besides the file name.  In this form, it works once and then after that does not work the way i had intended for it to function.  On the first try, it moves the file to the folder and logs it properly in the table.  after that, it logs it in the table just fine, but stop uploading the files to the folder.  does any one have any thoughts on this?

 

<?php 
$page_title = 'Upload a File';
include ('./includes/header.html');

$counter = 3; // Number of files to allow for.

if (isset($_POST['submitted'])) { // Handle the form.

require_once ('./includes/mysql_connect.php'); // Connect to the database.

for ($i = 0; $i < $counter; $i++) { // Handle each uploaded file.

	// Create index names to refer to the proper upload and description.
	$filename = 'upload' . $i;
	$description = 'description' . $i;

	// Check for a file.
	if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) {

		// Check for a description (not required).
		if (!empty($_POST[$description])) {
			$d = "'" . escape_data($_POST[$description]) . "'";
		} else {
			$d = 'NULL';
		}

		// Add the record to the database.
		$query = "INSERT INTO paddles_class (file_name, file_size, file_type, description) VALUES ('{$_FILES[$filename]['name']}', {$_FILES[$filename]['size']}, '{$_FILES[$filename]['type']}', $d)";
		$result = mysql_query ($query);

		if ($result) {

			// Return the upload_id from the database.
			$upload_id = mysql_insert_id();

			// Move the file over.
			if (move_uploaded_file($_FILES[$filename]['tmp_name'], "./paddles_class/$upload_id")) {

				echo '<p>File number ' . ($i + 1) . ' has been uploaded!</p>';

			} else { // File could not be moved.

				echo '<p><font color="red">File number ' . ($i + 1) . ' could not be moved.</font></p>';

				// Remove the record from the database.
				$query = "DELETE FROM paddles_class WHERE upload_id = $upload_id";
				$result = mysql_query ($query);

				// Add more detailed error reporting, if desired.

			}

		} else { // If the query did not run OK.
			echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font></p>'; 
			// Print the query and invoke the mysql_error() function to debug.
		}

	} // End of if (isset($the_file)...

} // End of FOR loop.

mysql_close(); // Close the database connection.

} // End of the main Submit conditional.
?>
<form enctype="multipart/form-data" action="add_file.php" method="post">

<fieldset><legend>Fill out the form to upload a file:</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="524288">

<?php // Create the inputs.
for ($i = 0; $i < $counter; $i++) {
	echo '<p><b>File:</b> <input type="file" name="upload' . $i . '" /></p>
<p><b>Description:</b> <textarea name="description' . $i . '" cols="40" rows="5"></textarea></p><br />
';
}
?>

</fieldset>
<input type="hidden" name="submitted" value="TRUE" />
<div align="center"><input type="submit" name="submit" value="Submit" /></div>

</form>
<?php
include ('./includes/footer.html');
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.