You are on page 1of 6

File Upload Script ================== <html> <head> <title>A File Upload Script</title> </head> <body> <div> <?

php if ( isset( $_FILES['fupload'] ) ) { print print print print print "name: ". $_FILES['fupload']['name'] ."<br "size: ". $_FILES['fupload']['size'] ." bytes<br "temp name: ".$_FILES['fupload']['tmp_name'] ."<br "type: ". $_FILES['fupload']['type'] ."<br "error: ". $_FILES['fupload']['error'] ."<br />"; />"; />"; />"; />";

if ( $_FILES['fupload']['type'] == "image/gif" ) { $source = $_FILES['fupload']['tmp_name']; $target = "upload/".$_FILES['fupload']['name']; move_uploaded_file( $source, $target );// or die ("Couldn't copy"); $size = getImageSize( $target ); $imgstr = "<p><img width=\"$size[0]\" height=\"$size[1]\" "; $imgstr .= "src=\"$target\" alt=\"uploaded image\" /></p>"; print $imgstr; } } ?> </div> <form enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF']?>" method="post"> <p> <input type="hidden" name="MAX_FILE_SIZE" value="102400" /> <input type="file" name="fupload" /><br/> <input type="submit" value="upload!" /> </p> </form> </body> </html>

File Uploader ============== <html> <head> <title>File Uploader</title> </head> <body> <h3>File Upload</h3> Select a file to upload:<br> <form action="uploader.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" size="45">

<br> <input type="submit" value="Upload File"> </form> </body> </html> File: uploader.php

<?php if( $_FILES['file']['name'] != "" ) { copy ( $_FILES['file']['tmp_name'], "C:/Apache/htdocs/" . $_FILES['file']['name'] ) or die( "Could not copy file" ); } else { die( "No file specified" ); } ?> <html> <head> <title>Upload complete</title> </head> <body> <h3>File upload <ul> <li>Sent: <?php <li>Size: <?php <li>Type: <?php </ul> succeeded...</h3> echo $_FILES['file']['name']; ?></li> echo $_FILES['file']['size']; ?> bytes</li> echo $_FILES['file']['type']; ?></li>

<a href="<?php echo $_FILES['file']['name']; ?>">Click here to view file</a> </body> </html>

File Uploading Error Constants in PHP ===================================== UPLOAD_ERR_OK No error occurred. UPLOAD_ERR_INI_SIZE in the php.ini file. The uploaded file exceeds the maximum value specified

UPLOAD_ERR_FORM_SIZE The uploaded file exceeds the maximum value specified by the MAX_FILE_SIZE hidden widget.

UPLOAD_ERR_PARTIAL e was uploaded. UPLOAD_ERR_NOFILE

The file upload was canceled and only part of the fil No file was uploaded.

Processing an uploaded file ================================ <?php $maxsize=28480; if (!$HTTP_POST_VARS['submit']) { $error=" "; } if (!is_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name']) AND !isset($e rror)) { $error = "<b>You must upload a file!</b><br /><br />"; unlink($HTTP_POST_FILES['upload_file']['tmp_name']); } if ($HTTP_POST_FILES['upload_file']['size'] > $maxsize AND !isset($error)) { $error = "<b>Error, file must be less than $maxsize bytes.</b><br /><br />"; unlink($HTTP_POST_FILES['upload_file']['tmp_name']); } if (!isset($error)) { move_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name'], "uploads/".$HTTP_POST_FILES['upload_file']['name']); print "Thank you for your upload."; exit; } else { echo ("$error"); } ?> <html> <head></head> <body> <form action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF']))?>" method="post" enctype="multipart/form-data"> Choose a file to upload:<br /> <input type="file" name="upload_file" size="80"> <br /> <input type="submit" name="submit" value="submit"> </form> </body> </html>

Simple File Upload Form ======================== <html> <head> <title>A Simple File Upload Form</title> </head>

