Image Downloading small problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anthony2oo5
    New Member
    • Jun 2007
    • 26

    Image Downloading small problem

    Hey,

    I'm trying to remotely download an image to my server via the web and an URL that the user inputs. I have managed to do this with a cron job, and write the file to the system no problem.

    My problem lies with viewing the image, the image wont show. I looked at the source code of the image and it seems to be because PHP is writing data to the top of the file,

    Code:
    HTTP/1.1 200 OK Set-Cookie: JCPARPT=ZLNIZZSSRVDXECKKMO; path=/ Content-Length: 9566 Content-Type: image/jpeg Last-Modified: Fri, 05 May 2006 22:14:11 GMT Accept-Ranges: bytes ETag: "8811773c9170c61:9be" Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET Date: Wed, 13 Jun 2007 14:13:08 GMT ÿØÿà�JF
    Is there anyway I can stop PHP doing this, or remove it from the code with some sort of preg_match before writing it to disk.

    Or is there an alternative method I should be using?

    thanks in advance for any time and help.
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    what is your code for getting the image?

    Comment

    • Anthony2oo5
      New Member
      • Jun 2007
      • 26

      #3
      [PHP]<?php
      include ('includes/functions.php') ;
      $Error = "";
      $UploadDIR = 'images/';
      $ShortenTo = 15;

      function RemoteGet($addr ess) {
      // Connect to the google website to find the millage
      $ch = curl_init();
      curl_setopt($ch , CURLOPT_URL, $address);
      curl_setopt($ch , CURLOPT_HEADER, 1);
      curl_setopt($ch , CURLOPT_RETURNT RANSFER, 1);
      $data = curl_exec($ch);
      curl_close($ch) ;
      echo $data;
      // Make a random string
      $RandomString = RandomString(4) ;
      // Get the file extention to put back later
      $FileExt = GetFileExt($add ress);
      // Make it lowercase
      strtolower($Fil eExt);
      // Check its the correct extention
      if (($FileExt == 'jpg') || ($FileExt == 'gif') || ($FileExt == 'png') || ($FileExt == 'bmp')) {
      // Remove the file extention from the file
      $file = RemoveExtension ($address);
      // Now clean anything thats not alphanumeric
      $file = preg_replace('`[^a-z0-9]`i', '', $file);
      // Shoerten the filename
      $file = substr($file, 0, $ShortenTo);
      // Put the extention back on
      $file = $file . '.' . $FileExt;
      // Set the path
      $uploadfile = $UploadDIR . $RandomString . $file;
      // The filename is
      $FileName = $RandomString . $file;
      // Write the file to disk
      $fh = fopen($FileName , 'w') or die("can't open file");
      fwrite($fh, $data);
      fclose($fh);
      return "http://" . $_SERVER[HTTP_HOST] . "/Image-" . $FileName;
      }
      }

      echo RemoteGet("http ://google.com/images/google.gif");

      ?>[/PHP]

      Comment

      • Motoma
        Recognized Expert Specialist
        • Jan 2007
        • 3236

        #4
        Rather than perform a curl call (which is likely where the headers are coming from), just fopen the URL directly.

        Comment

        • Anthony2oo5
          New Member
          • Jun 2007
          • 26

          #5
          Originally posted by Motoma
          Rather than perform a curl call (which is likely where the headers are coming from), just fopen the URL directly.
          Will try that in a sec, thanks.

          Was going to use that but thought curl was quicker.

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #6
            Originally posted by Anthony2oo5
            Will try that in a sec, thanks.

            Was going to use that but thought curl was quicker.
            Let us know how it turns out.

            Comment

            • Anthony2oo5
              New Member
              • Jun 2007
              • 26

              #7
              Ok, now it sort of works lol. But the file only seems to half download, Check here:

              [ur]http://piczy.net/Image-MCTWK.gif[/url]

              I changed the Cron job with the following:

              [PHP]$fh = fopen($address, 'r') or die("Can't open file");
              $data = fread($fh, 524288000000);
              fclose($fh);[/PHP]

              Note all the 0000000 at the end was me upping the limit to see if thats why it was stopping. But it wasnt.

              Any ideas? This must be possible and cant be this hard lol.

              Thanks for your time.

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Wait!

                Am I correct in my understanding that you are using cron to execute a PHP script?

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Try using file_get_conten ts, or better yet, fread instead.

                  Comment

                  • Anthony2oo5
                    New Member
                    • Jun 2007
                    • 26

                    #10
                    Originally posted by Motoma
                    Wait!

                    Am I correct in my understanding that you are using cron to execute a PHP script?
                    no, I have the above code in a php file, then just run the file directly. I had curl to get the image (i may have said cron before sorry), but changed it to fopen as you said.

                    Will have a go of file get contents.

                    Comment

                    • Motoma
                      Recognized Expert Specialist
                      • Jan 2007
                      • 3236

                      #11
                      Straight from PHP.net

                      When reading from anything that is not a regular local file, such as streams returned when reading remote files or from popen() and fsockopen(), reading will stop after a packet is available. This means that you should collect the data together in chunks as shown in the examples below.

                      [code=php]
                      <?php
                      $handle = fopen("http://www.example.com/", "rb");
                      $contents = '';
                      while (!feof($handle) )
                      {
                      $contents .= fread($handle, 8192);
                      }
                      fclose($handle) ;
                      ?>
                      [/code]

                      Comment

                      • Anthony2oo5
                        New Member
                        • Jun 2007
                        • 26

                        #12
                        YEY, file get contents works great thanks very much for all the help.

                        May I have one request?, can I have the code from my second post removed please? Everything else can stay.

                        Once again thanks all.

                        Comment

                        • Motoma
                          Recognized Expert Specialist
                          • Jan 2007
                          • 3236

                          #13
                          Originally posted by Anthony2oo5
                          YEY, file get contents works great thanks very much for all the help.

                          May I have one request?, can I have the code from my second post removed please? Everything else can stay.

                          Once again thanks all.
                          We don't work like that...I'm sure something could be censored out if you need it to.

                          Comment

                          • Anthony2oo5
                            New Member
                            • Jun 2007
                            • 26

                            #14
                            Just didn't want hackers knowing my script. can you censor like 4 and 40 please.

                            Thanks :)

                            Comment

                            • Motoma
                              Recognized Expert Specialist
                              • Jan 2007
                              • 3236

                              #15
                              Done!

                              Come back with any more questions.

                              Comment

                              Working...