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
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

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.