0

I need to download all photos about estates from an XML to save on the server. Every estate child in the XMl has a general section with all information and then a node called Foto (estate's photos) and another one for plans (Planimetria). The link of every image is structured as is:

<Link>http://www.site.it/ImageView.ashx?id=[photoID]&reduce=1438[can be set as I want es: 1000, 960, 1080]</Link>

I need to call it inside the $url_photo and $url_plan so I can read the photoID from XML and set resolution (1438,1000,960) with a global variable.

This is my code:

   <?php

    $xml = simplexml_load_file("Schede.xml"); // your xml
    $path = '/mnt/c/Users/Giuseppe/Desktop/FotoTest/';
    $i = 1;
    $resolution = '1000';


        // Estate Image
        foreach($xml->CR03_SCHEDE as $estate){

            //if((string) $estate['ELIMINATO'] = "NO"){

                echo "\nEstate n $i Images\n";

                foreach($estate->Foto->CR04_SCHEDE_FOTO as $photo){
                    $url_photo = (string) $photo->Link;
                    $filename_photo = basename($photo->CR04_FILENAME); // get the filename
                    if(file_exists($path . $filename_photo)) {
                        echo "file $filename_photo already exists \n";
                    } 

                    else {
                        $img_photo = file_get_contents($url_photo); // get the image from the url
                        file_put_contents($path . $filename_photo, $img_photo); // create a file and feed the image
                        echo "file $filename_photo created \n";
                    }
                }

                // Plans
                echo "\nEstate n $i plans\n";

                foreach($estate->Planimetria->CR04_SCHEDE_FOTO as $plan) {
                    $url_plan = (string) $plan->'http: // www.site.it/ImageView.ashx?id=' . $plan->ID . '&reduce=' . $resolution; //$plan->Link;
                    $filename_plan = basename($plan->CR04_FILENAME);
                    if(file_exists($path . $filename_plan)) {
                        echo "file planimetry $filename_plan already exists \n";
                    }

                    else {
                        $img_plan = file_get_contents($url_plan); // get the image from the url
                        file_put_contents($path . $filename_plan, $img_plan); // create a file and feed the image
                        echo "file planimetry $filename_plan created \n";
                    }

                }
                $i++;



/*}
            else{
                echo "$estate->attributes(Riferimento)"."Deleted\n";
            }*/
        }


    ?>

I also have a problem with the first if commented:

if((string) $estate['ELIMINATO'] = "NO")...

Eliminato is an attribute of CR03_SCHEDE but the script won't read it and in any case go inside the if. The complete XML has about 70/80 properties and the foreach works well to download all images, but I need that it should download the only one that has that attribute equals to NO

This is the example of XML (only one estate): link

Thanks to all

Giuseppe V
  • 21
  • 6
  • I don't see a problem statement, I see "I need..." Show us what you've tried, and what your problem was. Also include data required to reproduce the problem **in the question**. See [mcve]. – miken32 Jun 14 '19 at 21:56
  • Hi this is the error when I try to execute with php script.php: PHP Parse error: syntax error, unexpected ''http://www.site.it/Im' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /mnt/c/Users/Giuseppe/Desktop/Download Image/Dowload_image.php on line 36 – Giuseppe V Jun 14 '19 at 22:17
  • Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – miken32 Jun 14 '19 at 22:18
  • I'll try to find a resolution with this link – Giuseppe V Jun 15 '19 at 19:26

1 Answers1

0

This is a classic mistake:

if((string) $estate['ELIMINATO'] = "NO")

You used the assignment operator instead of the comparison operator. Please use this exact form:

if ('NO' == (string)$estate['ELIMINATO'])
Christian Lescuyer
  • 18,893
  • 5
  • 49
  • 45