smproph Posted December 9, 2010 Share Posted December 9, 2010 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>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/221085-save-remote-image-to-directory/ Share on other sites More sharing options...
trq Posted December 9, 2010 Share Posted December 9, 2010 Javascript executes on the client, long after your php is finished being processed on the server. Quote Link to comment https://forums.phpfreaks.com/topic/221085-save-remote-image-to-directory/#findComment-1144773 Share on other sites More sharing options...
chronister Posted December 9, 2010 Share Posted December 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/221085-save-remote-image-to-directory/#findComment-1144775 Share on other sites More sharing options...
smproph Posted December 9, 2010 Author Share Posted December 9, 2010 I am new to php and did not know i could parse the email address using php instead of javascript. All I had to do was $ar=split("@",$email); $head = $ar[0]; $img[]= 'http://my.snu.edu/images/idpictures/'.$head.'-S.jpg'; Quote Link to comment https://forums.phpfreaks.com/topic/221085-save-remote-image-to-directory/#findComment-1144780 Share on other sites More sharing options...
chronister Posted December 9, 2010 Share Posted December 9, 2010 Remember, pretty well anything you can do with Javascript can be done with PHP. Happy Learning Quote Link to comment https://forums.phpfreaks.com/topic/221085-save-remote-image-to-directory/#findComment-1144791 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.