<body> <form enctype="multipart/form-data" action="<?print $_SERVER['PHP_SELF']?>" method="post"> <p> <input type="hidden" name="MAX_FILE_SIZE" value="102400" /> <input type="file" name="fupload" /><br/> <input type="submit" value="upload!" /> </p> </form> </body> </html>

Authenticate user: Database based ================================ <?php function authenticate_user() { header('WWW-Authenticate: Basic realm="Secret Stash"'); header("HTTP/1.0 401 Unauthorized"); exit; } if (! isset($_SERVER['PHP_AUTH_USER'])) { authenticate_user(); } else { mysql_pconnect("localhost","authenticator","secret") or die("Can't connect to database server!"); mysql_select_db("java2s") or die("Can't select authentication database!"); $query = "SELECT username, pswd FROM user WHERE username='$_SERVER[PHP_AUT H_USER]' AND pswd=MD5('$_SERVER[PHP_AUTH_PW]')"; $result = mysql_query($query); // If nothing was found, reprompt the user for the login information. if (mysql_num_rows($result) == 0) { authenticate_user(); } } ?> Login form with Error Messages and Preserving User Input ================================================================ <?php function validate_user ($username, $password){

return true; } // create empty array to store error messages $errors = array(); $p =& $_POST; if (count ($p) > 0){ if (!isset ($p['username']) (trim ($p['username']) == '')){ $errors[] = 'You must enter a username.'; }elseif{ ((strlen ($p['username']) < 8) (ereg ('[^a-zA-Z0-9]', $p['usern ame']))){ $errors[] = 'You did not enter a valid username. Usernames must be at least eight characters long and can only contain letters and digits.'; } if (!isset ($p['password']) (trim ($p['password']) == '')){ $errors[] = 'You must enter a password.'; }elseif ((strlen ($p['password']) < 8) (ereg ('[^[:alnum:][:punct:][:spa ce:]]', $p['password']))){ $errors[] = 'You did not enter a valid password. Passwords must be at least eight characters long and can only contain letters, digits, punctuation and spaces.'; } if (count ($errors) == 0) { $r = validate_user ($p['username'], $p['password']); if ($r == false){ $errors[] = 'Login failed. Username/password not found.'; } else { print ('<html><head><title>Congratulations</title></head> <body><h1>Congratulations!</h1><p>You logged in!</p> </body></html>'); exit; } } } ?> <html> <head><title>Login Form</title></head> <body> <h1>Login Form</h1> <?php if (count ($errors) > 0) { $n = count ($errors); for ($i = 0; $i < $n; $i++){ print '<br /><font color="red">' . $errors[$i] . '</font>'; } } ?> <form action="<?php print ($PHP_SELF); ?>" method="POST"> <table> <tr><td>Username:</td> <td><input type="text" name="username" value="<?php if (isset ($p['username '])) print $p['username']; ?>" /></td> </tr> <tr><td>Password:</td> <td><input type="text" name="password" value="<?php if (isset ($p['password

'])) print $p['password']; ?>" /></td> </tr> <tr><td colspan="2"><input type="submit" name="submit"></td></tr> </table> <input type="hidden" name="__process_form__" value="1" /> </form> </body> </html>

Hard code login Form ====================== <HTML> <BODY> <FORM METHOD="POST" ACTION="LoginFormAction.php"> <H2>Login Page</H2> <BR><BR> User Name: <BR><INPUT TYPE="TEXT" NAME="username" SIZE="16"> <BR><BR> Password: <BR><INPUT TYPE="PASSWORD" NAME="password" SIZE="16"> <BR><BR> <BR><BR> <INPUT TYPE="SUBMIT" VALUE="Submit"> </FORM> </BODY> </HTML> <!-- LoginFormAction.php <?php $passwords = array("name1" "name2"

=>"pass1", =>"pass2");

if ($password == $passwords[$username]){ setcookie("username", $username, time()+1200); echo "<H2>Access granted.</H2>"; }else{ setcookie("username", "", time()-3600); echo "<H2>Invalid user name or password: access denied.</H2>"; } ?> -->

You might also like