简体   繁体   English

从本地服务器检索PHP中的图像内容并保存在远程服务器中

[英]Retrieving image content in PHP from local server and save in remote server

$imgData = base64_encode(file_get_contents('F:\images\home\pic.jpg')); 

The above is my local host path. 以上是我的本地主机路径。 For local host, it is working. 对于本地主机,它正在工作。

But, how can I get it in my remote server? 但是,如何在我的远程服务器上获取它?

I have not worked on FTP. 我尚未使用FTP。 Please guide me. 请指导我。

I need to get the content of the image alone. 我只需要获取图像的内容。 I don't need to upload the file to remote server. 我不需要将文件上传到远程服务器。

You seem to be developing on a Windows machine because of the \\ directory separator. 由于\\目录分隔符,您似乎在Windows计算机上进行开发。 It is best practice to use forward-slash / as it will work on both Windows and Linux, provided you use relative paths (the path with reference to root directory is called absolute. The path with reference to current directory is called relative). 最佳做法是使用正斜杠/因为它在Windows和Linux上都可以使用前提是您使用相对路径 (参考根目录的路径称为绝对路径。参考当前目录的路径称为相对路径)。 You should aim to use relative paths whenever possible. 您应该尽可能使用相对路径。

Example: 例:

  • F:/images/home/pic.jpg is an absolute path. F:/images/home/pic.jpg是绝对路径。 Notice I replaced your backslash \\ with a forwardslash / . 注意,我将您的反斜杠\\替换为正斜杠/
  • http://www.example.com/images/pic.png is an absolute path. http://www.example.com/images/pic.png是绝对路径。
  • ./pic.jpg is a relative path and is equivalent to pic.jpg ./pic.jpg是相对路径,等效于pic.jpg
  • ../images/pic.jpg ia a relative path ( .. means you've exited the current folder, entering the working directory where the images folder is located too). ../images/pic.jpg一个相对路径( ..表示您已经退出当前文件夹,也进入了images文件夹所在的工作目录)。

On your remote (Linux) server, you will have to know the relative path 在远程(Linux)服务器上,您将必须知道相对路径

Let's assume your remote server is a linux/apache server. 假设您的远程服务器是linux / apache服务器。 Maybe you should also tag your question with apache instead of javascript/angular ? 也许您还应该使用apache而不是javascript/angular标记问题?

You should determine the relative path to your image and use the examples above to rewrite your code to something like this: 您应该确定图像的相对路径,并使用上面的示例将代码重写为如下所示:

$imgData = base64_encode(file_get_contents('../images/home/pic.jpg'));

You can also use an absolute path ( http://www.example.com/images/pic.png ) but that is not always possible or recommended. 您也可以使用绝对路径( http://www.example.com/images/pic.png ),但这并不总是可行或建议的。

How to find the relative path to your image? 如何找到图像的相对路径? There are multiple ways to do that, using PHP, SSH or FTP. 使用PHP,SSH或FTP有多种方法可以做到这一点。 Your FTP software will show you the path to your image if you navigate to it, but you need to adapt it to your working directory path. 如果浏览到FTP,FTP软件将向您显示图像的路径,但是您需要根据您的工作目录路径对其进行调整。 This image is from Filezilla, an open source FTP client: 此图像来自Filezilla,这是一个开放源代码FTP客户端:

在此处输入图片说明

In this case, in /home/bg29ll/public_html/food2/images that you see in Filezilla, food2 is your working directory, so you would only need to use: 在这种情况下,在您在Filezilla中看到的/home/bg29ll/public_html/food2/images中, food2是您的工作目录,因此您只需要使用:

$imgData = base64_encode(file_get_contents('images/pic.jpg'));

You might also include this line in your php code and run it on your server: 您可能还会在您的php代码中包含以下行,并在您的服务器上运行它:

print_r( dirname(__FILE__) );

This will let you know what your absolute path is. 这会让您知道您的绝对路径是什么。

EDIT: to save the image in a database (although I would recommend to upload images to the server and only save the location/name into the DB) you would have to use a blob column type (one of its variations, TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB ). 编辑:要将图像保存在数据库中(尽管我建议将图像上传到服务器,并且仅将位置/名称保存到数据库中),您必须使用blob列类型(其变体之一, TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB )。 Mediumblob would allow for file sizes up to 16 MB. Mediumblob允许的文件大小最大为16 MB。

CREATE TABLE images (
    id   INT           AUTO_INCREMENT PRIMARY KEY,
    mime VARCHAR       (255) NOT NULL,
    data MEDIUMBLOB          NOT NULL
);

You would then use something like this to insert an image in the DB (using PDO ): 然后,您将使用类似这样的方式在DB中插入图像(使用PDO ):

    $blob = fopen($filePath, 'rb');

    $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)";
    $stmt = $this->pdo->prepare($sql);

    $stmt->bindParam(':mime', $mime);
    $stmt->bindParam(':data', $blob, PDO::PARAM_LOB);

    $stmt->execute();

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

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