0

I create a form which contain one image-array(name="image[]",type="file") to upload multiple images.And when user click on save button image is save in destination folder and with that thumbnail is create and it is save at its destination folder.

On local side everything is alright but when I upload it on server image is save in its destination but the thumbnails is not transfer to its destination folder.also folder for thumbnail is not create on server side.where on local-side folder is also create for thumbnails.

/* code for making thumbnails and upload it */

$fileName = $file[$fieldname]['name'][$i];

// $fileName    = preg_replace("/[^A-Za-z0-9._]/", "",$fileName);

$fileTemp   = $file[$fieldname]['tmp_name'][$i];
$dest = JPATH_ROOT . "\\"."images"."\\"."machines"."\\"."machines"."\\".$fileName;

$dest2 = JPATH_ROOT . "\\"."images"."\\"."machines"."\\"."thumbnails"."\\".$fileName;

$dest3 = JPATH_ROOT . "\\"."images"."\\"."machines"."\\"."thumbnails";

$dest3=str_replace("/","\\",$dest3);

$dest=str_replace("\\administrator/components/com_machine",null,$dest); //for image upload

$dest2=str_replace("\\administrator/components/com_machine",null,$dest2); // for  thumbnail creation
$dest=str_replace("/","\\",$dest);
$dest2=str_replace("/","\\",$dest2);

$filePath   =$dest;
$filePath2  =$dest2;

//Check for allowed extensions
$uploadedFileNameParts  = explode('.',$fileName);
$uploadedFileExtension  = array_pop($uploadedFileNameParts);
$uploadedFileExtension  = strtolower($uploadedFileExtension);
$validFileExts          = explode(',', 'jpeg,jpg,png,gif');

    //assume the extension is false until we know its ok
    $extOk = false;

    //go through every ok extension, if the ok extension matches the file extension (case insensitive)
    //then the file extension is ok
    foreach($validFileExts as $key => $value){
        if(preg_match("/$value/i", $uploadedFileExtension )){
            $extOk          = true;
            $fileExtension  = $value;
        }
    }


    // Check if acceptable extension
    if($extOk == false){
         $info['error'] = 1;
         $info['msg']   = JText::_('Not acceptable image extension');

        return $info;
    }



    if($fileName){
        // Try to upload file
        if(JFile::upload($fileTemp, $filePath)){
        //  JFile::upload($newimage, $dest2);


         //------------------thumbnail creation----------------
            $n_width=100; 
            $n_height=100; 



                     if(!is_dir($dest3))
                    {
                           mkdir($dest3);
                           chmod($dest3,0666);
                    } 

            //image creation
            if($file[$fieldname]['type'][$i]=="image/gif")
                { 
                $im=imagecreatefromgif("{$dest}");
                $width=imagesx($im); // Original picture width is stored
                $height=imagesy($im); // Original picture height is stored
                $n_height=($n_width/$width) * $height; // Add this line to maintain aspect ratio
                $newimage=imagecreatetruecolor($n_width,$n_height);
                imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
                if (function_exists("imagegif")) {
                header("Content-type: image/gif");
                imagegif($newimage,"{$dest2}");
                chmod($dest2,0666);
                //move_uploaded_file($newimage,$dest2);

                }

                elseif (function_exists("imagejpeg")) {
                    header("Content-type: image/jpeg");
                    imagejpeg($newimage,"{$dest2}");

                    chmod($dest2,0666);
                    }
                    }////////// end of gif file thumb nail creation//////////

            ////////////// starting of JPG thumb nail creation//////////
            //echo $dest2;
                    if($file[$fieldname]['type'][$i]=="image/jpeg"){
                    $im=imagecreatefromjpeg("{$dest}"); 
                    $width=imagesx($im); // Original picture width is stored
                    $height=imagesy($im); // Original picture height is stored
                    $n_height=($n_width/$width) * $height; // Add this line to maintain aspect ratio
                    $newimage=imagecreatetruecolor($n_width,$n_height); 
                    imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

                    imagejpeg($newimage,"{$dest2}");
                    chmod($dest2,0666);
                //  move_uploaded_file($newimage,$dest2);
                    }

                    if($file[$fieldname]['type'][$i]=="image/png"){
                    $im=imagecreatefrompng("{$dest}"); 
                    $width=imagesx($im); // Original picture width is stored
                    $height=imagesy($im); // Original picture height is stored
                    $n_height=($n_width/$width) * $height; // Add this line to maintain aspect ratio
                    $newimage=imagecreatetruecolor($n_width,$n_height); 
                    imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

                    imagepng($newimage,"{$dest2}");
                    chmod($dest2,0666);
                //  move_uploaded_file($newimage,$dest2);
                    }
                //  move_uploaded_file($newimage,"$dest2");

                //  exit;    
            //------------------end thumbnail creation--------------


            //JFile::upload($newimage, $dest3);


            $info['error']  = 0;
            $info['msg']    = JText::_('record saved');

            return $info;
        } 
    } else {
        $info['error']  = 1;
        $info['msg']    = JText::_('No image selected to upload');

        return $info;
    }   
  • Can you please update your question with the code your using for the upload as I bet the paths have not been defined correctly – Lodder Jan 27 '15 at 11:00
1

I can see why it may not work on a different hosting environment. Your paths are being destroyed with str_replace etc. Keep things simple like so:

$dest  = JPATH_ROOT . '/images/machines/machines/' . $fileName;
$dest2 = JPATH_ROOT . '/images/machines/thumbnails/' . $fileName;    
$dest3 = JPATH_ROOT . '/images/machines/thumbnails";

JFile::upload($fileTemp, $dest);
JFile::upload($newimage, $dest2);
  • When I write this JFile::upload($newimage, $dest2); there is error will generate like: "failed to move file" – deval oza Jan 27 '15 at 11:53
  • Have you tried debugging the code using var_dump(); with the variable? Have you ensured your tmp path in the global configuration is correct? – Lodder Jan 27 '15 at 12:00
  • var_dump(); returns false.What does it mean.I write var_dump($newimage); after $newimage; – deval oza Jan 27 '15 at 12:46
  • var_dump dumps the data for the variable. As mentioned before, ensure you have set up your tmp paths correctly in the configuration.php and start using var_dump($foo) from the top of your script to ensure that all variables return the desired value – Lodder Jan 27 '15 at 13:03
  • ok.i will try.. – deval oza Jan 27 '15 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.