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!