Jump to content

Save Remote Image to Directory


smproph

Recommended Posts

I'm trying to save images from a directory into mine. To get the image I am having to take the email from a database, split it and take whatever it is before the '@' sign and add it to "-S.jpg". I wrote the script and when I echo the variable it shows the correct thing, but when it tries to save it , it is trying to find the image as "script>-S.jpg". It looks like it is taking whatever is after the last '/' which in the variable since I am running javascript it is going to be </script> if you look at my variable $url. Here is the code below. Any help is appreciated.

 

while($rows=mysql_fetch_array($result)){
	$email=$rows['email'];


$url= "<SCRIPT LANGUAGE=\"javascript\">
var url;
var email = \"$email\";
function emailsplit () 
{ 
var userid = email.split(\"@\"); 
var url = userid[0]; 
var imgid = \"http://my.snu.edu/images/idpictures/\" + url + \"-S.jpg\"; 
return url;
}  
document.write(emailsplit());
</script>
";

         $img[]= 'http://my.snu.edu/images/idpictures/'.$url.'-S.jpg';
  
   

}

  
       
    
      function save_image($img,$fullpath='basename'){

          if($fullpath=='basename'){

              $fullpath = basename($img);
  
          }
          $ch = curl_init ($img);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
          $rawdata=curl_exec($ch);
          curl_close ($ch);
          if(file_exists($fullpath)){
              unlink($fullpath);
          }
          $fp = fopen($fullpath,'x');
          fwrite($fp, $rawdata);
          fclose($fp);
      }

  
      foreach($img as $i){
   
          save_image($i); 
   
          if(getimagesize(basename($i))){
   
              echo '<h3 style="color: green;">Image ' . basename($i) . ' Downloaded OK</h3>';
  
          }else{
  
              echo '<h3 style="color: red;">Image ' . basename($i) . ' Download Failed</h3>';
  
          }

      }

Link to comment
https://forums.phpfreaks.com/topic/221085-save-remote-image-to-directory/
Share on other sites

I don't understand why people use double quotes and escape the hell out of them when you can simply use a single quote which is a literal string (little performance gain since the PHP parser does not look for variables in them), and concatenate things....

$url= '<SCRIPT LANGUAGE="javascript">
var url;
var email = "'.$email.'";
function emailsplit () 
{ 
var userid = email.split("@"); 
var url = userid[0]; 
var imgid = "http://my.snu.edu/images/idpictures/" + url +"-S.jpg"; 
return url;
}  
document.write(emailsplit());
</script>';

$img[]= 'http://my.snu.edu/images/idpictures/'.$url.'-S.jpg';

 

 

Do you realize that the $url var right before -S.jpg, is going to contain the $url var which you declared... which is the entire script block above???

 

So it basically becomes

$img[] = 'http://my.snu.edu/images/idpictures/<SCRIPT LANGUAGE="javascript">
var url;
var email = "'.$email.'";
function emailsplit () 
{ 
var userid = email.split("@"); 
var url = userid[0]; 
var imgid = "http://my.snu.edu/images/idpictures/" + url +"-S.jpg"; 
return url;
}  
document.write(emailsplit());
</script>-S.jpg';

 

That is not exactly right, but the $url var in the $img[] array is not using the url var inside your script. Javascript is executed on the client side, and PHP is executed on the server... so the PHP is done by the time this javascript even has a chance to run.

 

Unless I missed something here, you should go back to the drawing board on the logic of this code.

 

Nate

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.