Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Help with editing an PHP file upload script

    Hi all, I have an existing script below that was written for me by a developer who has since left the project but I'm trying to tweak it to add an additional file upload.

    Basically, this is a submit file for a form that allows users to upload 1 single photo (with "name" and "category" being the other fields), so with each record has associated with it 1 photo. But I now need to change it so the user is required to upload 2 photos instead of 1. I added the new field on the form itself but now not sure what I need to change on the submit file to make this work.

    I'm not a developer so I would appreciate any help on this if possible.

    Thanks in advance, much appreciated.


    PHP Code:


    if ( $_SERVER['REQUEST_METHOD']    = 'POST')
    {
        
    $_SESSION        array_merge($_SESSION$_POST);

        
    // check if max quantity is reached
        
    $dbh = new dbh(    DB_USERDB_PASSDB_NAMEDB_HOST);
        
    $result $dbh->query("SELECT COUNT(1) FROM battle_user_arsenal WHERE uid='$user_id'");
        
    $items_nb        mysql_result($result0);
        
    set_flashIf(BATTLE_ADD_WEAPONS_LIMIT && $items_nb >= BATTLE_ADD_WEAPONS_LIMITsprintf('You cannot add more than %s photos.'BATTLE_ADD_WEAPONS_LIMIT), 'error');

        
    set_flashIf(empty($_POST['photo_name']), 'Please provide a name for your photo.''error');
        
    set_flashIf(empty($_POST['photo_class']), 'Please select a category for your photo.''error');
        
    set_flashIf(empty($_SESSION['photo_file']) && !is_uploaded_file($_FILES['photo_file']['tmp_name']), 'The photo file is required.''error');


        
    // if above is ok then check file
        
    if ( is_uploaded_file($_FILES['photo_file']['tmp_name']))
           {
                 if ( 
    in_array($_FILES['photo_file']['type'], array('image/jpeg''image/pjpeg''image/jpg''image/png''image/gif')))
                    {
                
    // all ok then try to upload
                
    $saveFileName            md5(microtime()). '.'pathinfo($_FILES['sneaker_file']['name'], PATHINFO_EXTENSION);
                if( 
    move_uploaded_file($_FILES['photo_file']['tmp_name'], UPLOAD_DIR_PATH .'/'$saveFileName))
                {
                    
    // check if file exist though, "move_uploaded_file" doesn't work 100%
                    
    if( is_fileUPLOAD_DIR_PATH .'/'$saveFileName))
                    {
                        
    //  remove previous & thumbs
                        
    if ( isset($_SESSION['photo_file'] ) && is_fileUPLOAD_DIR_PATH.'/'.$_SESSION['photo_file']))
                        {
                            
    $thumbs        glob(UPLOAD_DIR_PATH.'/*'.$_SESSION['photo_file']);
                            
    array_map('unlink', (array) $thumbs );
                        }

                        
    $_SESSION['photo_file']    = $saveFileName;
                    }
                    else
                    {
                        
    set_flash('Photo File upload failed, try again''error');
                    }


                }
                else
                {
                    
    set_flash('Photo File upload failed, try again''error');
                }
            }
            else
            {
                
    set_flash('Photo File is invalid type.''error');
            }
        }

        if( 
    flash_exists('error'))
        {
            
    redirectbattle_add_weapon.php');
        }
        else
        {
            redirect( battle_add_weapon_preview.php'
    );
        }



  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,539
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    On your form, what is the name of both variables (for both files)?
    Better yet, post your HTML form where they enter the photos.

  • Users who have thanked mlseim for this post:

    SneakerHead (03-09-2010)

  • #3
    New Coder
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks so much for helping mlseim. I have tried to change simplify some of the code so as not to confuse people on here but I guess copying and pasting exactly what I have might be better. (Although there are some other stuff in there that applies to other unrelated functionality). Just to provide some background. This is a facebook application that allows users to upload images and to store in their "arsenal" and then they can pin their photos against other peoples' "arsenals" in "battles". The battles are essentially voting contests.

    Thanks again for lending a hand, always grateful.

    The form page:
    PHP Code:

    <?php
    require('config.php');
    require(
    'includes/facebook.php');
    require(
    'includes/helpers.php');
    require(
    'includes/dbh.php');

    $fb = new Facebook(FB_API_KEYFB_SECRET);
    $fb->require_frame();
    $user_id $fb->require_login();

    if( ! 
    $fb->api_client->users_isAppAdded()) {
        
    $fb->redirect($fb->get_add_url());
    }

    // start session
    session_id($user_id '-'FB_APP_NAME);
    session_start();

    $weapon        = array(
        
    'sneaker_name' => ''
        
    'sneaker_class' => '',
        
    'sneaker_file' => '',
    );

    $weapon        array_merge($weapon$_SESSION );

    $dbh = new dbh(    DB_USERDB_PASSDB_NAMEDB_HOST);

    $result                         $dbh->query("SELECT COUNT(1) FROM received_gifts WHERE uid1='$user_id'");
    $received_gifts_nb    mysql_result($result);

    $user_points $dbh->get_object("SELECT * FROM user_points WHERE uid='$user_id'"false);
    ?>



    <?php include('css.php'); ?>
    <?php 
    include('battle_css.php'); ?>


    <div id="content"><?php include('tab_menu.php'); ?>

    <div id="background"><!--Top Ad Banner--> <?php if (SERVER_ENVIRONMENT == "PROD") : ?>
    <?php 
    include('top_banner_ad.php'); ?> <?php endif; ?> <!----------------->

    <table align="center" width="100%" cellspacing="0" cellpadding="0"
        class="main_table">
        <!-------------New Top Layout with banner---------->
        <?#php include('index_top_row.php'); ?>
        <!-------------------------------------------------->
        <tr>
            <td><?php include('battle_controls.php'?></td>
        </tr>


        <tr>
            <td class="send_gift_td">



            <table cellspacing="0" width="100%" cellpadding="0" border="0">
                <tr>
                    <td width="100%" valign="top">
                    <div class="white_box_showdown">
                    <?php if ( flash_exists'error')) : ?>
                    <div class="notice"><?php echo get_flash('error'); ?></div>

                    <?php endif; ?>

                    <form onsubmit="showProgress()" 
                        action="<?php echo ABSOLUTE_HTTP_HOST ?>/battle_add_weapon_submit.php"
                        method="post" enctype="multipart/form-data">
                    <table class="add_weapon_table">

                        <tr>
                        <td>
                          <!--<img src="<?=HTTP_HOST.ROOT.IMG_DIR.DIR_SEP?>battle_upload_sneaker.png" />-->
                          
                          
                            <h3 class="battle_heading">Upload photos to use in showdowns!<br><span style="font-size: 12px; color: #808080;">Upload pictures of your remodel and renovation designs, select the appropriate category, .jpg .gif .png formats only please. (Any inappropriate photos will be removed)</span></h3>
                            </td>
                        </tr>
                        <tr>
                            <td>
                            <div id="indicator">
                            Please wait, loading.<br><br> <img src="<?=HTTP_HOST.ROOT.IMG_DIR.DIR_SEP?>big_loading.gif" />
                            </div>
                            
                            <div id="upload_form">
                                <table class="add_weapon_table">
                                <tr>
                                <td class="formTitles">Give this remodel a description (i.e. "Kim's new country kicten"):</td>
                                <td width="300">
                  
                  <textarea type="text" rows="2" cols="20" name="sneaker_name"
                                    value="<?php echo $weapon['sneaker_name'?>" /></textarea>
                    
                    </td>
                                </tr>
                                <tr>
                                    <td class="formTitles">Select a category:</td>
                                    <td><select name="sneaker_class">
                                        <?php foreach((array('' => '-- SELECT ONE --')+$battleClasses) as $key => $value): ?>
                                        <option value="<?php echo $key ?>"<?php if($weapon['sneaker_class'] == $key): ?> selected="selected"<?php endif; ?>><?php echo $value ?></option>
                                            <?php endforeach; ?>
                                    </select></td>
                                </tr>
                                <tr>
                                    <td class="formTitles">Select your BEFORE photo:</td>
                                    <td><input type="file" name="sneaker_file" /></td>
                                </tr>
                            <!---- need functinoality to upload AFTER photo as a set-->    
                 <tr>
                                    <td class="formTitles">Select your AFTER photo:</td>
                                    <td><input type="file" name="sneaker_file2" /></td>
                                </tr>
                                    <!----------------------------------------------------->    
                                
                                <tr>
                                    <td></td>
                                    <td>
                                    <a href="<?php echo FB_APP_URL.'/battle_my_arsenal.php' ?>"><img src="<?php echo HTTP_HOST.'/images/button_go_back.png' ?>" /></a>
                                    <input type="image" src="<?php echo HTTP_HOST.'/images/button_upload.png' ?>" name="add" value="Upload" /></td>
                                </tr>
                                </table>
                            </div>
                            
                            
                            </td>
                        </tr>
                    </table>
                    </form>


                    </div>
                    </td>
                    <td valign="top">
                
                    </td>
                </tr>
            </table>
            </td>
        </tr>



    </table>
    </div>


    <script>
    function showProgress() {
        document.getElementById("indicator").setStyle({"display": "block"});
        document.getElementById("upload_form").setStyle({"display": "none"});
    }
    </script>
    The submit page:

    PHP Code:

    <?php
    require('config.php');
    require(
    'includes/facebook.php');
    require(
    'includes/helpers.php');
    require(
    'includes/dbh.php');

    error_reporting(E_ALL);
    set_time_limit);

    $fb = new Facebook(FB_API_KEYFB_SECRET);
    $user_id $fb->require_login();

    // start session
    session_id($user_id '-'FB_APP_NAME);
    session_start();

    if ( 
    $_SERVER['REQUEST_METHOD']    = 'POST')
    {
        
    $_SESSION        array_merge($_SESSION$_POST);

        
    // check if max quantity is reached
        
    $dbh = new dbh(    DB_USERDB_PASSDB_NAMEDB_HOST);
        
    $result $dbh->query("SELECT COUNT(1) FROM battle_user_arsenal WHERE uid='$user_id'");
        
    $items_nb        mysql_result($result0);
        
    set_flashIf(BATTLE_ADD_WEAPONS_LIMIT && $items_nb >= BATTLE_ADD_WEAPONS_LIMITsprintf('You cannot add more than %s photos.'BATTLE_ADD_WEAPONS_LIMIT), 'error');

        
    set_flashIf(empty($_POST['sneaker_name']), 'Please provide a name for your photo.''error');
        
    set_flashIf(empty($_POST['sneaker_class']), 'Please select a category for your photo.''error');
        
    set_flashIf(empty($_SESSION['sneaker_file']) && !is_uploaded_file($_FILES['sneaker_file']['tmp_name']), 'The BEFORE photo file is required.''error');


        
    // if above is ok then check file
        
    if ( is_uploaded_file($_FILES['sneaker_file']['tmp_name']))
           {
                 if ( 
    in_array($_FILES['sneaker_file']['type'], array('image/jpeg''image/pjpeg''image/jpg''image/png''image/gif')))
                    {
                
    // all ok then try to upload
                
    $saveFileName            md5(microtime()). '.'pathinfo($_FILES['sneaker_file']['name'], PATHINFO_EXTENSION);
                if( 
    move_uploaded_file($_FILES['sneaker_file']['tmp_name'], UPLOAD_DIR_PATH .'/'$saveFileName))
                {
                    
    // check if file exist though, "move_uploaded_file" doesn't work 100%
                    
    if( is_fileUPLOAD_DIR_PATH .'/'$saveFileName))
                    {
                        
    //  remove previous & thumbs
                        
    if ( isset($_SESSION['sneaker_file'] ) && is_fileUPLOAD_DIR_PATH.'/'.$_SESSION['sneaker_file']))
                        {
                            
    $thumbs        glob(UPLOAD_DIR_PATH.'/*'.$_SESSION['sneaker_file']);
                            
    array_map('unlink', (array) $thumbs );
                        }

                        
    $_SESSION['sneaker_file']    = $saveFileName;
                    }
                    else
                    {
                        
    set_flash('Photo File upload failed, try again''error');
                    }


                }
                else
                {
                    
    set_flash('Photo File upload failed, try again''error');
                }
            }
            else
            {
                
    set_flash('Photo File is invalid type.''error');
            }
        }

        if( 
    flash_exists('error'))
        {
            
    redirectFB_APP_URL.'/battle_add_weapon.php');
        }
        else
        {
            
    redirectFB_APP_URL.'/battle_add_weapon_preview.php');
        }

    }

    The preview page:

    PHP Code:

    <?php
    require('config.php');
    require(
    'includes/facebook.php');
    require(
    'includes/helpers.php');
    require(
    'includes/dbh.php');

    $fb = new Facebook(FB_API_KEYFB_SECRET);
    $user_id $fb->require_login();

    // start session
    session_id($user_id '-'FB_APP_NAME);
    session_start();

    $weapon        = array(
        
    'sneaker_name' => ''
        
    'sneaker_class' => '',
        
    'sneaker_file' => '',
    );

    $weapon        array_merge($weapon$_SESSION );

    $dbh = new dbh(    DB_USERDB_PASSDB_NAMEDB_HOST);

    $result                         $dbh->query("SELECT COUNT(1) FROM received_gifts WHERE uid1='$user_id'");
    $received_gifts_nb    mysql_result($result);

    $user_points $dbh->get_object("SELECT * FROM user_points WHERE uid='$user_id'"false);

    ?>
    <?php 
    include('css.php'); ?>
    <?php 
    include('battle_css.php'); ?>


    <div id="content"><?php include('tab_menu.php'); ?>

    <div id="background"><!--Top Ad Banner--> <?php if (SERVER_ENVIRONMENT == "PROD") : ?>
    <?php 
    include('top_banner_ad.php'); ?> <?php endif; ?> <!----------------->

    <table align="center" width="100%" cellspacing="0" cellpadding="0"
        class="main_table">
        <tr>
            <td><?php include('battle_controls.php'?></td>
        </tr>


        <tr>
            <td class="send_gift_td">

            <table cellspacing="0" width="100%" cellpadding="0" border="0">
                <tr>
                    <td width="100%" valign="top">
                    <div class="white_box">
                    
                    <form action="<?php echo FB_APP_URL ?>/battle_add_weapon_save.php" method="post">
                    <table class="add_weapon_table" width="100%">
                        <tr>
                            <td colspan="2">
                            <h3 class="battle_heading">Preview Your Weapon</h3>
                            </td>
                        </tr>
                        
                        <tr>
                            <td>
                            
                                <table>
                                    <tr>
                                        <td valign="top">Description:</td>
                                        <td><?php echo $weapon['sneaker_name'?></td>
                                    </tr>
                                    <tr>
                                        <td>Category:</td>
                                        <td><?php echo $battleClasses[$weapon['sneaker_class']] ?></td>
                                    </tr>
                                </table>    
                            </td>
                            
                            <td style="text-align: right;">
                            <img src="<?php echo HTTP_HOST'/'UPLOAD_DIRNAME.'/100-75_'.$weapon['sneaker_file'?>" />
                            </td>
                            
                        </tr>
                        
                        <tr>
                            <td colspan="2" style="text-align: right; border-top: 1px solid #C5CDE0;">
                            
                                <table style="text-align: right;" align="right">
                                    <tr>
                                        <td><h3>Are you sure?</h3></td>
                                        <td><a href="<?php echo FB_APP_URL.'/battle_add_weapon.php'?>"><img src="<?php echo HTTP_HOST.'/images/button_go_back.png' ?>" /></a></td>
                                        <td><input type="image" src="<?php echo HTTP_HOST.'/images/button_upload.png' ?>" name="add" value="Yes" /></td>
                                    </tr>
                                </table>
                            
                            </td>
                        </tr>
                        
                    </table>
                </form>

                    </div>
                    </td>
            
                </tr>
            </table>
            </td>
        </tr>



    </table>
    </div>
    The Save page:

    PHP Code:

    <?php
    require('config.php');
    require(
    'includes/facebook.php');
    require(
    'includes/helpers.php');
    require(
    'includes/dbh.php');

    $fb = new Facebook(FB_API_KEYFB_SECRET);
    $user_id $fb->require_login();

    // start session
    session_id($user_id '-'FB_APP_NAME);
    session_start();

    if ( 
    $_SERVER['REQUEST_METHOD']    = 'POST')
    {
        
    set_flashIf(empty($_SESSION['sneaker_name']), 'Sneaker name is required.''error');
        
    set_flashIf(empty($_SESSION['sneaker_class']), 'Sneaker class is required.''error');
        
    set_flashIf(empty($_SESSION['sneaker_file']), 'Sneaker file is required.''error');


        if( 
    flash_exists('error'))
        {
            
    $fb->redirectFB_APP_URL.'/battle_add_weapon.php');
        }
        else 
        {
            
    $_SESSION        addslashes_deep($_SESSION);
            
            
    $sneaker_name            $_SESSION['sneaker_name'];
            
    $sneaker_class        $_SESSION['sneaker_class'];
            
    $sneaker_file            $_SESSION['sneaker_file'];
            
            
    $dbh = new dbh(    DB_USERDB_PASSDB_NAMEDB_HOST);
            
    $result $dbh->query("INSERT INTO battle_user_arsenal (uid, sneaker_name, sneaker_class, sneaker_file)
                                        VALUES('$user_id', '$sneaker_name', '$sneaker_class', '$sneaker_file')"
    );
            
    // save data now
            
    if( $dbh->affected_rows 
            {
                
    $_SESSION    = array();
                
    // charge for pointa
                
    $add_fee        BATTLE_ADD_WEAPON_COST;
                
    $dbh->query("UPDATE user_points SET points=points-'$add_fee' WHERE uid='$user_id'");
                
    $fb->redirectFB_APP_URL.'/battle_my_arsenal.php?show_feed=1');
            }
            else
            {
                
    set_flash('Sneaker has not been save, try again''error');
                
    $fb->redirectFB_APP_URL.'/battle_add_weapon.php');
            }
        }

    }

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,539
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    There's one MySQL thing you'll have to do ... but not sure if you know how to do it ...

    Before you do anything, you need to add another variable to your MySQL table.
    The table called: battle_user_arsenal
    You currently have, sneaker_file
    You also have to add, sneaker_file2
    Add the new "column" or "variable" with the same parameters as "sneaker_file".
    This is something you have to do ... as we don't have access to your MySQL database.

    Make a safe copy of all your files (in a separate place) before overwriting
    them with these. Very hard to do this without testing anything ... but I'm
    taking a shot at it. This won't work until you have added "sneaker_file2"
    to your MySQL database.

    My form page:
    PHP Code:

    <?php
    require('config.php');
    require(
    'includes/facebook.php');
    require(
    'includes/helpers.php');
    require(
    'includes/dbh.php');

    $fb = new Facebook(FB_API_KEYFB_SECRET);
    $fb->require_frame();
    $user_id $fb->require_login();

    if( ! 
    $fb->api_client->users_isAppAdded()) {
        
    $fb->redirect($fb->get_add_url());
    }

    // start session
    session_id($user_id '-'FB_APP_NAME);
    session_start();

    $weapon        = array(
        
    'sneaker_name' => ''
        
    'sneaker_class' => '',
        
    'sneaker_file' => '',
        
    'sneaker_file2' => '',
    );

    $weapon        array_merge($weapon$_SESSION );

    $dbh = new dbh(    DB_USERDB_PASSDB_NAMEDB_HOST);

    $result                         $dbh->query("SELECT COUNT(1) FROM received_gifts WHERE uid1='$user_id'");
    $received_gifts_nb    mysql_result($result);

    $user_points $dbh->get_object("SELECT * FROM user_points WHERE uid='$user_id'"false);
    ?>

    <?php include('css.php'); ?>
    <?php 
    include('battle_css.php'); ?>

    <div id="content"><?php include('tab_menu.php'); ?>

    <div id="background"><!--Top Ad Banner--> <?php if (SERVER_ENVIRONMENT == "PROD") : ?>
    <?php 
    include('top_banner_ad.php'); ?> <?php endif; ?> <!----------------->

    <table align="center" width="100%" cellspacing="0" cellpadding="0"
        class="main_table">
        <!-------------New Top Layout with banner---------->
        <?#php include('index_top_row.php'); ?>
        <!-------------------------------------------------->
        <tr>
            <td><?php include('battle_controls.php'?></td>
        </tr>


        <tr>
            <td class="send_gift_td">



            <table cellspacing="0" width="100%" cellpadding="0" border="0">
                <tr>
                    <td width="100%" valign="top">
                    <div class="white_box_showdown">
                    <?php if ( flash_exists'error')) : ?>
                    <div class="notice"><?php echo get_flash('error'); ?></div>

                    <?php endif; ?>

                    <form onsubmit="showProgress()" 
                        action="<?php echo ABSOLUTE_HTTP_HOST ?>/battle_add_weapon_submit.php"
                        method="post" enctype="multipart/form-data">
                    <table class="add_weapon_table">

                        <tr>
                        <td>
                          <!--<img src="<?=HTTP_HOST.ROOT.IMG_DIR.DIR_SEP?>battle_upload_sneaker.png" />-->
                          
                          
                            <h3 class="battle_heading">Upload photos to use in showdowns!<br><span style="font-size: 12px; color: #808080;">Upload pictures of your remodel and renovation designs, select the appropriate category, .jpg .gif .png formats only please. (Any inappropriate photos will be removed)</span></h3>
                            </td>
                        </tr>
                        <tr>
                            <td>
                            <div id="indicator">
                            Please wait, loading.<br><br> <img src="<?=HTTP_HOST.ROOT.IMG_DIR.DIR_SEP?>big_loading.gif" />
                            </div>
                            
                            <div id="upload_form">
                                <table class="add_weapon_table">
                                <tr>
                                <td class="formTitles">Give this remodel a description (i.e. "Kim's new country kicten"):</td>
                                <td width="300">
                  
                  <textarea type="text" rows="2" cols="20" name="sneaker_name"
                                    value="<?php echo $weapon['sneaker_name'?>" /></textarea>
                    
                    </td>
                                </tr>
                                <tr>
                                    <td class="formTitles">Select a category:</td>
                                    <td><select name="sneaker_class">
                                        <?php foreach((array('' => '-- SELECT ONE --')+$battleClasses) as $key => $value): ?>
                                        <option value="<?php echo $key ?>"<?php if($weapon['sneaker_class'] == $key): ?> selected="selected"<?php endif; ?>><?php echo $value ?></option>
                                            <?php endforeach; ?>
                                    </select></td>
                                </tr>
                                <tr>
                                    <td class="formTitles">Select your BEFORE photo:</td>
                                    <td><input type="file" name="sneaker_file" /></td>
                                </tr>
                            <!---- need functinoality to upload AFTER photo as a set-->    
                 <tr>
                                    <td class="formTitles">Select your AFTER photo:</td>
                                    <td><input type="file" name="sneaker_file2" /></td>
                                </tr>
                                    <!----------------------------------------------------->    
                                
                                <tr>
                                    <td></td>
                                    <td>
                                    <a href="<?php echo FB_APP_URL.'/battle_my_arsenal.php' ?>"><img src="<?php echo HTTP_HOST.'/images/button_go_back.png' ?>" /></a>
                                    <input type="image" src="<?php echo HTTP_HOST.'/images/button_upload.png' ?>" name="add" value="Upload" /></td>
                                </tr>
                                </table>
                            </div>
                            
                            
                            </td>
                        </tr>
                    </table>
                    </form>


                    </div>
                    </td>
                    <td valign="top">
                
                    </td>
                </tr>
            </table>
            </td>
        </tr>



    </table>
    </div>


    <script>
    function showProgress() {
        document.getElementById("indicator").setStyle({"display": "block"});
        document.getElementById("upload_form").setStyle({"display": "none"});
    }
    </script>
    My submit page:
    PHP Code:

    <?php
    require('config.php');
    require(
    'includes/facebook.php');
    require(
    'includes/helpers.php');
    require(
    'includes/dbh.php');

    error_reporting(E_ALL);
    set_time_limit);

    $fb = new Facebook(FB_API_KEYFB_SECRET);
    $user_id $fb->require_login();

    // start session
    session_id($user_id '-'FB_APP_NAME);
    session_start();

    if ( 
    $_SERVER['REQUEST_METHOD']    = 'POST')
    {
        
    $_SESSION        array_merge($_SESSION$_POST);

        
    // check if max quantity is reached
        
    $dbh = new dbh(    DB_USERDB_PASSDB_NAMEDB_HOST);
        
    $result $dbh->query("SELECT COUNT(1) FROM battle_user_arsenal WHERE uid='$user_id'");
        
    $items_nb        mysql_result($result0);
        
    set_flashIf(BATTLE_ADD_WEAPONS_LIMIT && $items_nb >= BATTLE_ADD_WEAPONS_LIMITsprintf('You cannot add more than %s photos.'BATTLE_ADD_WEAPONS_LIMIT), 'error');

        
    set_flashIf(empty($_POST['sneaker_name']), 'Please provide a name for your photo.''error');
        
    set_flashIf(empty($_POST['sneaker_class']), 'Please select a category for your photo.''error');
        
    set_flashIf(empty($_SESSION['sneaker_file']) && !is_uploaded_file($_FILES['sneaker_file']['tmp_name']), 'The BEFORE photo file is required.''error');
        
    set_flashIf(empty($_SESSION['sneaker_file2']) && !is_uploaded_file($_FILES['sneaker_file2']['tmp_name']), 'The AFTER photo file is required.''error');

        
    // if above is ok then check file - BEFORE PHOTO
        
    if ( is_uploaded_file($_FILES['sneaker_file']['tmp_name']))
           {
                 if ( 
    in_array($_FILES['sneaker_file']['type'], array('image/jpeg''image/pjpeg''image/jpg''image/png''image/gif')))
                    {
                
    // all ok then try to upload
                
    $saveFileName            md5(microtime()). '.'pathinfo($_FILES['sneaker_file']['name'], PATHINFO_EXTENSION);
                if( 
    move_uploaded_file($_FILES['sneaker_file']['tmp_name'], UPLOAD_DIR_PATH .'/'$saveFileName))
                {
                    
    // check if file exist though, "move_uploaded_file" doesn't work 100%
                    
    if( is_fileUPLOAD_DIR_PATH .'/'$saveFileName))
                    {
                        
    //  remove previous & thumbs
                        
    if ( isset($_SESSION['sneaker_file'] ) && is_fileUPLOAD_DIR_PATH.'/'.$_SESSION['sneaker_file']))
                        {
                            
    $thumbs        glob(UPLOAD_DIR_PATH.'/*'.$_SESSION['sneaker_file']);
                            
    array_map('unlink', (array) $thumbs );
                        }

                        
    $_SESSION['sneaker_file']    = $saveFileName;
                    }
                    else
                    {
                        
    set_flash('Photo File upload failed, try again''error');
                    }


                }
                else
                {
                    
    set_flash('Photo File upload failed, try again''error');
                }
            }
            else
            {
                
    set_flash('Photo File is invalid type.''error');
            }
        }

            
    // if above is ok then check file - AFTER PHOTO
        
    if ( is_uploaded_file($_FILES['sneaker_file2']['tmp_name']))
           {
                 if ( 
    in_array($_FILES['sneaker_file2']['type'], array('image/jpeg''image/pjpeg''image/jpg''image/png''image/gif')))
                    {
                
    // all ok then try to upload
                
    $saveFileName            md5(microtime()). '.'pathinfo($_FILES['sneaker_file2']['name'], PATHINFO_EXTENSION);
                if( 
    move_uploaded_file($_FILES['sneaker_file2']['tmp_name'], UPLOAD_DIR_PATH .'/'$saveFileName))
                {
                    
    // check if file exist though, "move_uploaded_file" doesn't work 100%
                    
    if( is_fileUPLOAD_DIR_PATH .'/'$saveFileName))
                    {
                        
    //  remove previous & thumbs
                        
    if ( isset($_SESSION['sneaker_file2'] ) && is_fileUPLOAD_DIR_PATH.'/'.$_SESSION['sneaker_file2']))
                        {
                            
    $thumbs        glob(UPLOAD_DIR_PATH.'/*'.$_SESSION['sneaker_file2']);
                            
    array_map('unlink', (array) $thumbs );
                        }

                        
    $_SESSION['sneaker_file2']    = $saveFileName;
                    }
                    else
                    {
                        
    set_flash('Photo File upload failed, try again''error');
                    }


                }
                else
                {
                    
    set_flash('Photo File upload failed, try again''error');
                }
            }
            else
            {
                
    set_flash('Photo File is invalid type.''error');
            }
        }
        
        if( 
    flash_exists('error'))
        {
            
    redirectFB_APP_URL.'/battle_add_weapon.php');
        }
        else
        {
            
    redirectFB_APP_URL.'/battle_add_weapon_preview.php');
        }

    }

    ?>

    My preview page:
    PHP Code:

    <?php
    require('config.php');
    require(
    'includes/facebook.php');
    require(
    'includes/helpers.php');
    require(
    'includes/dbh.php');

    $fb = new Facebook(FB_API_KEYFB_SECRET);
    $user_id $fb->require_login();

    // start session
    session_id($user_id '-'FB_APP_NAME);
    session_start();

    $weapon        = array(
        
    'sneaker_name' => ''
        
    'sneaker_class' => '',
        
    'sneaker_file' => '',
        
    'sneaker_file2' => '',
    );

    $weapon        array_merge($weapon$_SESSION );

    $dbh = new dbh(    DB_USERDB_PASSDB_NAMEDB_HOST);

    $result                         $dbh->query("SELECT COUNT(1) FROM received_gifts WHERE uid1='$user_id'");
    $received_gifts_nb    mysql_result($result);

    $user_points $dbh->get_object("SELECT * FROM user_points WHERE uid='$user_id'"false);

    ?>
    <?php 
    include('css.php'); ?>
    <?php 
    include('battle_css.php'); ?>


    <div id="content"><?php include('tab_menu.php'); ?>

    <div id="background"><!--Top Ad Banner--> <?php if (SERVER_ENVIRONMENT == "PROD") : ?>
    <?php 
    include('top_banner_ad.php'); ?> <?php endif; ?> <!----------------->

    <table align="center" width="100%" cellspacing="0" cellpadding="0"
        class="main_table">
        <tr>
            <td><?php include('battle_controls.php'?></td>
        </tr>


        <tr>
            <td class="send_gift_td">

            <table cellspacing="0" width="100%" cellpadding="0" border="0">
                <tr>
                    <td width="100%" valign="top">
                    <div class="white_box">
                    
                    <form action="<?php echo FB_APP_URL ?>/battle_add_weapon_save.php" method="post">
                    <table class="add_weapon_table" width="100%">
                        <tr>
                            <td colspan="2">
                            <h3 class="battle_heading">Preview Your Weapon</h3>
                            </td>
                        </tr>
                        
                        <tr>
                            <td>
                            
                                <table>
                                    <tr>
                                        <td valign="top">Description:</td>
                                        <td><?php echo $weapon['sneaker_name'?></td>
                                    </tr>
                                    <tr>
                                        <td>Category:</td>
                                        <td><?php echo $battleClasses[$weapon['sneaker_class']] ?></td>
                                    </tr>
                                </table>    
                            </td>
                            
                            <td style="text-align: right;">
                            <img src="<?php echo HTTP_HOST'/'UPLOAD_DIRNAME.'/100-75_'.$weapon['sneaker_file'?>" />
                            </td>
                            <td style="text-align: right;">
                            <img src="<?php echo HTTP_HOST'/'UPLOAD_DIRNAME.'/100-75_'.$weapon['sneaker_file2'?>" />
                            </td>
                        </tr>
                        
                        <tr>
                            <td colspan="2" style="text-align: right; border-top: 1px solid #C5CDE0;">
                            
                                <table style="text-align: right;" align="right">
                                    <tr>
                                        <td><h3>Are you sure?</h3></td>
                                        <td><a href="<?php echo FB_APP_URL.'/battle_add_weapon.php'?>"><img src="<?php echo HTTP_HOST.'/images/button_go_back.png' ?>" /></a></td>
                                        <td><input type="image" src="<?php echo HTTP_HOST.'/images/button_upload.png' ?>" name="add" value="Yes" /></td>
                                    </tr>
                                </table>
                            
                            </td>
                        </tr>
                        
                    </table>
                </form>

                    </div>
                    </td>
            
                </tr>
            </table>
            </td>
        </tr>

    </table>
    </div>

    My Save Page:
    PHP Code:

    <?php
    require('config.php');
    require(
    'includes/facebook.php');
    require(
    'includes/helpers.php');
    require(
    'includes/dbh.php');

    $fb = new Facebook(FB_API_KEYFB_SECRET);
    $user_id $fb->require_login();

    // start session
    session_id($user_id '-'FB_APP_NAME);
    session_start();

    if ( 
    $_SERVER['REQUEST_METHOD']    = 'POST')
    {
        
    set_flashIf(empty($_SESSION['sneaker_name']), 'Sneaker name is required.''error');
        
    set_flashIf(empty($_SESSION['sneaker_class']), 'Sneaker class is required.''error');
        
    set_flashIf(empty($_SESSION['sneaker_file']), 'Sneaker file BEFORE is required.''error');
       
    set_flashIf(empty($_SESSION['sneaker_file2']), 'Sneaker file AFTER is required.''error');


        if( 
    flash_exists('error'))
        {
            
    $fb->redirectFB_APP_URL.'/battle_add_weapon.php');
        }
        else 
        {
            
    $_SESSION        addslashes_deep($_SESSION);
            
            
    $sneaker_name            $_SESSION['sneaker_name'];
            
    $sneaker_class        $_SESSION['sneaker_class'];
            
    $sneaker_file            $_SESSION['sneaker_file'];
        
    $sneaker_file2            $_SESSION['sneaker_file2'];
            
            
    $dbh = new dbh(    DB_USERDB_PASSDB_NAMEDB_HOST);
            
    $result $dbh->query("INSERT INTO battle_user_arsenal (uid, sneaker_name, sneaker_class, sneaker_file, sneaker_file2)
                                        VALUES('$user_id', '$sneaker_name', '$sneaker_class', '$sneaker_file' , '$sneaker_file2')"
    );
            
    // save data now
            
    if( $dbh->affected_rows 
            {
                
    $_SESSION    = array();
                
    // charge for pointa
                
    $add_fee        BATTLE_ADD_WEAPON_COST;
                
    $dbh->query("UPDATE user_points SET points=points-'$add_fee' WHERE uid='$user_id'");
                
    $fb->redirectFB_APP_URL.'/battle_my_arsenal.php?show_feed=1');
            }
            else
            {
                
    set_flash('Sneaker has not been save, try again''error');
                
    $fb->redirectFB_APP_URL.'/battle_add_weapon.php');
            }
        }

    }
    ?>

  • Users who have thanked mlseim for this post:

    SneakerHead (03-09-2010)

  • #5
    New Coder
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up

    That worked perfectly! Thanks so much!!

  • #6
    New Coder
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks again mlseim, I have one other question, after making the changes you suggested the other day, everything seems fine. However, I realized that the thumbnailing is not working correctly when I try to upload files that are large in filesize. For example, on the form above I will attempt to upload two files that are 1.8MB each. I already changed the PHP max upload size on the server and I indeed do see the images uploaded to my upload directory. However, the thumbnails aren't showing up on the preview page as shown above. Also, when I try to resize the image on the fly but going to the image file name directly via url, it does not work. But it works with small sized photos though.

    Is there something I need to set with phpThumbs to make it able to resize large photos on the fly? The test photos I used were 3264x2448. I know I'm veering off the original topic but I'm actually not quite sure where the problem is right now.

    Thanks for all the help thus far.

    Ray

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,539
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    The only thing the preview page is doing is displaying photos from the directory:

    <td style="text-align: right;">
    <img src="<?php echo HTTP_HOST. '/'. UPLOAD_DIRNAME.'/100-75_'.$weapon['sneaker_file'] ?>" />
    </td>
    <td style="text-align: right;">
    <img src="<?php echo HTTP_HOST. '/'. UPLOAD_DIRNAME.'/100-75_'.$weapon['sneaker_file2'] ?>" />
    </td>


    If a photo does not show, the only reason is that it's pushed-off the page or hidden.
    If both photos are in the directories, they should appear.

    I don't see anywhere that you process an image to change it's size.
    That should really be done in the "submit page" file.

    Perhaps if you just told any users that photos cannot exceed X and Y in size?
    You could use PHP GD to reduce the photo dimensions, but are they all JPEG,
    or are some PNG, and GIF?

    Not sure what final result you're looking for.

  • #8
    New Coder
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks again, I think I got it. Stupid me, I had to adjust the memory_limit on the server. Now it's working. The site uses phpThumbs to do the thumbnailing.

    http://phpthumb.sourceforge.net/

    I Google'd and found that php does not have enough memory to generate the thumbnails, so I increased it to 256MB and it worked!

    Sorry for the bump, I should've done more research before posting again. Perhaps this can be of help to someone else in the future.

    Thanks again for the help!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •