This has been stressing me out. I’m calling a php function that saves an image from a user input url, then echos a javascript function to set it as the background image.
Everythings working fine except setting the image as the background. Its saved on the server and everything. Im not getting any js or php syntax errors, the browser just says 404 not found even though the file with the correct name is there in the directory. It works completely fine when i dont include the session variable as part of the image name.
Thanks in advance, this is probably just me being stupid as its been a while since ive used php.
<?php
session_start();
$src = $_GET['image'];
$contents= file_get_contents($src);
$file = fopen( pic.$_SESSION['id'].'.jpg','w+');
fwrite($file, $contents);
fclose($file);
echo '<script>';
echo 'var body = document.getElementsByTagName("body")[0];';
echo 'var source = "pic'.$_SESSION['id'].'.jpg";';
echo '$(body).css("background-image", "url(source)");';
echo 'alert(source);'; //just to test to make sure it returns the correct name
echo '</script>';
?>
Note, it’s always good to check your JavaScript code when it is rendered by the server-side. You may always check “Source Code” in any browser, or use special developer tools.