B&T's Tips & Scripts
How did you find this site?
  Search engine25 % 
  My webhost27 % 
  Another website29 % 
  Referral13 % 
  Other6 % 
The last vote was cast today
B&T's Tool Box
Password Generation
Length of Password
Include Numbers
Include Uppercase Letters
Include Lowercase Letters
Include Special Characters
Password Encryption for .htpasswd
Username
Password
Hex RGB Color Conversion
Hex #
RGB
Check HTTP status code
enter full url to be checked
Domain Name / IP Resolution
enter Domain Name or IP Address

B&T's Recommendations

Site5.com

Got (or getting) a Mac?

AppleChronicles.com

B&T's Tips & Scripts is a collection of Tips & Scripts to help you build a better website.  The Tips & Scripts are targeted towards the native Apache PHP MySQL environment.  Check back frequently as there will be additions and modifications.  Please check our terms of use.
B&T's Tips & Scripts is a safe website.
No pop-up ads and no evil downloads.
Search Tips & Scripts
as current the latest index of this site
Tag Definitions Date - most recent update
- for advanced users
- topic rating
php5 - requires php5 or later
Have you had enough programming and website building for a while?
Need a break to clear your mind?
Go to PrettyWorthless.com, where you will find nothing of value.


   Using .htaccess  

  1. .htaccess Basics Mar 9, 2006
  2. Domain & subdomain pointing May 12, 2005
  3. Automatic subdomains Jun 17, 2007
  4. Undefined subdomains Jul 20, 2008
  5. Prevent direct access to a pointer or subdomain directory Aug 15, 2006
  6. Forcing or eliminating the WWW. Feb 9, 2005
  7. Fixing the trailing slash problem May 11, 2005
  8. Friendly URLs Mar 21, 2007
  9. Rewrites and https Dec 18, 2007
  10. Blocking traffic to your website Jan 2, 2005
  11. Anti-leech May 25, 2005
  12. Fancy default index page Jan 25, 2005


   Using .htaccess/.htpasswd Password Protection  

  1. Setting up password protection Feb 9, 2005
  2. Encrypt your password for .htpasswd Dec 21, 2007
  3. Auto Password Change and Email Notification Dec 21, 2007 php5
  4. Auto generate .htpasswd file Dec 18, 2007 php5
  5. Passing user authentication information to PHP Jun 13, 2004


   Coding Tips  

  1. HTML "One Liners" Nov 13, 2005
  2. PHP "One Liners" Apr 5, 2007
  3. JavaScript "One Liners" Aug 25, 2007
  4. CSS examples Sep 9, 2006
  5. SQL coding examples Aug 29, 2006
  6. AJAX example Sep 24, 2006


   Uploads and Downloads  

  1. File Upload Script Dec 20, 2007 php5
  2. Custom Download Page Dec 14, 2007 php5
  3. Tracking and Securing Downloads Dec 20, 2007 php5


   Mail  

  1. Form Mail Aug 19, 2007
  2. Obfuscate your email address Nov 12, 2006
  3. Read and auto-process mail Nov 12, 2004
  4. Auto-responder Dec 18, 2007 php5
  5. Display your email message count Jul 30, 2004
  6. Setting up SPF Records Jan 4, 2007
  7. Specify a return-path for generated mail Jul 18, 2006


   Working with Images  

  1. Watermark images Jul 14, 2007
  2. Resize images (create thumbnails) Jul 14, 2007


   Frequently Requested Website Functionality  

  1. Visitor Counter Dec 14, 2007 php5
  2. 404 Error Page (with reporting and logging) Dec 20, 2007 php5
  3. CAPTCHA Aug 19, 2006
  4. Poll (voting) Dec 17, 2007 php5   see it on this page
  5. Weather Jul 27, 2008 php5
  6. FAQ Page May 23, 2005
  7. Random/weighted banners, quotes & more Feb 9, 2005
  8. Load a random image (simple) Jan 20, 2004
  9. Image (banner) rotation Oct 15, 2007
  10. Simple "members only" pages May 13, 2005
  11. Replace banned words Apr 5, 2007
  12. Get the most recent file date Jan 30, 2006
  13. Moving (bouncing) Image Nov 10, 2005
  14. Sticky default values for an input Oct 22, 2006


   Using PHP  

  1. Creating a custom php.ini file Dec 18, 2007 php5
  2. Copying the php.ini file Dec 18, 2007 php5
  3. Deleteing php.ini files Dec 18, 2007 php5
  4. Clean up your PHP scripts Nov 11, 2005
  5. Regular Expression Rules Mar 22, 2006


   Website Managment  

  1. Tips for keeping your website secure Jan 24, 2006
  2. Using Cron Sep 13, 2005
  3. Website Monitoring Dec 19, 2007
  4. Automated Site Backups Dec 19, 2007
  5. Automated MySQL Backups Dec 19, 2007
  6. Server-to-server file transfer December 16, 2006
  7. MySQL Error Notification Dec 20, 2007
  8. Find File and Directory Sizes Dec 14, 2007 php5
  9. Deleting session files Aug 9, 2006
  10. FTP made easy Mar 1, 2006
  11. Search and Replace Dec 14, 2007 php5


   Other Tips  

  1. URL Validation Feb 8, 2008
  2. Stop direct links to web pages Jan 22, 2004
  3. Ensure your page is not opened in a frame May 5, 2005
  4. Masking your URL in the Address Bar Jul 20, 2004
  5. Ban or allow IP Addresses (without using .htaccess) Jan 19, 20008
  6. Using Cookies Sep 24, 2006
  7. View your website in different resolutions Jan 2, 2005
 

