We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34127
    • 135 Posts
    Hi, I’ve been trying to get all of my scripts and such working 100% with MODx (or more or less rewriting them when I get time), and my latest one is having issues with sockets. Basically, I have a database app which was coded ages ago and some poor planning went into it. Every entry has an image, where the full URL to that image is stored in the DB. Some of those images no longer exist (some were hosted on imageshack, others were on my server but the path got changed and the URL never got updated). This is the relevant part:

    if( preg_match( "#^(http|ftp|https)://\S+/(\S+).(gif|png|jpg)$#i", $row['Image'], $matches ) ) {
    	$handle = @fopen( $matches[0], "rb" );
    	$this->output .= $matches[0];
    	
    	if( $handle !== false ) {
    		@fclose( $handle );
    		$file_size = getimagesize( $matches[0] );
    		if( $file_size ) {
    			$file_contents = file_get_contents( $matches[0] );
    		}
    	}
    	
    	if( $file_contents ) {
    		$matches[3] = str_replace( " ", "_", $matches[3]);
    		$matches[3] = preg_replace( "/[^\w\.]/", "", $matches[3]);
    		$filename = $matches[3] . '.' . $matches[4];
    		
    		if( @file_exists( $upload_dir . $filename ) ) {
    			$counter = 1;
    			while ( @file_exists( $upload_dir . $matches[3] . '-' . $counter . '.' . $matches[4] ) ) {
    				$counter++;
    			}
    			$filename = $matches[3] . '-' . $counter . '.' . $matches[4];
    		}
    
    //copy file to server and update the record



    What I’m trying to do is use fopen to check that the URL exists (ie can be opened). If it does, I use file_get_contents to grab the file, move that file to my "images" directory (because I want all images to be stored on my server), and save the file as the filename which is extracted from the URL at the very start of the process. There are over 1600 records to be updated, so this can’t be done manually and I thought a quick update script would be nice. The only issue is when the file doesn’t exist... it completely halts the processing of the script, even thought I’m using "@" to suppress errors/warnings. What am I doing wrong? Thanks for your help! smiley
      • 22303 MODX Staff
      • 10,725 Posts
      Eleventeen, I believe this was fixed a little while back in trunk; the MODx error handler was trapping suppressed warnings unintentionally. Are you by any chance trying out MODx 0.9.6 RC3? If not, that is one option, which could also help verify the solution works properly. The other is to find the phpError function in manager/includes/document.parser.class.inc.php and modify the first line from ...
      if ($nr == 8 && $this->stopOnNotice == false) {
      .... to ...
      if ($nr == 0 || ($nr == 8 && $this->stopOnNotice == false)) {

        • 34127
        • 135 Posts
        I actually just upgraded to RC-3 (rev. 2592) hoping that it would go away but the problem is still there. Should I grab the latest copy from SVN and see if that works? I’ll also try that fix you posted if that doesn’t solve it. Thanks! smiley
          • 22303 MODX Staff
          • 10,725 Posts
          Hmm, that fix should be in RC3...
            • 34127
            • 135 Posts
            That’s odd, I even re-uploaded the document parser file and I’m still getting the error. I also checked the file and was able to find that line of code you posted. What might be wrong?
              • 34127
              • 135 Posts
              Sorry to bump this, but is there something I can do to fix this? I even tried to a fresh install of the latest RC and the same thing is happening. Thanks again. smiley