Php 如何使用curl保存表单中提交的文件url?

Php 如何使用curl保存表单中提交的文件url?,php,forms,file-upload,curl,Php,Forms,File Upload,Curl,我正在尝试创建一个上传插件,允许用户从他们的计算机上传任何文件,或者从他们在提供的文本字段中键入的url上传任何文件 这是我必须从本地磁盘上载文件的脚本: session_start(); //Loop through each file for($i=0; $i<count($_FILES['file']); $i++) { //Get the temp file path if (isset($_FILES['file']['tmp_name'][$i])) { $tm

我正在尝试创建一个上传插件,允许用户从他们的计算机上传任何文件,或者从他们在提供的文本字段中键入的url上传任何文件

这是我必须从本地磁盘上载文件的脚本:

session_start();
//Loop through each file
for($i=0; $i<count($_FILES['file']); $i++) {
  //Get the temp file path
  if (isset($_FILES['file']['tmp_name'][$i]))
  {
  $tmpFilePath = $_FILES['file']['tmp_name'][$i];
  }

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    if (isset($_FILES['file']['name'][$i]))
    $newFilePath = "./uploaded_files/" . $_FILES['file']['name'][$i];
    }

    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

    echo "Uploaded Successfully!<br />";

}

您想使用curl的具体原因是什么?以下是您如何在没有它的情况下简单地做到这一点:

$url = $_POST['url'];
$file_content = file_get_contents($url);
$file_name = array_pop(explode('/', parse_url($url, PHP_URL_PATH)));
file_put_contents('/home/path/' . $file_name, $file_content);

你也应该考虑查看URL并在使用之前检查它是否有效。

确切地说,问题是什么?@ GoGoLeGe我想在用户提交URL的前端有一个文本输入框。所述url然后通过卷曲。我不能用我的代码来做那件事。我可以向curl中添加什么来告诉它查找提交的文本字段?当问题被明确标识为重复时,为什么要删除并重新发布此问题?使用
if(isset($\u post['url'))
?我猜您正在处理图像,所以检查正确的图像格式和大小将是一个好的开始。我最终会接受所有的文件类型,但现在,当我处理语法并使一切正常时…是的,图像。
$url = $_POST['url'];
$file_content = file_get_contents($url);
$file_name = array_pop(explode('/', parse_url($url, PHP_URL_PATH)));
file_put_contents('/home/path/' . $file_name, $file_content);