简体   繁体   English

使用PHP将网页上的文件上传到服务器

[英]Upload a file from web to a server in PHP

I have a php code which uploads my photos to a server. 我有一个php代码,可将我的照片上传到服务器。 The code works fine for local files, but it is not working for files from Web. 该代码适用于本地文件,但不适用于Web中的文件。 Below is my code for uploading local files 下面是我上传本地文件的代码

<?php

    $filepath="@/home/path/to/my/file.png";
    $serverurl ="https://cxdetails.com/upload.php";
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_VERBOSE,0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_URL, $serverurl);
    curl_setopt($ch,CURLOPT_POST,true);
    $post=array("file"=>$filepath);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
    $response=curl_exec($ch);
    echo $response;

?>

but it is not working if I change the $filepath to " http://www.howtocheck.zu/path/sarah.png ". 但是如果我将$ filepath更改为“ http://www.howtocheck.zu/path/sarah.png ”,它将无法正常工作。 Am not sure why this is not working. 不知道为什么这不起作用。 Please help me out on this. 请帮助我。

Thanks in advance. 提前致谢。

Can u try replace: 您可以尝试更换:

curl_setopt($ch,CURLOPT_POSTFIELDS,$post);

to: 至:

curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($post));

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

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