简体   繁体   English

使用图像URL和PHP将图像从远程服务器上传/下载/保存到本地服务器?

[英]Upload/download/save image to a local server from a remote server using an Image URL and PHP?

I am building a Discussion Forum as part of a bigger application I am building, the forum is just 1 section of the Application. 我正在构建一个讨论论坛,作为我正在构建的更大应用程序的一部分,该论坛只是该应用程序的一部分。

For my TextArea fields when posting a new Topic or a Post Reply, I have decided that nothing is as good as the PageDown Markdown Library . 对于发布新主题或发布回复时的TextArea字段,我认为没有什么比PageDown Markdown Library更好。 It is the same one that StackOverflow uses on all their sites and it works better than many of it's competitors. StackOverflow在其所有站点上使用的都是同一款产品,并且比许多竞争对手更有效。

The way the library ships though, I am not happy with the default Insert Image functionality. 虽然该库的发布方式对默认的“ Insert Image功能不满意。 You hit the button to insert an image and it allows you to enter a URL for an Image and then it inserts the proper MarkDown syntax to show the linked image. 您单击按钮以插入图像,它允许您输入图像的URL,然后插入正确的MarkDown语法以显示链接的图像。

This just won't cut it. 这只是不会削减。 I need the functionality that you see on StackOverflow! 我需要您在StackOverflow上看到的功能! Very similar anyways. 反正很相似。

I need it to show a Dialog when you click the Insert Image button, like it does now, but instead of just an input field for a Image URL, it will have 2 filed options... 当您单击“插入图像”按钮时,我需要它显示一个对话框,就像现在一样,但是它不仅具有图像URL的输入字段,还将具有2个字段选项...

  • Upload image from your computer 从您的计算机上传图像
  • Insert an Image URL and it will then DOWNLOAD the image from that URL and insert it into the post just as if you had uploaded it from your computer. 插入图像URL,然后它将从该URL下载图像并将其插入到帖子中,就像您是从计算机上载的一样。 This is important to not confuse this step. 这对于不要混淆此步骤很重要。 IT should not simply insert the Image linking it to the original Image URL. IT人员不应简单地插入将其链接到原始图像URL的图像。 Instead it will take that URL and download/upload the Image to the same server that the upload from computer option does and then it will insert the NEW Image URL pointing to the newly uploaded image! 取而代之的是,它将使用该URL并将图像下载/上传到与upload from computer选项相同的服务器upload from computer ,然后它将插入指向新上载图像的NEW Image URL!

Based on some simple HTML like below for a Dialog window with a filed for my Upload from Computer functionality, which I already have working. 基于一些简单的HTML,例如下面的“对话”窗口,其中包含我已经从“计算机上载”功能提交的文件。 I need to come up with some JavaScript and PHP that will download/save a remote image to my upload folder on my server when a button is clicked using only the URL that will be inside the URL text input field. 我需要提供一些JavaScript和PHP,当仅使用URL文本输入字段中的URL单击按钮时,它们将下载/保存远程图像到服务器上的上传文件夹。

So it will need to do a few things... 因此,它需要做一些事情...

  • Fetch and save an image file to my uploads folder using PHP when the only thing that the PHP function will receive is a URL of the image which could be on the same server or most likely a remote server. 当PHP函数将接收的唯一内容是图像的URL时,可以使用PHP提取图像文件并将其保存到我的上载文件夹中,该图像可以位于同一服务器上,也可以位于远程服务器上。
  • After successfully saving/uploading an image from the URL, the PHP function will return a JSON string with the status/error and if successful then it will also return the actual URL and filename of where the new image is saved on the local server. 从URL成功保存/上传图像后,PHP函数将返回状态/错误的JSON字符串,如果成功,则还将返回实际图像和本地服务器上新图像的保存位置的URL和文件名。 The JavaScript/AJAX script will receive this JSON response and insert the Markdown syntax for the image into the PageDown editor. JavaScript / AJAX脚本将收到此JSON响应,并将图像的Markdown语法插入PageDown编辑器。
  • The PHP function will need to ensure that the URL that it is trying to save/download is a valid image file and not some malicious file! PHP函数将需要确保它尝试保存/下载的URL是有效的图像文件,而不是某些恶意文件! Also not simply just some file of the wrong filetype like a non-image file unless we are allowing the file type. 除非我们允许该文件类型,否则不只是错误文件类型的文件,例如非图像文件。
  • It will be part of a module installed on many dinosaur servers so it needs to work on as many servers as possible too! 它是许多恐龙服务器上安装的模块的一部分,因此它也需要在尽可能多的服务器上工作!

    From the web 通过网络

    From your computer 从您的计算机

I would be greatful of any help, tips, code snippets or anything to help with this. 如果有任何帮助,技巧,代码段或任何有关此方面的帮助,我将不胜感激。 At this stage I really just need to build a nie PHP function that will upload images from a remote URL and also ensure that the URL passed in is a real image file or even better that it is in the allowed file types array! 在此阶段,我真的只需要构建一个nie PHP函数即可从远程URL上传图像,并确保传入的URL是真实图像文件,甚至更好的是它位于允许的文件类型数组中!

A couple years ago I had started this but have now lost it and I am starting over and don't remeber much about how I went about doing it then. 几年前,我已经开始这样做了,但是现在却迷失了方向。

The easiest way to download a file from a remote server would be to use copy ( http://php.net/manual/en/function.copy.php ): 从远程服务器下载文件的最简单方法是使用copy( http://php.net/manual/en/function.copy.php ):

copy('http://someurl.com/image.png', '/var/www/uploads/image.png');

As this function returns a bool, it is easy to determine whether the operation was successful and create a JSON response. 由于此函数返回布尔值,因此很容易确定操作是否成功并创建JSON响应。

To verify that the file is an actual image, there is unfortunately no way that is 100% sure. 要验证该文件是否为实际图像,很遗憾,无法百分百确定。 It is probably enough to check the mimetype though. 虽然可能足以检查模仿类型。 You can use finfo for that ( http://php.net/manual/en/function.finfo-file.php ): 您可以为此使用finfo( http://php.net/manual/en/function.finfo-file.php ):

$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, $filename);
finfo_close($finfo);

For a gif, this would return image/gif for example. 对于gif,例如,将返回image / gif。 You will have to hardcode a list of all mimetypes you want to allow. 您将必须对要允许的所有mimetypes列表进行硬编码。

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

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