这一直让我感到压力。我正在调用一个 php 函数,该函数从用户输入的 url 中保存图像,然后回显一个 javascript 函数以将其设置为背景图像。除了将图像设置为背景外,一切工作正常。它保存在服务器和所有东西上。我没有收到任何 js 或 php 语法错误,浏览器只是说 404 not found 即使目录中有正确名称的文件。当我不包含会话变量作为图像名称的一部分时,它工作得很好。
在此先感谢,这可能只是我很愚蠢,因为我使用 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>';
?>