Jump to content

Image Download Script Error


lefreak

Recommended Posts

Hi Guys,

I have an issue with the following script which is throwing this error

Quote

( ! ) Parse error: syntax error, unexpected end of file in C:\wamp64\www\script\images.php on line 41

I just can not see the error, can anyone help (not even sure the script is going to work)

<?php
/* settings */
//folder for images saving
$saveDir = "c:\wamp64\www\script\images\";

//database connection
// Turn off all error reporting
error_reporting(0);

$conn = mysqli_connect('localhost', 'root', '', 'tbl_temp_products');

//start and end table row to prevent timeout or hard server work
$start = '1';
$end = '200';
/* end of settings */

//query for fetching the image urls
$sql = 'SELECT image_url FROM tbl_temp_products ORDER BY id DESC LIMIT ' . $start . ',' . $end . '';
$res = $conn->query($sql);

$count = 0;          //this is for count the total row fetched
$notify = '';        //this is for seeing result or errors

while ($row = $res->fetch_assoc()) {
$url = $row(['image_url']);
$dest = $saveDir . clean($row(['image_url'])) . '.jpg';

if (!file_exists($dest)){   //prevent file overwriting
   if (copy($url, $dest)){
      $notify.= 'image saved: '. $row(['image_url']);
      $count++;
   }else{
      $notify.= 'ERROR saving image: '. $row(['image_url']);
   }else{
   $notify.= 'image already exists: '. $row(['image_url']);
   }
}
//output result
echo 'TOTAL IMAGE SAVED: ' .$count .'\n';
echo $notify;
?>

 

Link to comment
Share on other sites

14 minutes ago, Barand said:

The problem is here (obvious when you look at the colour formatting of the posted code)


$saveDir = "c:\wamp64\www\script\images\";
                                       ^

"\"s are escape characters, so the final one escapes the ending ". You need to use \\ or / as path separators.

Thanks Barand, total rookie miss lol

Also getting this error now

Quote

( ! ) Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\wamp64\www\script\images.php on line 34

which is the second }else{

if (!file_exists($dest)){   //prevent file overwriting
   if (copy($url, $dest)){
      $notify.= 'image saved: '. $row(['image_url']);
      $count++;
   }else{
      $notify.= 'ERROR saving image: '. $row(['image_url']);
   }else{
   $notify.= 'image already exists: '. $row(['image_url']);
   }
}

 

Link to comment
Share on other sites

11 minutes ago, Barand said:

You can have only one else per if.

You can have several elseif's though

I did think of that but still throws the error

Quote

Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) in C:\wamp64\www\script\images.php on line 34

 

Link to comment
Share on other sites

Perhaps

if (!file_exists($dest)) {   //prevent file overwriting
   if (copy($url, $dest)) {
      $notify.= 'image saved: '. $row(['image_url']);
      $count++;
   } else {
      $notify.= 'ERROR saving image: '. $row(['image_url']);
   }
} else {
   $notify.= 'image already exists: '. $row(['image_url']);
}

 

Link to comment
Share on other sites

thank you for helping previously Barand.

I am now receiving this error

Quote

( ! ) Fatal error: Uncaught Error: Function name must be a string in C:\wamp64\www\script\images.php on line 25

( ! ) Error: Function name must be a string in C:\wamp64\www\script\images.php on line 25

which is this line

$url = $row(['image_url']);

 

Link to comment
Share on other sites

12 minutes ago, NotSunfighter said:

Sorry for the intrution, but the line above that:

while ($row = $res->fetch_assoc()) {

Has NO ending bracket

no need to be sorry, every little helps thank you

do you mean ) or }

this is the code now as it stands

while ($row = $res->fetch_assoc()) {
$url = $row(['image_url']);
$dest = $saveDir . clean($row(['image_url'])) . '.jpg';

if (!file_exists($dest)) {   //prevent file overwriting
   if (copy($url, $dest)) {
      $notify.= 'image saved: '. $row(['image_url']);
      $count++;
   } else {
      $notify.= 'ERROR saving image: '. $row(['image_url']);
   }
} else {
   $notify.= 'image already exists: '. $row(['image_url']);
}
}

 

Link to comment
Share on other sites

thank you for the easy to understand help

I am now not receiving any errors and the script is running however it is only retrieving 1 image and saving it in the folder as .jpg with out the name which is causing the script to report the image already exists. I added a function to clean the url from the image name before saving so not sure if that is what is causing the problem.

<?php
/* settings */
//folder for images saving
$saveDir = "c:/wamp64/www/script/images/";

