SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    upload image to server help

    Hi there! im new to php , im having problem when upload image to server. Hope somebody can help me.

    i upload picture in admin CMS, the pic able to save in mysql but is not save in my server, the script below :
    PHP Code:
    $catImage uploadImage('fleImage'SRV_ROOT 'images/category/'); 
    i have check the folder/directory name all correct but the picture wont save in my server images folder & i try this script but not working as well:
    PHP Code:
    $target_path "images/category/"
    $target_path $target_path basename$_FILES['uploadedfile']['name']); 
    i got this tutorial from :
    PHP MySQL Shopping Cart Tutorial

    Thank You In Advance

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,903
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    Can you please post the code for the uploadImage function?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay

    PHP Code:
    <?php
    require_once '../../library/config.php';
    require_once 
    '../library/functions.php';

    checkUser();

    $action = isset($_GET['action']) ? $_GET['action'] : '';
    switch (
    $action) {
        
        case 
    'add' :
            
    addCategory();
            break;
          
        case 
    'modify' :
            
    modifyCategory();
            break;
            
        case 
    'delete' :
            
    deleteCategory();
            break;
        
        case 
    'deleteImage' :
            
    deleteImage();
            break;
        
           
        default :
            
    // if action is not defined or unknown
            // move to main category page
            
    header('Location: index.php');
    }


    /*
        Add a category
    */
    function addCategory()
    {
        
    $name        $_POST['txtName'];
        
    $description $_POST['mtxDescription'];
        
    $image       $_FILES['fleImage'];
        
    $parentId    $_POST['hidParentId'];
        
        
    $catImage uploadImage('fleImage'SRV_ROOT 'images/category/');

        
        
    $sql   "INSERT INTO tbl_category (cat_parent_id, cat_name, cat_description, cat_image) 
                  VALUES (
    $parentId, '$name', '$description', '$catImage')";
        
    $result dbQuery($sql) or die('Cannot add category' mysql_error());
        
        
    header('Location: index.php?catId=' $parentId);              
    }

    /*
        Upload an image and return the uploaded image name 
    */
    function uploadImage($inputName$uploadDir)
    {
        
    $image     $_FILES[$inputName];
        
    $imagePath '';
        
        
    // if a file is given
        
    if (trim($image['tmp_name']) != '') {
            
    // get the image extension
            
    $ext substr(strrchr($image['name'], "."), 1); 

            
    // generate a random new file name to avoid name conflict
    ////////////$imagePath = md5(rand() * time()) . ".$ext";///////////////// remove md5 function ///////
             
    $imagePath $image['name'] ;
            
            
    // check the image width. if it exceed the maximum
            // width we must resize it
            
    $size getimagesize($image['tmp_name']);
            
            if (
    $size[0] > MAX_CATEGORY_IMAGE_WIDTH) {
                
    $imagePath createThumbnail($image['tmp_name'], $uploadDir $imagePathMAX_CATEGORY_IMAGE_WIDTH);
            } else {
                
    // move the image to category image directory
                // if fail set $imagePath to empty string
                
    if (!move_uploaded_file($image['tmp_name'], $uploadDir $imagePath)) {
                    
    $imagePath '';
                }
            }    
        }

        
        return 
    $imagePath;
    }

    /*
        Modify a category
    */
    function modifyCategory()
    {
        
    $catId       = (int)$_GET['catId'];
        
    $name        $_POST['txtName'];
        
    $description $_POST['mtxDescription'];
        
    $image       $_FILES['fleImage'];
        
        
    $catImage uploadImage('fleImage'SRV_ROOT 'images/category/');
        
        
    // if uploading a new image
        // remove old image
        
    if ($catImage != '') {
            
    _deleteImage($catId);
            
    $catImage "'$catImage'";
        } else {
            
    // leave the category image as it was
            
    $catImage 'cat_image';
        }
         
        
    $sql    "UPDATE tbl_category 
                   SET cat_name = '
    $name', cat_description = '$description', cat_image = $catImage
                   WHERE cat_id = 
    $catId";
               
        
    $result dbQuery($sql) or die('Cannot update category. ' mysql_error());
        
    header('Location: index.php');              
    }

    /*
        Remove a category
    */
    function deleteCategory()
    {
        if (isset(
    $_GET['catId']) && (int)$_GET['catId'] > 0) {
            
    $catId = (int)$_GET['catId'];
        } else {
            
    header('Location: index.php');
        }
        
        
    // find all the children categories
        
    $children getChildren($catId);
        
        
    // make an array containing this category and all it's children
        
    $categories  array_merge($children, array($catId));
        
    $numCategory count($categories);

        
    // remove all product image & thumbnail 
        // if the product's category is in  $categories
        
    $sql "SELECT pd_id, pd_image, pd_thumbnail
                FROM tbl_product
                WHERE cat_id IN (" 
    implode(','$categories) . ")";
        
    $result dbQuery($sql);
        
        while (
    $row dbFetchAssoc($result)) {
            @
    unlink(SRV_ROOT PRODUCT_IMAGE_DIR $row['pd_image']);    
            @
    unlink(SRV_ROOT PRODUCT_IMAGE_DIR $row['pd_thumbnail']);
        }
        
        
    // delete the products
        
    $sql "DELETE FROM tbl_product
                WHERE cat_id IN (" 
    implode(','$categories) . ")";
        
    dbQuery($sql);
        
        
    // then remove the categories image
        
    _deleteImage($categories);

        
    // finally remove the category from database;
        
    $sql "DELETE FROM tbl_category 
                WHERE cat_id IN (" 
    implode(','$categories) . ")";
        
    dbQuery($sql);
        
        
    header('Location: index.php');
    }


    /*
        Recursively find all children of $catId
    */
    function getChildren($catId)
    {
        
    $sql "SELECT cat_id ".
               
    "FROM tbl_category ".
               
    "WHERE cat_parent_id = $catId ";
        
    $result dbQuery($sql);
        
        
    $cat = array();
        if (
    dbNumRows($result) > 0) {
            while (
    $row dbFetchRow($result)) {
                
    $cat[] = $row[0];
                
                
    // call this function again to find the children
                
    $cat  array_merge($catgetChildren($row[0]));
            }
        }

        return 
    $cat;
    }


    /*
        Remove a category image
    */
    function deleteImage()
    {
        if (isset(
    $_GET['catId']) && (int)$_GET['catId'] > 0) {
            
    $catId = (int)$_GET['catId'];
        } else {
            
    header('Location: index.php');
        }
        
        
    _deleteImage($catId);
        
        
    // update the image name in the database
        
    $sql "UPDATE tbl_category
                SET cat_image = ''
                WHERE cat_id = 
    $catId";
        
    dbQuery($sql);        

        
    header("Location: index.php?view=modify&catId=$catId");
    }

    /*
        Delete a category image where category = $catId
    */
    function _deleteImage($catId)
    {
        
    // we will return the status
        // whether the image deleted successfully
        
    $deleted false;

        
    // get the image(s)
        
    $sql "SELECT cat_image 
                FROM tbl_category
                WHERE cat_id "
    ;
        
        if (
    is_array($catId)) {
            
    $sql .= " IN (" implode(','$catId) . ")";
        } else {
            
    $sql .= " = $catId";
        }    

        
    $result dbQuery($sql);
        
        if (
    dbNumRows($result)) {
            while (
    $row dbFetchAssoc($result)) {
                
    // delete the image file
                
    $deleted = @unlink(SRV_ROOT CATEGORY_IMAGE_DIR $row['cat_image']);
            }    
        }
        
        return 
    $deleted;
    }

    ?>

  4. #4
    SitePoint Member shedishedi's Avatar
    Join Date
    Sep 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    I use this for my website:
    $target_path = "images/category/";

    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

    ----------------------------
    Then i insert to database
    ----------------------------
    //Writes the information to the database
    mysql_query("INSERT INTO banner (uploadedfile) VALUES ('$uploadedfile')");

    ---------------------------------------
    Then create a function to upload for you
    ---------------------------------------
    function upload ($file, $path){

    move_uploaded_file($file, $path);

    }

    ------------------------------------------
    Then call your function to upload your photo
    ------------------------------------------
    $picture = $_FILES['uploadedfile']['tmp_name'];
    thumb($picture, $target_path);


    If you have already uploaded your script to server, you have to make sure your folder permission is set to 777 using your ftp software. I use filezilla.

    Let me know if my tip helps.
    Cheers and happy coding

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    What you pasted is not a function definition. Look through (or Search In Files, if your editor supports it) the code until you find the words "function uploadImage"

  6. #6
    SitePoint Member shedishedi's Avatar
    Join Date
    Sep 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs down

    Please explain to me Stalion what ma not doing right. But it works for me

  7. #7
    SitePoint Member
    Join Date
    Jul 2011
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys ! Thank you so much !! problem solved

    My mistakes here:
    $catImage = uploadImage('fleImage', SRV_ROOT . 'images/category/');

    I change it to:
    $catImage = uploadImage('fleImage', '../../images/category/');

    On my config.php:
    define('SRV_ROOT', '/root/www/plaincart/');

    shedishedi: I had tried the method you gave but is not working for me
    but Thank you so much for helping appreciate it

    StarLion: the only words "function uploadImage" i found is only in the processCategory.php file that i provide, i had tried looking for the words but no luck. anyways...Thanks for helping


Bookmarks

Posting Permissions

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