AD MANAGEMENT

Collapse

BEHOSTED

Collapse

GOOGLE

Collapse

[PHP] Creating a File Upload Script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [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/7...gorwtan0je.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!!


  • #2
    Re: [PHP] Creating a File Upload Script

    Thanks, i am gonna use it

    Comment

    Unconfigured Ad Widget

    Collapse

    Announcement

    Collapse
    1 of 2 < >

    FreeHostForum Rules and Guidelines

    Webmaster forum - Web Hosting Forum,Domain Name Forum, Web Design Forum, Travel Forum,World Forum, VPS Forum, Reseller Hosting Forum, Free Hosting Forum

    Signature

    Board-wide Policies:

    Do not post links (ads) in posts or threads in non advertising forums.

    Forum Rules
    Posts are to be made in the relevant forum. Users are asked to read the forum descriptions before posting.

    Members should post in a way that is respectful of other users. Flaming or abusing users in any way will not be tolerated and will lead to a warning or will be banned.

    Members are asked to respect the copyright of other users, sites, media, etc.

    Spam is not tolerated here in most circumstances. Users posting spam will be banned. The words and links will be censored.

    The moderating, support and other teams reserve the right to edit or remove any post at any time. The determination of what is construed as indecent, vulgar, spam, etc. as noted in these points is up to Team Members and not users.

    Any text links or images contain popups will be removed or changed.

    Signatures
    Signatures may contain up to four lines

    Text in signatures is subject to the same conditions as posts with respect decency, warez, emoticons, etc.

    Font sizes above 3 are not allowed

    Links are permitted in signatures. Such links may be made to non-Freehostforum material, commercial ventures, etc. Links are included within the text and image limits above. Links to offensive sites may be subject to removal.

    You are allowed ONLY ONE picture(banner) upto 120 pixels in width and 60 pixels in height with a maximum 30kB filesize.

    In combination with a banner/picture you can have ONLY ONE LINE text link.


    Advertising
    Webmaster related advertising is allowed in Webmaster Marketplace section only. Free of charge.

    Shopping related (tangible goods) advertising is allowed in Buy Sell Trade section only. Free of charge.

    No advertising allowed except paid stickies in other sections.

    Please make sure that your post is relevant.


    More to come soon....
    2 of 2 < >

    Advertise at FreeHostForum

    We offer competitive rates and a many kinds of advertising opportunities for both small and large scale campaigns.More and more webmasters find advertising at FreeHostForum.com is a useful way to promote their sites and services. That is why we now have many long-term advertisers.

    At here, we also want to thank you all for your support.

    For more details:
    http://www.freehostforum.com/threads...eHostForum-com

    More ad spots:
    http://www.freehostforum.com/forums/...-FreeHostForum
    See more
    See less
    Working...
    X