在PHP中通过curl上传文件

时间:2014-07-24 20:50:12

标签: php curl file-upload

过去一天,我对这个问题感到非常震惊。 我必须通过php通过curl上传用户选择的文件。 我已经完成了所有先前提到的有关相关主题的问题,所做的更改超过100次,但我仍无法找到解决方案并将文件上传到/ work / uploads目录中的localhost。 实际上我的代码从未运行过 有关它的最新消息是我的isset()每次都返回false。 这是我的代码

upload_file.html //表单部分

<form action="cm_upload.php" method="Post" enctype="multipart/form-data">               
<p align="center">              
<label for="file">Choose File:</label> <input type="file" name="file" id="file">
<input type="submit" name="submit" value="Upload File">
</p></form>

cm_upload.php

<?php
if (isset($_POST['file']))
{
    $url = "http://127.0.0.1/work/accept.php"; 
    $filename = $_FILES['file']['name'];
    $filedata = $_FILES['file']['tmp_name'];
    if ($filedata != '')
    {

        $postfields = array("filedata" => "@$filedata", "filename" => $filename);
        $ch = curl_init();
        $options = array(
            CURLOPT_URL => $url,
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $postfields,
            CURLOPT_RETURNTRANSFER => true
        ); // cURL options
        curl_setopt_array($ch, $options);
        curl_exec($ch);
        if(!curl_errno($ch))
        {
            $info = curl_getinfo($ch);
            if ($info['http_code'] == 200)
                $errmsg = "File uploaded successfully";
                echo "file uploaded successfully";
        }
        else
        {
            $errmsg = curl_error($ch);
            echo "failed to upload file";
        }
        curl_close($ch);
    }
    else
    {
        $errmsg = "Please select the file";
    }
}
else
echo "file not set";
?>

这是我的accept.php代码

<?php

$uploadpath = "work/uploads/";
$filedata = $_FILES['filedata']['tmp_name'];
$filename = $_POST['filename'];
if ($filedata != '' && $filename != '')
    copy($filedata,$uploadpath.$filename);
?>

请帮帮我..谢谢你:)

0 个答案:

没有答案