简体   繁体   English

使用php在我的服务器上下载图像

[英]Download image on my server using php

I have a div class for an image search button: 我有一个用于图像搜索按钮的div类:

<div class="form-group">
    <button onclick="myFunction()">Click me</button>
    <p id="demo"></p>
    <script>
        var link = "https://source.unsplash.com/all/?";
        var articleTitle = "<?php echo "'" . $article->title . "'"; ?>";
        function myFunction() {
            var d = new Date();
            var c = link + articleTitle + "[" + d.getTime()+"]";
            document.getElementById("demo").innerHTML = "<img src=" + c + " height='100' width='100'/>" ;
        }
    </script>
</div>

Each click on this button gives me a new image by my keyword ($article->title) with a unique http url. 每次单击此按钮,都会通过关键字($ article-> title)为我提供一个具有唯一http URL的新图像。

And I want to do one more button, which will save the image founded by first button on my server. 我想再做一个按钮,这将把第一个按钮创建的图像保存在服务器上。 I want to use something like this: 我想使用这样的东西:

<?php 
     $image = file_get_contents("c");
     file_put_contents('C:\xampp\htdocs\test\htdocs\image\image.jpg', $image);
?>

Where "c" is my link from first button. 其中“ c”是我从第一个按钮开始的链接。

So the question is: What should I change here: file_get_contents("c"); 所以问题是:我应该在这里更改什么: file_get_contents("c"); as I want place my url from first button into file_get_contents , because file_get_contents("c"); 我想将我的网址从第一个按钮放到file_get_contents ,因为file_get_contents("c"); this method didnt work. 这种方法没有用。

If I get you right, you want to save an image which is generated using javascript to your server, using php? 如果我理解正确,您想使用php将使用javascript生成的图像保存到您的服务器上吗? As php code is only executed on page load, you have to use a form submit and save the image in the target php file (which of course can be the same file) or you have to use an AJAX request to a second php file, which saves the image. 由于php代码仅在页面加载时执行,因此您必须使用表单提交并将图像保存在目标php文件(当然可以是同一文件)中,或者您必须对第二个php文件使用AJAX请求,保存图像。

A solution without AJAX could look something like that: 没有AJAX的解决方案可能看起来像这样:

<form name="save_img_form" action="[target file or same file]" method="[get/post]"
onsubmit="document.save_img_form.img_src = c; return true;">
 <input type="hidden" name="img_src" value="" />
 <input type="submit" value="save image" />
</form>

In order for this to work, you have to define the variable c globally. 为了使它起作用,您必须全局定义变量c。 Change the line var c = link ... to c = link ... , so the variable becomes global (compare here: http://www.w3schools.com/js/js_scope.asp ) 将行var c = link ...更改为c = link ... ,这样变量将变为全局变量(在此处进行比较: http : //www.w3schools.com/js/js_scope.asp

On the target page (or same page) you use something like this to retrieve the image source: 在目标页面(或同一页面)上,使用类似以下的方法来检索图像源:

<?php 
$img_src = $_GET["img_src"];  //or use $_POST, if you have used this method.
?>

Then you can do whatever you want with the image source. 然后,您可以对图像源进行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM