【问题标题】:Saving Path and file info to mysql while using curl使用 curl 时将路径和文件信息保存到 mysql
【发布时间】:2023-03-11 02:08:01
【问题描述】:

我正在使用共享服务器。

我可以使用 curl 保存外部图像。但我无法将路径和文件信息保存到 mysql。

<form method="post" enctype="multipart/form-data">
<table>
 <tr><td><b>URL: </b> </td><td><input type="**text**" name="url" id="url" size=40></td>  </tr>
 <tr><th colspan=2><p><input name="upload" type="submit" class="box" id="upload" value="Upload"></p></th></tr>
 </table>
 </form>

 <?php

 set_time_limit(0);
 ini_set('display_errors',true);//Just in case we get some errors, let us know....

 if (isset($_POST['upload']))
 {

  $url = $_POST['url'];
  $dir = "./files/";

  $fileName = $_FILES['url']['name'];
  $fileSize = $_FILES['url']['size'];
  $fileType = $_FILES['url']['type'];
  $filePath = $dir . $fileName;

  $ch = curl_init($url);
  $fp = fopen ($local_file, 'w+');
  $ch = curl_init($remote_file);
  curl_setopt($ch, CURLOPT_TIMEOUT, 50);
  curl_setopt($ch, CURLOPT_FILE, $fp);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_ENCODING, "");
  curl_exec($ch);
  curl_close($ch);

  include ('db.php');
  if(!get_magic_quotes_gpc())
  {
  $fileName = addslashes($fileName);
  $filePath = addslashes($filePath);
  }
  $query = "INSERT INTO upload2 (name, size, type, path ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
 mysql_query($query);
 mysql_close($conn);
 }

当我将输入类型更改为文件中的文本时,它不起作用。

我需要获取文件信息并保存到mysql服务器

【问题讨论】:

  • 我想你已经在这里删除了一大堆 cURL 代码?我很清楚,您想要的是从用户的计算机上传文件,而不是通过 cURL 从 URL 获取它?还是您想要选择两者之一?
  • Yaa curl 评论在此处添加代码时被删除,现在已添加。我希望创建一个以 url 作为输入并将文件保存到服务器的表单。类似example.com/example.jpg
  • 对于这么简单的事情,我建议你使用file_get_contents() 而不是搞乱cURL。虽然 cURL 让您可以更精细地控制获取文件的请求,但这不是您需要的,如果您需要添加一些标头或(出于某种奇怪的原因)POST 来获取文件,您可以使用流来做到这一点上下文。
  • file_get_contents() 在我的共享服务器上不起作用:(
  • allow_url_fopen 是否已禁用 (var_dump(ini_get('allow_url_fopen'));)?似乎很奇怪,他们应该允许 cURL 但不允许 allow_url_fopen...

标签: php mysql curl


【解决方案1】:

试试这个:

<form method="post" enctype="application/x-www-form-urlencoded">
  <table>
    <tr>
      <td style="font-weight:bold;">URL:</td>
      <td><input type="text" name="url" id="url" size=40 value="http://" /></td>
    </tr>
    <tr>
      <!-- Why is this a <th> and not a <td> ? -->
      <th colspan="2"><input name="upload" type="submit" class="box" id="upload" value="Upload" /></th>
    </tr>
  </table>
</form>

<?php

  set_time_limit(0);
  ini_set('display_errors',true);//Just in case we get some errors, let us know....

  if (isset($_POST['upload'])) {

    // Build local file system paths
    $baseDir = "./files";
    $fileName = basename(rtrim($_POST['url'],'/')); // This means that if someone specifies the root of a domain (/) it will call the local file "domain"
    $filePath = rtrim($baseDir,'/')."/$fileName";

    // Try to parse the URL we have been given and add any missing required info
    if ((!$parts = parse_url($_POST['url'])) || !isset($parts['host'])) die('ERROR: The specified URL is invalid');
    $url = (isset($parts['scheme']) && $parts['scheme']) ? $parts['scheme'].'://' : 'http://';
    if (isset($parts['user']) && $parts['user']) {
      $url .= $parts['user'];
      if (isset($parts['pass']) && $parts['pass']) $url .= ':'.$parts['pass'];
      $url .= '@';
    }
    $url .= $parts['host'];
    if (isset($parts['port']) && $parts['port']) $url .= ':'.$parts['port'];
    $url .= (isset($parts['path']) && $parts['path']) ? $parts['path'] : '/';
    if (isset($parts['query']) && $parts['query']) $url .= '?'.$parts['query'];
    if (isset($parts['fragment']) && $parts['fragment']) $url .= '#'.$parts['fragment'];

    print("Fetching file '$url' to '$filePath'<br />\n");

    // Open a local file point in write mode
    if (file_exists($filePath)) die("ERROR: The file '$filePath' already exists");
    $fp = fopen($filePath,'w') or die("ERROR: Could not open local file '$filePath' for writing!");

    // Let's go cURLing...
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_ENCODING, "");
    curl_exec($ch);
    $fileSize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    $fileType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    $transferTime = curl_getinfo($ch, CURLINFO_TOTAL_TIME);

    print("cURL operation complete (took $transferTime seconds)<br />\n");

    // Close cURL and the local file pointer, we're done with them both
    curl_close($ch);
    fclose($fp);

    // Check the $fileSize reported by cURL is the same as the file size on disk
    if ($fileSize != ($fs = filesize($filePath))) {
      print("WARNING: file size reported by cURL ($fileSize bytes) does not match size of file on disk ($fs bytes)<br />\nUsing size of file on disk for DB insert<br />\n");
      $fileSize = $fs;
    }

    include ('db.php');
    $query = "INSERT INTO upload2 (name, size, type, path ) ".
    "VALUES ('".mysql_real_escape_string($fileName)."', '$fileSize', '$fileType', '".mysql_real_escape_string($filePath)."')";

    print("Running MySQL Query: $query<br />\n");

    mysql_query($query);
    mysql_close($conn);

    print('Done!');

  }

?>

【讨论】:

  • 添加了一些额外的间距以使其更具可读性,并添加了额外的注释
  • 非常感谢,现在去测试一下
  • 请记住,它只是一个基本框架 - 例如,它不会考虑人们上传两个同名文件,它只是拒绝第二个请求。
  • 我明白,但这只是我需要的剂量。再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
  • 2018-05-30
  • 2012-04-29
  • 2015-10-28
  • 2019-07-02
  • 2013-09-20
相关资源
最近更新 更多