Jump to content
xisto Community
alex198555

Php And Ajax Upload Form? Urgently

Recommended Posts

Hey, guys! I would like to create some upload form using PHP and AJAX for my web-site. This upload form has to make uploads to specified directory which will mentioned in the script file. So, how can I do it? Please, indicate or write some examples. It will be better if the script will have a feature of multiple uploads.

Share this post


Link to post
Share on other sites

I know this uses PHP/html and works just as well. No multiple uploads, though. Try making a workaround using ajax. Too tired to modify further.

Please keep the acknowledgement line as it is.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://forums.xisto.com/no_longer_exists/; <html xmlns="http://forums.xisto.com/no_longer_exists/; xml:lang="en" lang="en"> <head> <title>File upload</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <body> <?php/*+++++++++++++++++++++++++++++++++++++++By Rubikcode 2007. Published under GNU General Public License.To be distributed with all header info (this paragraph/footer text) intact.Modifications are allowed so long as the owner of this script is acknowleged.+++++++++++++++++++++++++++++++++++++++Database settings[Table 1]------------------------| id | date | filename |------------------------+++++++++++++++++++++++++++++++++++++++InstructionsCreate an 'images' folder where this file is located. Prepare the database as shown above.You may modify the below to suit your needs (eg adding a Name field.)Add columns to the database and change the MySQL query data. Also change the HTML form fields at the bottom of the script. Just follow what is already put ther and all should be fine.+++++++++++++++++++++++++++++++++++++++*//*+++++++++++++++++++++++++++++++++++++++++++++++++VARIABLES*/  define('GW_MAXFILESIZE', 32768);	  // 32 KB  define('GW_UPLOADPATH', 'images/');  // Define database constants  define('DB_HOST', 'guitarwars.net;;  define('DB_USER', 'admin');  define('DB_PASSWORD', 'rockit');  define('DB_NAME', 'Table 1');/*++++++++++++++++++++++++++++++++++++++++++++++++++*/  if (isset($_POST['submit'])) {	// Connect to the database	$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);	// Grab the data from the POST	$screenshot = mysqli_real_escape_string($dbc, trim($_FILES['screenshot']['name']));	if (!empty($file)) {	  if (($file_size > 0) && ($file_size <= GW_MAXFILESIZE)) {		if ($_FILES['file']['error'] == 0) {		  // Move the file to the target upload folder		  $target = GW_UPLOADPATH . $file;		  if (move_uploaded_file($_FILES['file']['tmp_name'], $target)) {			// Write the data to the database			$query = "INSERT INTO guitarwars (id, date, filename) VALUES (0, NOW(), '$file')";			mysqli_query($dbc, $query);			// Confirm success with the user			echo '<p>Thanks for adding your new file! It will be reviewed as soon as possible.</p>';			echo '<p>$file</p>';			echo '<p><a href="$_SERVER[\'PHP_SELF\']"><< Back</a></p>';			// Clear the data to clear the form			$file = "";			mysqli_close($dbc);		  }		  else {		   // In case of error			echo '<p class="error">Sorry, there was a problem uploading your file.</p>';		  }		}	  }	  else {		// Another error		echo '<p class="error">The screen shot must be no greater than ' . (GW_MAXFILESIZE / 1024) . ' KB in size.</p>';	  }	  // Try to delete the temporary file	  @unlink($_FILES['file']['tmp_name']);	}	else {	  echo '<p class="error">Please enter all of the information to add your file.</p>';	}  }   // Form found Below?>  <hr />  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">	<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo GW_MAXFILESIZE; ?>" />	<label for="screenshot">File:</label>	<input type="file" id="file" name="file" />	<hr />	<input type="submit" value="Add" name="submit" />  </form><!-- DO NOT DELETE THIS! --><p>Powered by <a href=http://rubikcode.co.nr/>Rubikcode</a>.</body> </html>

Please tell me if it works.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.