上使用PUT文件://我已經編寫了一個腳本來使用file_get_contents();
將圖像文件上傳到服務器,這在使用相對文件路徑(例如, /var/test/image1.jpg
以及當我使用遠程URL例如。 http://domain.com/test/image1.jpg
。PHP - 在沒有表單的情況下訪問/上傳本地文件到服務器。在API
但是,它不適用於本地路徑,即。文件:// C:/Users/me/Pictures/image1.jpg。我得到,「file_get_contents(file://C:/Users/me/Pictures/image1.jpg)
[function.file-get-contents]:未能打開流:沒有這樣的文件或目錄」或「不支持遠程主機文件訪問」。
我真的需要這樣做,以便遠程客戶端可以通過API將本地映像上傳到我的服務器。因此,我不能使用表單並需要使用此處指定的PUT方法([http://www.php.net/manual/en/features.file-upload.put-method.php][1]),但是如何我訪問該文件?
我的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);
}
奇怪的一個簡單的例子,不要評論...... upvoted正因爲如此。 – Asken
你想做什麼?你有沒有上傳圖片並試圖將其提供給用戶?它看起來像我試圖訪問客戶端文件時使用file:// C:/ Users ... – Asken
@Asken,感謝有點困惑的downvote自己!我正嘗試從遠程計算機上傳文件而不使用表單。我正在使用Curl。 – LeeTee