Webmaster Forum  

Go Back   Webmaster Forum > Website Design Forum > Website Design Forum
User Name
Password
Register FAQ Members List Calendar Transactions Store Search Today's Posts Mark Forums Read


[PHP] Creating a File Upload Script

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 07-18-2005, 19:26
admans
Senior Member


Join Date: Jul 2005
Posts: 248
Trader Rating: (0)
Points: 22 (Donate)
5 F$/Referral Refer Friends
admans is on a distinguished road
Default [PHP] Creating a File Upload Script

Recently someone asked for an Upload script, so here you go, out my pure PHP.

First we need to create the database table with the following SQL:
Code:
CREATE TABLE `uploads` (
`id` int(10) unsigned NOT NULL auto_increment,
`whenuploaded` datetime NOT NULL default '0000-00-00 00:00:00',
`ipaddress` varchar(15) NOT NULL default 'unknown',
`imageloc` varchar(255) NOT NULL default 'unknown',
`imagesize` int(10) unsigned default NULL,
`imagetype` varchar(30) default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;

Then here's the PHP code you need at the top of the page where you're going to have your upload form. You need to change the database access details at the top and the 'defines' to specify your domain and path names. Create the DEST_DIR directory on the server with 777 permissions.

PHP Code:
<?php

define
('MAX_ALLOWED_FILE_SIZE'1024000);
define("DEST_DIR"'/upload/1/');
define('DEST_PATH''/home/yourcpanelid/public_html' DEST_DIR);
define('DEST_URL''http://yourdomain.com' DEST_DIR);
    
$allowed_types = array("image/gif""image/pjpeg""image/x-png""image/bmp");
    
$dbhost "localhost";
$dbname "yourdb_name";
$dbuser "yourdb_user";
$dbpass "yourdb_password";

$errormessage "Please enter file to be uploaded.";
    
if ((isset(
$_REQUEST['form_submit'])) && ('form_uploader' == $_REQUEST['form_submit']))
{
    
$picfile_name $_FILES['picfile']['name'];
    
$picfile_type $_FILES['picfile']['type'];
    
$picfile_size $_FILES['picfile']['size'];
    
$picfile_temp $_FILES['picfile']['tmp_name'];
        
    if (
MAX_ALLOWED_FILE_SIZE >= $picfile_size)
    {
        if (
in_array($picfile_type$allowed_types))
    {
        if (
is_uploaded_file($_FILES['picfile']['tmp_name']))
        {

            if (
file_exists(DEST_PATH $picfile_name))
            {
                
$unique_id time();
            
$picfile_name $unique_id '_' $picfile_name;
            }
                    
            if (
move_uploaded_file($picfile_tempDEST_PATH $picfile_name))
            {
                
$errormessage "File uploaded as:
<b>" 
DEST_URL $picfile_name "</b>";
                        
                if(
mysql_connect($dbhost$dbuser$dbpass))
            {
                if(
mysql_select_db($dbname))
                {
                
$sql1 "INSERT INTO uploads (whenuploaded, ipaddress, imageloc, imagesize, imagetype) VALUES (";
                
$sql1 .= "'" date("Y-m-d H:i:s") . "',";
                
$sql1 .= "'" $_SERVER['REMOTE_ADDR'] . "',";
                
$sql1 .= "'" DEST_DIR $picfile_name "',";
                
$sql1 .= "" $picfile_size ",";
                
$sql1 .= "'" $picfile_type "')";
                                
                if (!
mysql_query($sql1))
                {
                    
$errormessage .= "
<font color=red><b>Query failed [$sql1].</b></font>"
;
                }
                }
                else
                {
                    
$errormessage .= "
<font color=red><b>Could not select database.</b></font>"
;
                }
            }
            else
            {
                
$errormessage .= "
<font color=red><b>Could not connect to database.</b></font>"
;
            }
            }
            else
            {
            
$errormessage "<b><font color='red'>File upload failed for obscure reasons (error code: " $_FILES['picfile']['error'] . ").</font></b>";
            }
        }
        else
        {
            
$errormessage "<b><font color='red'>No file uploaded.</font></b>";
        }
        }
        else
        {
        
$errormessage "<b><font color='red'>Invalid file type.</font></b>";
        }
    }
    else
    {
        
$errormessage "<b><font color='red'>File too big (maximum size is " MAX_ALLOWED_FILE_SIZE .    ").</font></b>";
    }
    
$_REQUEST['form_submit'] = "";
}

?>

And here's your form, which goes on the same page as the PHP script above.

HTML Code:
<form action="<?php $PHP_SELF ?>" method="post" enctype="multipart/form-data" name="form_uploader" id="form_uploader">
<table border="0" align="center" cellpadding="1" cellspacing="1">
   <tr>
      <td colspan="3">
         <div align="center"><?php echo $errormessage; ?></div>
      </td>
   </tr>
   <tr>
      <td>File:</td>
      <td colspan="2">
         <input name="MAX_FILE_SIZE" type="hidden" value="<?php echo MAX_ALLOWED_FILE_SIZE ?>">
    <input name="picfile" type="file" id="picfile" title="Enter the path to the file you wish to upload, or use the Browse button to get a file selection dialog." size="60" maxlength="250">
    <input name="form_submit" type="hidden" id="form_submit" value="form_uploader">
      </td>
   </tr>
   <tr>
      <td colspan="3">
         <div align="center">
        <input name="Submit" type="submit" title="Press this button to start the upload." value="Upload">
         </div>
      </td>
   </tr>
</form>
__________________

http://img76.imageshack.us/img76/6450/sc2but4ng.gif | http://img495.imageshack.us/img495/...ogorwtan0je.gif | <a href="http://webtools.sc2.info"><img src="http://img129.imageshack.us/img129/8682/sc2webtan7ju.gif" border="1" width="100" alt="Free Webtools for all" /></a> | <a href="http://www.indexrated.com"><img src="http://img398.imageshack.us/img398/4813/listed1pb.gif" border="1" width="100" alt="Index Rated Directory - Rating Quality Sites" /></a>

Get all Games,Apps and Wallpapers Nokia, Samsung and Sony Erricson!!

admans is offline
Reply With Quote
  #2  
Old 07-19-2005, 08:01
xyris
Senior Member


Join Date: Jul 2005
Posts: 104
Trader Rating: (0)
Points: 0 (Donate)
5 F$/Referral Refer Friends
xyris is on a distinguished road
Default Re: [PHP] Creating a File Upload Script

Thanks, i am gonna use it
xyris is offline
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Resources : | Advertise at FHF | itextLink.com| Reseller Hosting| TextDot| iNamePros| Any Webmaster| Web Host| Dep3|


All times are GMT -4. The time now is 06:36.


Powered by: vBulletin
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.