代码之家  ›  专栏  ›  技术社区  ›  user83632

使用PHP将文件上载到FTP服务器,而不将其存储在文件系统中

  •  0
  • user83632  · 技术社区  · 16 年前

    我一直在尝试使用 proc_open 然后 ftp_fput 但是没有用,我猜是因为流在 不是吗 fstat 能够的

    <?php
    $ciphertext = 'sadfasfasdf90809sf890as8fjwkjlf';
    
    //The Descriptors
    $descriptorspec = array( 
      0 => array("pipe", "r"),  // stdin 
      1 => array("pipe", "w"),  // stdout 
      2 => array("pipe", "w")   // error
    );
    
    $process = proc_open('cat', $descriptorspec, $pipes);
    if (is_resource($process)) {    
    fwrite($pipes[0], $ciphertext); 
    fclose($pipes[0]);
    
    //Debug test to proce that $pipes[1] is a valid stream     
    //while(!feof($pipes[1])) {
    //  $content .=  fgets($pipes[1],1024);
    //}
    
    //FTP connection etc etc OMMITTED to save space.
    
    $upload = @ftp_fput($conn_id,$dir."/".$ftp_file.$extenstion,$pipes[1],FTP_BINARY);
    fclose($pipes[1]);
    // Check upload status
    echo ('upload '. ($upload ? 'true':' false')); 
    }
    ?> 
    

    我希望有人能提供帮助或建议任何改进或替代方法。

    谢谢

    菲尔

    2 回复  |  直到 16 年前
        1
  •  1
  •   user83632 user83632    16 年前

    永远不要相信服务器管理员!

    上面的代码可以工作,不需要使用tmpfile。

    谢谢

    菲尔

        2
  •  0
  •   Seb    16 年前

    为什么您不想创建一个文件?这仅仅是因为您没有其他选择、安全原因或其他原因吗?

    我通常将文件系统中没有的文件上传到FTP的方式是使用 tmpfile :

    <?php
    
      $fh = tmpfile();
      fwrite($fh, data);
    
      // do upload
    
      fclose($fh);
    
    ?>
    

    使用一个临时文件会使您的操作变得非常干净;你需要写什么就写什么,上传,然后关闭文件,然后它神奇地消失了。

    现在,我不知道你是否可以上传到FTP服务器上除了文件以外的其他东西(即来自内存的数据)。。。至少我试着想办法,但没能找到。