SOLVED::phpThumb image upload errors - can't figure it out

Hello,

I’m struggling with some image upload errors in the error log since moving a site to a new server. I’ve changed PHP versions from 7.3 to 7.4 and back again but the problem exists.
There seems to be some path inconsistencies looking at the logs but I can’t find an issue in the imageUpload snippet and the issue doesn’t exist on the old server.
MODx 2.8.4
pThumb 2.3.3
PHP 7.3 (changed from 7.4 only to troubleshoot, the old server is 7.3)
Here’s the error log if anyone has any suggestions…

[2022-09-30 08:22:05] (ERROR @ /home/myWebsite/public_html/dev/core/cache/includes/elements/modsnippet/92.include.cache.php : 68) PHP warning: mkdir(): File exists
[2022-09-30 08:22:05] (ERROR @ /home/myWebsite/public_html/dev/core/components/phpthumbof/model/phpthumbof.class.php : 156) [pThumb] Resource: 58 || Image: assets/userfiles/jan-sep30-full/2022-09-30_8-22-05_markboedges-treeswoodland.jpeg
Could not generate thumbnail
phpThumb debug output:
    [0] => phpThumb() v1.7.17-202109221111 in file "phpthumb.class.php" on line 234
    [1] => setSourceFilename(/home2/myWebsite/public_html/dev/assets/userfiles/jan-sep30-full/2022-09-30_8-22-05_markboedges-treeswoodland.jpeg) set $this->sourceFilename to "/home2/myWebsite/public_html/dev/assets/userfiles/jan-sep30-full/2022-09-30_8-22-05_markboedges-treeswoodland.jpeg" in file "phpthumb.class.php" on line 299
    [2] => $AvailableImageOutputFormats = array(text;ico;bmp;wbmp;gif;webp;png;jpeg) in file "phpthumb.class.php" on line 1011
    [3] => $this->thumbnailFormat set to $this->config_output_format "jpeg" in file "phpthumb.class.php" on line 1022
    [4] => $this->thumbnailFormat set to $this->f "jpeg" in file "phpthumb.class.php" on line 1029
    [5] => $this->thumbnailQuality set to "75" in file "phpthumb.class.php" on line 1039
    [6] => resolvePath: /home2/myWebsite/public_html/dev/assets/userfiles/jan-sep30-full/2022-09-30_8-22-05_markboedges-treeswoodland.jpeg (allowed_dirs: Array
(
)
) in file "phpthumb.class.php" on line 1182
    [7] => resolvePath: iteration, path=/home2/myWebsite/public_html/dev/assets/userfiles/jan-sep30-full/2022-09-30_8-22-05_markboedges-treeswoodland.jpeg, base path = /home/myWebsite/public_html/dev in file "phpthumb.class.php" on line 1201
    [8] => open_basedir: "" in file "phpthumb.class.php" on line 1168
    [9] => resolvePath: stop at component 2 in file "phpthumb.class.php" on line 1217
    [10] => resolvePath: stop at path=/home2/myWebsite in file "phpthumb.class.php" on line 1220
    [11] => resolvePath: no match, returning null in file "phpthumb.class.php" on line 1222
    [12] => !$this->config_allow_src_above_docroot therefore setting "/home2/myWebsite/public_html/dev/assets/userfiles/jan-sep30-full/2022-09-30_8-22-05_markboedges-treeswoodland.jpeg" (outside "/home/myWebsite/public_html/dev") to null in file "phpthumb.class.php" on line 1393
    [13] => $this->sourceFilename set to "" in file "phpthumb.class.php" on line 932
    [14] => phpThumb() v1.7.17-202109221111
http://phpthumb.sourceforge.net

Error messages disabled.

