Copy and paste the following code into a .php file.
In Depth
This script can also be used to upload any kind of files you want. In the script I left in basic examples of common image mimes. A mime is what the server recognizes the file extension as. Below is a a complete list of mimes.
Open Mime List
Upload Dir.
Look for this line of code.
// Where are the files going?
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . "/file_dir/";
Change file_dir to the folder you are uploading the files to. Them CHMOD that folder to 777.
Finished Message
The vary last echo statement before the HTML is what will appear if the file is uploaded. The default in the script is a table that shows the file name, file size, and the file type. You can change this to whatever you want.
HTML Code
The HTML code is what the page will look like when it loads. All the PHP runs in the background. When the form is submited, it runs if_post action, then the upload function.
| Code: |
| <?php
/* Image Upload Sciprt Copyright Pbkill.com Do Not Remove Under Penealty of Law */ // Make the function for upload function do_upload() { // Valid file Mime types / extension $allowed_types = array( "image/gif" => "gif", "image/pjpeg" => "jpg", "image/png" => "png", "image/bmp" => "bmp", "image/jpeg" => "jpg", // Add more types here if you like ); // Check to see if file is an allowed extension if(!array_key_exists($_FILES['userfile']['type'], $allowed_types)) { die("<center><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Invalid file type!</font></center>"); } // Set the maximum file size => 204800 = 200kb $maxfilesize = 204800; // Is it under the allowed Max file size? if($_FILES['userfile']['size'] > $maxfilesize) { die("<center><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">File is too large!</font></center>"); } // Where are the files going? $uploaddir = $_SERVER['DOCUMENT_ROOT'] . "/file_dir/"; // What is the files temporary name? $file = $_FILES['userfile']['tmp_name']; // What is the files actual name? $filename = $_FILES['userfile']['name']; // Check to see if the file allready exists? if(file_exists($uploaddir . $filename)) { die("<center><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">A file with that name already exists on this server.</font></center>"); } else { // If the file does not already exist, copy it. copy($file, $uploaddir.$filename) or die("<center><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">Could not copy file.</font></center>"); } // Put your finished message here /* The following echo statement will output a table showing the file name, size and type */ $fname = $_FILES['userfile']['name']; $fsize = $_FILES['userfile']['size']; $ftype = $_FILES['userfile']['type']; echo "<center><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">File Has Been Uploaded</font><br><br> <table width=\"400\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\"> <tr> <td width=\"156\"><div align=\"right\"><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">You're File:</font></div></td> <td width=\"232\"><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$fname</font></td> </tr> <tr> <td><div align=\"right\"><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">File Size:</font></div></td> <td><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$fsize (In Bytes)</font></td> </tr> <tr> <td><div align=\"right\"><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">File Type:</font></div></td> <td><font color=\"#000000\" size=\"2\" face=\"Verdana, Arial, Helvetica, sans-serif\">$ftype</font></td> </tr> </table> </center>"; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>File Upload</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <form method="post" enctype="multipart/form-data" name="form1"> <input type="hidden" name="action" value="do_upload"> <table width="539" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td width="218"><div align="right"><font color="#000000" size="2" face="Verdana, Arial, Helvetica, sans-serif">File To Upload: </font></div></td> <td width="219"> <font color="#000000"> <input name="userfile" type="file" class="button" id="userfile"> </font></td> <td width="84"><input type="submit" name="Submit" value="Upload File!" class="button"></td> </tr> </table> </form> </body> </html> <?php // If the form has been completed, execute the upload function (above). if($_POST['action'] == "do_upload") { do_upload(); } ?> |
In Depth
This script can also be used to upload any kind of files you want. In the script I left in basic examples of common image mimes. A mime is what the server recognizes the file extension as. Below is a a complete list of mimes.
Open Mime List
Upload Dir.
Look for this line of code.
// Where are the files going?
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . "/file_dir/";
Change file_dir to the folder you are uploading the files to. Them CHMOD that folder to 777.
Finished Message
The vary last echo statement before the HTML is what will appear if the file is uploaded. The default in the script is a table that shows the file name, file size, and the file type. You can change this to whatever you want.
HTML Code
The HTML code is what the page will look like when it loads. All the PHP runs in the background. When the form is submited, it runs if_post action, then the upload function.
