Jump to content

Recommended Posts

hi

 

In the following php funtion code, function uploadproductimage() returns an array at line 77 of the below posting, to $images  variable at line 10 in funtion addproduct().

 

 

If i use print_r ($images);

or print_r($images['image']); etc..  (As we know that arrays should be printed using print_r)

 

below the line of code in addproduct() as follows:

 

$images = uploadProductImage('fleImage', SRV_ROOT . 'images/product/');
print_r($images)

instead,if i use ,echo $image; i get the output of $image variable..

Could u give me the reason,please?

 

<?php
function addProduct()
{
    $catId       = $_POST['cboCategory'];
    $name        = $_POST['txtName'];
    $description = $_POST['mtxDescription'];
    $price       = str_replace(',', '', (double)$_POST['txtPrice']);
    $qty         = (int)$_POST['txtQty'];
    
    $images = uploadProductImage('fleImage', SRV_ROOT . 'images/product/');


    $mainImage = $images['image'];
    $thumbnail = $images['thumbnail'];

    $sql   = "INSERT INTO tbl_product (cat_id, pd_name, pd_description, pd_price, pd_qty, pd_image, pd_thumbnail, pd_date)
              VALUES ('$catId', '$name', '$description', $price, $qty, '$mainImage', '$thumbnail', NOW())";

    $result = dbQuery($sql);
    
    header("Location: index.php?catId=$catId");    
}

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

        // generate a random new file name to avoid name conflict
        $imagePath = md5(rand() * time()) . ".$ext";
        
        list($width, $height, $type, $attr) = getimagesize($image['tmp_name']); 

        // make sure the image width does not exceed the
        // maximum allowed width
        if (LIMIT_PRODUCT_WIDTH && $width > MAX_PRODUCT_IMAGE_WIDTH) {
            $result    = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_PRODUCT_IMAGE_WIDTH);
            $imagePath = $result;

        } else {
            $result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
        }    
        
        if ($result) {
            // create thumbnail
            $thumbnailPath =  md5(rand() * time()) . ".$ext";



            $result = createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH);

            

            // create thumbnail failed, delete the image
            if (!$result) {
                unlink($uploadDir . $imagePath);
                $imagePath = $thumbnailPath = '';
            } else {
                $thumbnailPath = $result;
            }    
        } else {
            // the product cannot be upload / resized
            $imagePath = $thumbnailPath = '';
        }
        
    }

    
    return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}
?> 

 

Thanks,

karthikanov24

 

Link to comment
https://forums.phpfreaks.com/topic/174915-print_r-not-working-here/
Share on other sites

Frankly I did not understand your question 100%, but I think the print_r is working fine, maybe you forgot to add ; at the end of print_r function.

 

One hint though: a single array element can not be printed using print_r. Instead you might want to use echo $images['image'];

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.