edit phpThumb.config.php and (temporarily) set
$PHPTHUMB_CONFIG['disable_debug'] = false;
to view the details of this error in file "phpthumb.class.php" on line 4270
    [15] => resolvePath: /home2/myWebsite/public_html/dev/assets/userfiles/jan-sep30-full/2022-09-30_8-22-05_markboedges-treeswoodland.jpeg (allowed_dirs: Array
(
)
) in file "phpthumb.class.php" on line 1182
    [16] => resolvePath: iteration, path=/home2/myWebsite/public_html/dev/assets/userfiles/jan-sep30-full/2022-09-30_8-22-05_markboedges-treeswoodland.jpeg, base path = /home/myWebsite/public_html/dev in file "phpthumb.class.php" on line 1201
    [17] => resolvePath: stop at component 2 in file "phpthumb.class.php" on line 1217
    [18] => resolvePath: stop at path=/home2/myWebsite in file "phpthumb.class.php" on line 1220
    [19] => resolvePath: no match, returning null in file "phpthumb.class.php" on line 1222
    [20] => !$this->config_allow_src_above_docroot therefore setting "/home2/myWebsite/public_html/dev/assets/userfiles/jan-sep30-full/2022-09-30_8-22-05_markboedges-treeswoodland.jpeg" (outside "/home/myWebsite/public_html/dev") to null in file "phpthumb.class.php" on line 1393
    [21] => SetCacheFilename() _src set from md5($this->sourceFilename) "" = "d41d8cd98f00b204e9800998ecf8427e" in file "phpthumb.class.php" on line 3615
    [22] => SetCacheFilename() _par set from md5(_farC_zc1_h200_w300_dpi150_q75) in file "phpthumb.class.php" on line 3654
    [23] => starting ExtractEXIFgetImageSize() in file "phpthumb.class.php" on line 3408
    [24] => skipping getimagesize() because $this->sourceFilename is empty in file "phpthumb.class.php" on line 3423
    [25] => getimagesize("") failed in file "phpthumb.class.php" on line 3450
    [26] => $this->useRawIMoutput=true after checking $UnAllowedParameters in file "phpthumb.class.php" on line 1654
    [27] => ImageMagickThumbnailToGD() aborting because $this->sourceFilename is empty in file "phpthumb.class.php" on line 1703
    [28] => ImageMagickThumbnailToGD() failed in file "phpthumb.class.php" on line 3457
    [29] => SetOrientationDependantWidthHeight() starting with ""x"" in file "phpthumb.class.php" on line 3387
    [30] => SetOrientationDependantWidthHeight() setting w="300", h="200" in file "phpthumb.class.php" on line 3403
    [31] => EXIF thumbnail extraction: (size=0; type=""; 0x0) in file "phpthumb.class.php" on line 3516
    [32] => starting SourceImageToGD() in file "phpthumb.class.php" on line 3782
    [33] => Not using EXIF thumbnail data because $this->exif_thumbnail_data is empty in file "phpthumb.class.php" on line 3854
    [34] => $this->gdimg_source is still empty in file "phpthumb.class.php" on line 3906
    [35] => ImageMagickThumbnailToGD() failed in file "phpthumb.class.php" on line 3908
    [36] => phpThumb() v1.7.17-202109221111
http://phpthumb.sourceforge.net

Error messages disabled.

edit phpThumb.config.php and (temporarily) set
$PHPTHUMB_CONFIG['disable_debug'] = false;
to view the details of this error in file "phpthumb.class.php" on line 4270
    [37] => SourceImageToGD() failed in file "phpthumb.class.php" on line 379
----------------------

[2022-09-30 08:22:05] (ERROR @ /home/myWebsite/public_html/dev/core/cache/includes/elements/modsnippet/92.include.cache.php : 88) PHP warning: copy(assets/userfiles/jan-sep30-full/assets/userfiles/jan-sep30-full/2022-09-30_8-22-05_markboedges-treeswoodland.jpeg): failed to open stream: No such file or directory

That last line is specifying the path twice - which could be the issue (? doesn’t happen on the old server though). The following path is created and does exists: assets/userfiles/jan-sep30-full/

The snippet #92 imageUpload is:

<?php
// initialize output;
$output = true;

// get the current user name to create the file name as  
$userName = $modx->user->get('username');
// Use it on registration to create user folder for the profile picture
$fieldname = $hook->getValue('username');
// if $fieldname exist (only on registration) use that field to create folder
if ($fieldname) {
    $userName = $fieldname;
}

// valid extensions
$ext_array = array('jpg', 'jpeg', 'png');
 
// create unique path for this form submission
$uploadpath = 'assets/userfiles/'. $userName . '/';

// you can create some logic to automatically
// generate some type of folder structure here.
// the path that you specify will automatically
// be created by the script if it doesn't already
// exist.