//database connection
// Turn off all error reporting
//error_reporting(0);

$conn = mysqli_connect('localhost', 'root', '', 'tbl_temp_products');

function containsTLD($string) {
  preg_match(
    "/(AC($|\/)|\.AD($|\/)|\.AE($|\/)|\.AERO($|\/)|\.AF($|\/)|\.AG($|\/)|\.AI($|\/)|\.AL($|\/)|\.AM($|\/)|\.AN($|\/)|\.AO($|\/)|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.SD($|\/)|\.SE($|\/)|\.SG($|\/)|\.SH($|\/)|\.SI($|\/)|\.SJ($|\/)|\.SK($|\/)|\.SL($|\/)|\.SM($|\/)|\.SN($|\/)|\.SO($|\/)|\.SR($|\/)|\.ST($|\/)|\.SU($|\/)|\.SV($|\/)|\.SY($|\/)|\.SZ($|\/)|\.TC($|\/)|\.TD($|\/)|\.TEL($|\/)|\.TF($|\/)|\.TG($|\/)|\.TH($|\/)|\.TJ($|\/)|\.TK($|\/)|\.TL($|\/)|\.TM($|\/)|\.TN($|\/)|\.TO($|\/)|\.TP($|\/)|\.TR($|\/)|\.TRAVEL($|\/)|\.TT($|\/)|\.TV($|\/)|\.TW($|\/)|\.TZ($|\/)|\.UA($|\/)|\.UG($|\/)|\.UK($|\/)|\.US($|\/)|\.UY($|\/)|\.UZ($|\/)|\.VA($|\/)|\.VC($|\/)|\.VE($|\/)|\.VG($|\/)|\.VI($|\/)|\.VN($|\/)|\.VU($|\/)|\.WF($|\/)|\.WS($|\/)|\.XN--0ZWM56D($|\/)|\.XN--11B5BS3A9AJ6G($|\/)|\.XN--80AKHBYKNJ4F($|\/)|\.XN--9T4B11YI5A($|\/)|\.XN--DEBA0AD($|\/)|\.XN--G6W251D($|\/)|\.XN--HGBK6AJ7F53BBA($|\/)|\.XN--HLCJ6AYA9ESC7A($|\/)|\.XN--JXALPDLP($|\/)|\.XN--KGBECHTV($|\/)|\.XN--ZCKZAH($|\/)|\.YE($|\/)|\.YT($|\/)|\.YU($|\/)|\.ZA($|\/)|\.ZM($|\/)|\.ZW)/i",
    $string,
    $M);
  $has_tld = (count($M) > 0) ? true : false;
  return $has_tld;
}

function clean($url) {
  $U = explode(' ',$url);

  $W =array();
  foreach ($U as $k => $u) {
    if (stristr($u,".")) { //only preg_match if there is a dot
      if (containsTLD($u) === true) {
      unset($U[$k]);
      return clean( implode(' ',$U));
    }
    }
  }
  return implode(' ',$U);
}


//start and end table row to prevent timeout or hard server work
$start = '1';
$end = '200';
/* end of settings */

//query for fetching the image urls
$sql = 'SELECT image_url FROM tbl_temp_products ORDER BY id DESC LIMIT ' . $start . ',' . $end . '';
$res = $conn->query($sql);

$count = 0;          //this is for count the total row fetched
$notify = '';        //this is for seeing result or errors

while ($row = $res->fetch_assoc()) {
$url = $row['image_url'];
$dest = $saveDir . clean($row['image_url']) . '.jpg';

if (!file_exists($dest)) {   //prevent file overwriting
   if (copy($url, $dest)) {
      $notify.= 'image saved: '. $row['image_url'];
      $count++;
   } else {
      $notify.= 'ERROR saving image: '. $row['image_url'] . '<br/>';
   }
} else {
   $notify.= 'image already exists: '. $row['image_url'] . '<br/>';
}
}

//output result
echo 'TOTAL IMAGE SAVED: ' .$count .'\n';
echo $notify;
?>

 

Link to comment
Share on other sites

On 6/21/2020 at 9:46 AM, lefreak said:

thank you for the easy to understand help

I am now not receiving any errors and the script is running however it is only retrieving 1 image and saving it in the folder as .jpg with out the name which is causing the script to report the image already exists. I added a function to clean the url from the image name before saving so not sure if that is what is causing the problem.


<?php
/* settings */
//folder for images saving
$saveDir = "c:/wamp64/www/script/images/";

