
[PHP] Simple Image Uploader [PHP]
First of, this uploader will only upload .jpg .jpeg .bmp .png .gif files "Ill show you how to change them later"
First things first, Create a Dir on your site call it "uploaded_files"
Now to make it so it can be uploaded to, edit the CHMOD to 777
now you've done that bit

time to create the form,
Make a new PHP file and call it " index "
Copy(CTRL + C) and Paste (CTRL + V) this code into it
Code:
<form action="uploader.php" method="post" enctype="multipart/form-data">
<div align="center"><label for="file">Select a file:</label> <input type="file" name="userfile" id="file"></div> <br/>
<div align="center">File types allowed: .jpg .jpeg .bmp .png .gif</div>
<div align="center">Max file size: 4.0 MB</div>
<div align="center"><input type="submit" name="submit" value="Upload" /></div>
</form>
Save this file and dont edit anything unless you chage the max file size later.
Now create another PHP file called " uploader "
Copy & Paste this into the file
Code:
<?
// Config upload options
define("ALL_LOWERCASE", true); // Fixes a bug with capitalised file extensions
$allowed_filetypes = array('.jpg', '.jpeg','.gif','.bmp','.png', '.ico','.JPG','.PNG','.JPEG','.GIF','.ICO','.BMP',); // These will be the types of file that will pass the validation.
$max_filesize = 4194304; // Maximum filesize in BYTES (currently 4.0MB).
$upload_path = 'upload/'; // The place the files will be uploaded to (currently a 'upload' directory, remember it needs to be CHMOD 777).
$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
// Checks file extension thats requested to be uploaded compared to the allowed_filetypes array, if allowed continue. If not on the allow list, DISPLAY ERROR 1
if(!in_array($ext,$allowed_filetypes))
die('<h3>OOPS!</h3>
<p>The file you attemped to upload is not allowed, make sure the file is on the allowed list and try again.</p>'); // ERROR 1
// Checks file size to make sure it is below the maximum set by the max_filesize array. If under set value (Currently 2.0 MB) continue. If larger then set value, DISPLAY ERROR 2
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('<h3>OOPS!</h3>
<p>The image you attempted to upload is too large.</p>'); // ERROR 2
// Confirms the existance and access to the upload directory (Where uploads will be stored) if directory set is writable upload will process. If directory set doesn't exist or isn't accesible, DISPLAY PROCESS ERROR 1
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.'); // PROCESS ERROR 1
// Uploader will upload the requested file to the path given.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
// Upload successful, final step, generate embed codes. (My URL structure e.g. http://www.yoursite.net/ is present in this code at the moment as in order to test out the uploader and embed codes yourself (For the live preview) I've set it up fully. Edit them to your website url and directories etc.. when you set it up on your own web-server
echo '<h3>The Upload was successful</h3>
<p><strong>Your image upload of <em>'. $filename . '</em> was successful:</strong></p>
<img src="http://www.yoursite.net/'.$upload_path.$filename.' " "alt="uploaded image"/>
<p><strong>Your uploaded image is now live on our servers, scroll down below to obtain the URL to your file. Optionally you can use one of the embed methods which have been generated for you.</strong></p>
<table border="0" cellspacing="10" cellpadding=0">
<tr>
<td>If you just need the general link to your uploaded image then use this code below:</td>
</tr>
<tr>
<td><strong>Direct link to image</strong>:<br/> <textarea cols="35" rows="3">http://www.yoursite.net/'.$upload_path.$filename.'</textarea></td>
</tr>
</table>
<table border="0" cellspacing="10" cellpadding="0">
<tr>
<td>If you have uploaded this image and need it embedding in a forum post then use the codes generated below!</td>
</tr>
<tr>
<td><strong>Embedding the image in a forum:</strong><br/> <textarea cols="35" rows="3">[IMG]http://www.yoursite.net/'.$upload_path.$filename.'[/IMG]</textarea></td>
</tr>
</table>
<table border="0" cellspacing="10" cellpadding="0">
<tr>
<td>If you want to make your images have a hyperlink on a forum or website then use these codes:</td>
</tr>
<tr>
<td>
<strong>Hotlink for forums 1</strong>:<br/> <textarea cols="35" rows="3">[URL=http://consumingfire.site50.net[IMG=http://www.yoursite.net/'.$upload_path.$filename.'[/IMG][/URL]</textarea></td>
</tr>
<tr>
<td>
<strong>Hotlink for forums 2</strong>:<br/> <textarea cols="35" rows="3">[url=http://consumingfire.site50.net][img=http://www.yoursite.net/'.$upload_path.$filename.'[/img][/url]</textarea></td>
</tr>
<tr>
<td>
<strong>Hotlink for Websites</strong>:<br/> <textarea cols="35" rows="3"><a href="http://consumingfire.site50.net"><img src="http://www.yoursite.net/'.$upload_path.$filename.'" border="0" alt="image hosted at Pulse GFX"/></a></textarea></td>
</tr>
</table>
';
else
echo '<h3>OOPS</h3>
<p><strong>There was an error during the image upload.</strong></p>'; // PROCESS ERROR 2 :(.
?>
Not the parts that say
Code:
http://www.yoursite.net
Just change them all to your site
to edit the files change the following line
Code:
$allowed_filetypes = array('.jpg', '.jpeg','.gif','.bmp','.png', '.ico','.JPG','.PNG','.JPEG','.GIF','.ICO','.BMP',);
ad whatever
DONT FORGET ITS '.zip', or '.rar', ect ectTo change the file size edit this line
Code:
$max_filesize = 4194304; // Maximum filesize in BYTES (currently 4.0MB).
To the file size Eg
2097152 = 2.0MB
One you done all that upload it to your site!