简体   繁体   English

PHP-无格式访问/上传本地文件到服务器。 在API上使用PUT file://

[英]PHP - accessing / uploading local file to server without a form. Using PUT file:// on API

I have written a script to upload an image file to a server using file_get_contents(); 我编写了一个脚本,使用file_get_contents();将图像文件上传到服务器file_get_contents(); this works when using a relative file path eg. 这在使用相对文件路径时有效。 /var/test/image1.jpg and also when I use a remote url eg. /var/test/image1.jpg以及当我使用远程URL时。 http://domain.com/test/image1.jpg . http://domain.com/test/image1.jpg

However, it does not work for local paths ie. 但是,它不适用于本地路径,即。 file://C:/Users/me/Pictures/image1.jpg. 文件:// C:/Users/me/Pictures/image1.jpg。 I get, " file_get_contents(file://C:/Users/me/Pictures/image1.jpg) [function.file-get-contents]: failed to open stream: No such file or directory" or "remote host file access not supported". 我得到“ file_get_contents(file://C:/Users/me/Pictures/image1.jpg) [function.file-get-contents]:无法打开流:没有这样的文件或目录”或“远程主机文件访问不支持”。

I really need this to work for local paths so that remote clients can upload their local images to my server via an API. 我确实需要使用它来处理本地路径,以便远程客户端可以通过API将本地图像上传到我的服务器。 I therefore cannot use a form and need to use the PUT method specified here ([http://www.php.net/manual/en/features.file-upload.put-method.php][1]) but how to I access the file? 因此,我无法使用表格,需要使用此处指定的PUT方法([http://www.php.net/manual/en/features.file-upload.put-method.php][1]),但是如何使用我访问文件了吗?

The code for my API is below but am trying to figure out how to get the file contents from a clients local machine: 我的API的代码如下,但是我试图弄清楚如何从客户端本地计算机获取文件内容:

if($_SERVER['REQUEST_METHOD'] == 'PUT') {
    $input = file_get_contents("php://input"); //filepath passed from client in XML

//Convert the XML to an object we can deal with
$xml = simplexml_load_string($input);   

$file_path = $xml->image->filepath; 

if  (file_exists($file_path) ){

    $file_data = file_get_contents($file_path);

    //Initialise the Curl Instance
    $ch = curl_init();

    //Set our Curl Options
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $file_data);

    //Execute the request
    $response = curl_exec($ch);

    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    //Close the Handle
    curl_close($ch);
}

You shouldn't specify file:// To access local paths you can directly do 您不应该指定file://来直接访问本地路径

file_get_contents('C:/Users/me/Pictures/image1.jpg');

You can also navigate relative to current directory, using ../ based navigation like below 您还可以使用基于../的导航相对于当前目录,如下所示

file_get_contents('../../../somefile.xyz');

EDIT: If you are trying to access a client-side file , ie remote paths that way, that wouldn't work . 编辑:如果您试图访问客户端文件 ,即以这种方式访问​​远程路径,那将不起作用 You need something like HTTP, FTP etc to transfer your file over. 您需要HTTP,FTP等传输文件。 Simply giving a local file-system address isn't sufficient. 仅提供本地文件系统地址是不够的。

The end-point needs to somehow make the file available (HTTP/FTP server) or you need to get the client to upload it via the browser and an <input> field. 终点需要以某种方式使该文件可用(HTTP / FTP服务器),或者你需要让客户端上传通过浏览器和它<input>字段。

There's no way that you can use file_get_contents() to open any files from the remote client's computer, unless they run it locally (they would also need to set up PHP for that) and then somehow submit it to your server. 您无法使用file_get_contents()从远程客户端计算机上打开任何文件,除非它们在本地运行(它们也需要为此设置PHP),然后以某种方式将其提交到您的服务器。

file_get_contents() allows you to open files that are on the same machine with your script, not on different ones file_get_contents()允许您打开与脚本在同一计算机上的文件,而不是在不同计算机上的文件

You can take a look at http://www.tizag.com/phpT/fileupload.php for an easy example of file uploaders 您可以在http://www.tizag.com/phpT/fileupload.php中查看文件上传器的简单示例

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

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