//database connection
// Turn off all error reporting
//error_reporting(0);

$conn = mysqli_connect('localhost', 'root', '', 'tbl_temp_products');

function containsTLD($string) {
  preg_match(
    "/(AC($|\/)|\.AD($|\/)|\.AE($|\/)|\.AERO($|\/)|\.AF($|\/)|\.AG($|\/)|\.AI($|\/)|\.AL($|\/)|\.AM($|\/)|\.AN($|\/)|\.AO($|\/)|\.AQ($|\/)|\.AR($|\/)|\.ARPA($|\/)|\.AS($|\/)|\.ASIA($|\/)|\.AT($|\/)|\.AU($|\/)|\.AW($|\/)|\.AX($|\/)|\.AZ($|\/)|\.BA($|\/)|\.BB($|\/)|\.BD($|\/)|\.BE($|\/)|\.BF($|\/)|\.BG($|\/)|\.BH($|\/)|\.BI($|\/)|\.BIZ($|\/)|\.BJ($|\/)|\.BM($|\/)|\.BN($|\/)|\.BO($|\/)|\.BR($|\/)|\.BS($|\/)|\.BT($|\/)|\.BV($|\/)|\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/)|\.TO($|\/)|\.TP($|\/)|\.TR($|\/)|\.TRAVEL($|\/)|\.TT($|\/)|\.TV($|\/)|\.TW($|\/)|\.TZ($|\/)|\.UA($|\/)|\.UG($|\/)|\.UK($|\/)|\.US($|\/)|\.UY($|\/)|\.UZ($|\/)|\.VA($|\/)|\.VC($|\/)|\.VE($|\/)|\.VG($|\/)|\.VI($|\/)|\.VN($|\/)|\.VU($|\/)|\.WF($|\/)|\.WS($|\/)|\.XN--0ZWM56D($|\/)|\.XN--11B5BS3A9AJ6G($|\/)|\.XN--80AKHBYKNJ4F($|\/)|\.XN--9T4B11YI5A($|\/)|\.XN--DEBA0AD($|\/)|\.XN--G6W251D($|\/)|\.XN--HGBK6AJ7F53BBA($|\/)|\.XN--HLCJ6AYA9ESC7A($|\/)|\.XN--JXALPDLP($|\/)|\.XN--KGBECHTV($|\/)|\.XN--ZCKZAH($|\/)|\.YE($|\/)|\.YT($|\/)|\.YU($|\/)|\.ZA($|\/)|\.ZM($|\/)|\.ZW)/i",
    $string,
    $M);
  $has_tld = (count($M) > 0) ? true : false;
  return $has_tld;
}

function clean($url) {
  $U = explode(' ',$url);

  $W =array();
  foreach ($U as $k => $u) {
    if (stristr($u,".")) { //only preg_match if there is a dot
      if (containsTLD($u) === true) {
      unset($U[$k]);
      return clean( implode(' ',$U));
    }
    }
  }
  return implode(' ',$U);
}


//start and end table row to prevent timeout or hard server work
$start = '1';
$end = '200';
/* end of settings */

//query for fetching the image urls
$sql = 'SELECT image_url FROM tbl_temp_products ORDER BY id DESC LIMIT ' . $start . ',' . $end . '';
$res = $conn->query($sql);

$count = 0;          //this is for count the total row fetched
$notify = '';        //this is for seeing result or errors

while ($row = $res->fetch_assoc()) {
$url = $row['image_url'];
$dest = $saveDir . clean($row['image_url']) . '.jpg';

if (!file_exists($dest)) {   //prevent file overwriting
   if (copy($url, $dest)) {
      $notify.= 'image saved: '. $row['image_url'];
      $count++;
   } else {
      $notify.= 'ERROR saving image: '. $row['image_url'] . '<br/>';
   }
} else {
   $notify.= 'image already exists: '. $row['image_url'] . '<br/>';
}
}

//output result
echo 'TOTAL IMAGE SAVED: ' .$count .'\n';
echo $notify;
?>

 

Can anyone help with this problem as I have tried various things which either killed the script or did not work.

Thanks

Link to comment
Share on other sites

  • 4 weeks later...

I think I have narrowed it down to the clean urls function which is not just removing the url from the string but the entire string including the filename.

I dont fully understand the contents of the preg_match.

Eg. http://my-site.com/gifts/images/my-image.jpg

I require the function to remove http://my-site.com/gifts/images/  and leave only the filename

help would be appreciated as its got me stumped 😖