// EXAMPLE:
// this would put all file uploads into a new,
// unique folder every day.
// $uploadpath = 'assets/'uploads/'.date('Y-m-d').'/';
 
// get full path to unique folder
$target_path = $modx->config['base_path'] . $uploadpath;

// get uploaded file names:
$submittedfiles = array_keys($_FILES);

// Pthumb parameters
$width = 300;
$height = 200;
$options = "w=$width&h=$height&zc=1";

 
// loop through files
foreach ($submittedfiles as $sf) {
 
    // Get Filename and make sure its good.
    $filename = basename( $_FILES[$sf]['name'] );
 
    // Get file's extension
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    $ext = mb_strtolower($ext); // case insensitive

    // is the file name empty (no file uploaded)
    if($filename != '') {
         
        // is this the right type of file?
        if(in_array($ext, $ext_array)) {

            // clean up file name and make it unique
            $filename = mb_strtolower($filename); // to lowercase
            $filename = strtolower(preg_replace("/[^A-Za-z0-9.]+/i", "-", $filename)); // spaces to underscores and weird characters
            $filename = date("Y-m-d_G-i-s_") . $filename; // add date & time

            // full path to new file
            $myTarget = $uploadpath . $filename;
             
            // create directory to move file into if it doesn't exist
            mkdir($target_path, 0755, true);

            // is the file moved to the proper folder successfully?
            if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
                // set a new placeholder with the new full path (if you need it in subsequent hooks)
                //$modx->setPlaceholder('fi.'.$sf.'_new', $myTarget);



                // Resize and crop image using Pthumbs 
                $thumbnail = $modx->runSnippet("phpthumbof",array('input' => $myTarget, 'options' => $options));
                // remove the first "/" for thumbnail path
                $thumbnail_strip = ltrim($thumbnail, '/');
                // include modx base path to the pthumbs file path
                $thumbnail_fullPath = $modx->config['base_path'] . $thumbnail_strip; 
                // get only the file name
                $thumbnail_name = str_replace("/assets/components/phpthumbof/cache/", "", $thumbnail);
                // target to move the file from pthumbs
                $thumb_newTarget = $uploadpath . $thumbnail_name;
                // Move the pthumb resized file to the user folder
                if (copy($thumbnail_fullPath,$thumb_newTarget)) {
                    // delete the Pthumb cache file
                    unlink($thumbnail_fullPath);
                    // delete the original uploaded file
                    unlink($myTarget);
                }



				//$modx->setPlaceholder('target', $myTarget);
				$hook->setValue($sf, $thumb_newTarget);

				// set the permissions on the file
                if (!chmod($myTarget, 0644)) { /*some debug function*/ }
                 
            } else {
                // File not uploaded
                $errorMsg = 'There was a problem uploading the file.';
                $hook->addError($sf, $errorMsg);
                $output = false; // generate submission error
            }
         
        } else {
            // File type not allowed
            $errorMsg = 'Type of file not allowed.';
            $hook->addError($sf, $errorMsg);
            $output = false; // generate submission error
        }
     
    // if no file, don't error, but return blank
    } else {
        $hook->setValue($sf, '');
    }
}
 
return $output;

Line 88 is:

87         // Move the pthumb resized file to the user folder
88          if (copy($thumbnail_fullPath,$thumb_newTarget)) {
89         // delete the Pthumb cache file
90         unlink($thumbnail_fullPath);
91         // delete the original uploaded file
92        unlink($myTarget);
93         }

Thanks for any help

The key problem seems to be the different paths here: /home2/... respectively /home/....
(It’s not clear to me though, where this difference comes from.)

!$this->config_allow_src_above_docroot therefore setting "/home2/myWebsite/public_html/dev/assets/userfiles/jan-sep30-full/2022-09-30_8-22-05_markboedges-treeswoodland.jpeg" (outside "/home/myWebsite/public_html/dev") to null in file "phpthumb.class.php"

Because the src path isn’t allowed to be outside the document root, the thumbnail file doesn’t get generated (and can’t therefore not be copied on line 88).


Maybe changing the system value phpthumb_allow_src_above_docroot helps (but I’m not sure).

@halftrainedharry - That seems to have sorted out the issue:-)
Thank you so much for taking the time.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.