[英]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 from computer
option does and then it will insert the NEW Image URL pointing to the newly uploaded image! 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... 因此,它需要做一些事情...
It will be part of a module installed on many dinosaur servers so it needs to work on as many servers as possible too! 它是许多恐龙服务器上安装的模块的一部分,因此它也需要在尽可能多的服务器上工作!
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.