function containsTLD($string) {
  preg_match(
    "/(AC($|\/)|\.AD($|\/)|\.AE($|\/)|\.AERO($|\/)|\.AF($|\/)|\.AG($|\/)|\.AI($|\/)|\.AL($|\/)|\.AM($|\/)|\.AN($|\/)|\.AO($|\/)|\.AQ($|\/)|\.AR($|\/)|\.ARPA($|\/)|\.AS($|\/)|\.ASIA($|\/)|\.AT($|\/)|\.AU($|\/)|\.AW($|\/)|\.AX($|\/)|\.AZ($|\/)|\.BA($|\/)|\.BB($|\/)|\.BD($|\/)|\.BE($|\/)|\.BF($|\/)|\.BG($|\/)|\.BH($|\/)|\.BI($|\/)|\.BIZ($|\/)|\.BJ($|\/)|\.BM($|\/)|\.BN($|\/)|\.BO($|\/)|\.BR($|\/)|\.BS($|\/)|\.BT($|\/)|\.BV($|\/)|\.BW($|\/)|\.BY($|\/)|\.BZ($|\/)|\.CA($|\/)|\.CAT($|\/)|\.CC($|\/)|\.CD($|\/)|\.CF($|\/)|\.CG($|\/)|\.CH($|\/)|\.CI($|\/)|\.CK($|\/)|\.CL($|\/)|\.CM($|\/)|\.CN($|\/)|\.CO($|\/)|\.COM($|\/)|\.COOP($|\/)|\.CR($|\/)|\.CU($|\/)|\.CV($|\/)|\.CX($|\/)|\.CY($|\/)|\.CZ($|\/)|\.DE($|\/)|\.DJ($|\/)|\.DK($|\/)|\.DM($|\/)|\.DO($|\/)|\.DZ($|\/)|\.EC($|\/)|\.EDU($|\/)|\.EE($|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0AD($|\/)|\.XN--G6W251D($|\/)|\.XN--HGBK6AJ7F53BBA($|\/)|\.XN--HLCJ6AYA9ESC7A($|\/)|\.XN--JXALPDLP($|\/)|\.XN--KGBECHTV($|\/)|\.XN--ZCKZAH($|\/)|\.YE($|\/)|\.YT($|\/)|\.YU($|\/)|\.ZA($|\/)|\.ZM($|\/)|\.ZW)/i",
    $string,
    $M);

  $has_tld = (count($M) > 0) ? true : false;
  return $has_tld;
}

function clean($url) {
  $U = explode(' ',$url);

  $W =array();
  foreach ($U as $k => $u) {
    if (stristr($u,".")) { //only preg_match if there is a dot
      if (containsTLD($u) === true) {
      unset($U[$k]);
      return clean( implode(' ',$U));
    }
    }
  }
  return implode(' ',$U);
}

 

Link to comment
Share on other sites

so you just want the filename (optional extension) from a uri?

<?php declare (strict_types = 1);
  $uri = 'http://my-site.com/gifts/images/my-image.jpg';
    echo $uri . '<br>';
  $ext = pathinfo($uri, PATHINFO_EXTENSION);
  $uri = pathinfo($uri, PATHINFO_FILENAME);
    echo 'filename is: ' . $uri . '<br>voila!<br>';
    echo '<br>looking for extension?<br>';
    echo 'filename and extension: ' . $uri . '.' . $ext;
    echo '<br>and once again, voila!';
 exit;
?>

 

Link to comment
Share on other sites

13 hours ago, jodunno said:

so you just want the filename (optional extension) from a uri?


<?php declare (strict_types = 1);
  $uri = 'http://my-site.com/gifts/images/my-image.jpg';
    echo $uri . '<br>';
  $ext = pathinfo($uri, PATHINFO_EXTENSION);
  $uri = pathinfo($uri, PATHINFO_FILENAME);
    echo 'filename is: ' . $uri . '<br>voila!<br>';
    echo '<br>looking for extension?<br>';
    echo 'filename and extension: ' . $uri . '.' . $ext;
    echo '<br>and once again, voila!';
 exit;
?>

 

I am trying to remove the tld from the url and leave only the filename

Link to comment
Share on other sites

ok for anyone still following this topic I was on the completely wrong side of the tracks lol

I changed the function completely to this and now it works as expected by removing the url in the array and leaving only the filename behind.

function clear_string_from_characters( $string ) {

		$search = array(
			'http://www.mysite.co.uk/store/images/',
		);

		$replace = '';

    	return str_replace( $search, $replace, $string );

	}

 

Link to comment
Share on other sites

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.