Back to Top     Print this Tip

File Upload Script

This is a simple file upload script. It is a good starter example which can be customized to meet your needs.   This script will allow you to specify an upload directory, allowed file types, max file size and max space allowed to be used in the upload directory.   It will even send you an email letting you know there has been an upload to your website and/or add an entry to a log file for each upload.   You need to change the variables at the top of the script as needed.

The specified upload directory can either be a directory relative to the location of the script (in the example the directory "./" is the directory where the script is located, "subdirectory/" would be one directory down from the script) or you can use the full Unix path.   Be sure to include a trailing slash on the directory.

If you want to upload files larger than 2MB there are changes required to the php.ini file.   See the Tip "Creating a custom php.ini file" on this Tips & Scripts page.

You can download this script as a .txt file.  Remember to rename the file as a .php file.

- - Start Script Here - -
<?php
/*
This script can send an email and/or make an entry in a log file
There are two variables below - one for an email address and one for a log file
Set both vraiables to the values you want to use
If you do not want either an email or log entry, comment out the respective line
For example, if you do not want an email sent, put a // in front of the $emailAddress line - same for the $logFile line
*/
$logFile $_SERVER['DOCUMENT_ROOT'].'/upload.log';   // full path to your log file
$emailaddress "mail@yourdomain.com";
$home_page "home.htm";  // used for a link to return
$uploaddir "./";  // the directory where files are to be uploaded - include the trailing slash
$type = array(".jpg",".gif",".txt");  // enter in all lower case, the script will handle a match with upper case
$maxSize 100000;  // maximum file size that can be uploaded - in bytes
$maxFileSpace 50000000;  // maximum space that can be used by files matching the $type array in the upload directory - in bytes
putenv('TZ=EST5EDT'); // eastern time
// change nothing below this line
$maxDisplay $maxSize 1000;
?>
<html><head></head><body>
<div style="text-align: center; margin: 100px auto; border: 1px black solid; width:400px;">
<?php
// print_r($_FILES);  // can be used for debugging
$file_name $_FILES['file']['name'];
$file_size $_FILES['file']['size'];
$file_tmp_name $_FILES['file']['tmp_name'];
if (!empty(
$file_name)) {
  unset(
$error);
  echo 
"<br>File Name: $file_name<br><br>"
  echo 
"File Size: $file_size bytes<br><br>";
  
// file size test
  
if ($file_size == $error .= "<span style='color: red;'>Invalid file</span><br>";
  if (
$file_size $maxSize $error .= "<span style='color: red;'>Your file exceeds $maxDisplay K.</span><br>";
  
// file type test
  
if (!in_array(strtolower(strstr($file_name'.')),$type) ) $error .= "<span style='color: red;'>Your file is not a valid file type.</span><br>";
  
// max directory size test
  
foreach(scandir($uploaddir) as $file_select) if (in_array(strtolower(strstr($file_select'.')),$type)) $total_size $total_size filesize("$uploaddir$file_select");
  if ((
$total_size+$file_size) >= $maxFileSpace)  $error .= "<span style='color: red;'>Total file space limits have been exceeded.</span><br>";
  
// scrub characters in the file name
  
$file_name stripslashes($file_name);
  
$file_name preg_replace("#[ ]#","_",$file_name);  // change spaces to underscore
  
$file_name preg_replace('#[^()\.\-,\w]#','_',$file_name);  //only parenthesis, underscore, letters, numbers, comma, hyphen, period - others to underscore
  
$file_name preg_replace('#(_)+#','_',$file_name);  //eliminate duplicate underscore
  // check for file already exists
  
if (file_exists($uploaddir.$file_name)) $error .= "<span style='color: red;'>File already exists.</span><br>";
  
// if all is valid, do the upload
  
if (empty($error)) {
    if (
move_uploaded_file($file_tmp_name,$uploaddir.$file_name)) {
      
chmod($uploaddir.$file_name,0644);
      echo 
"<span style='color: green;'>Your file was successfully uploaded!</span>";
      if (isset(
$emailAddress)) {
        
$message $file_name " was uploaded by" $_SERVER['REMOTE_ADDR'] . "at" date('Y-m-d H:i:s'); 
        
mail($emailaddress,"You have a file upload",$message,"From: Website <>");
      }
      if (isset(
$logFile)) {
        
$logData $file_name "||" $_SERVER['REMOTE_ADDR'] . "||" date('Y-m-d H:i:s') . "\r\n";
        @
file_put_contents($logFile,$logData,FILE_APPEND|LOCK_EX);
      } 
    } else {
      echo 
"<span style='color: red;'>Your file could not be uploaded.</span>";
    }
  }
  echo 
"$error<hr>";
}
?>
<p>Upload a <span style="color: blue;">
<?php
foreach($type as $print_type) echo $print_type;
?>
</span> file to our server<br>
Maximum file size is <?php echo $maxDisplay?>K</p>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" enctype="multipart/form-data">
File: <input type="file" name="file" style="width: 250px;"><br>
<input type="submit" name="submit" value="Upload File"></form>
<a href="<?php echo $home_page?>">Return to the Home Page</a>
</div></body></html>

- - End Script Here - -

Rate This Topic



 
You are visitor 446,208
Last week there were 4,264 visitors
Copyright ©2004-2008