【问题标题】:PHP fopen from windows to external unix serverPHP fopen 从 windows 到外部 unix 服务器
【发布时间】:2015-10-30 01:10:15
【问题描述】:

我在使用 fopen() 时遇到问题,当我尝试写入 unix 服务器上的文件时,该函数返回“无法打开流:没有这样的文件或目录”。 我的 PHP 应用程序在 Windows 上运行。

这是我使用 fopen 函数的代码:

$path = "/212.120.15.80/tmp/NMJ.pdf"; //Path to unix server
$url = $_FILES [$fileElementName] ['tmp_name'];


     $newfname = $path;

     $file = fopen ( $url, "rb" );
     if ($file) {
        $newf = fopen ( $newfname, "wb" );
        $fopen = $newf;

        if ($newf) {
            while ( ! feof ( $file ) ) {
                fwrite ( $newf, fread ( $file, 1024 * 8 ), 1024 * 8 );
            }
        }
     }
     if ($fopen === false) {
        $return_fopen = 'Fichier non téléchargé';
     } else {
        $return_fopen = 'Le fichier a été déposé sur le serveur';
     }

     if ($file) {
        fclose ( $file );
     }
     if ($newf) {
        fclose ( $newf );
     }

我对 tmp 目录具有写/读权限。

当我使用以下路径在 Windows 服务器上编写文件时,此代码有效:“\\Example/share”

【问题讨论】:

    标签: php windows unix fopen


    【解决方案1】:

    首先,在尝试加载其内容之前检查文件是否存在是值得的。在这种情况下使用file_exists()。 然后,您的路径设置为本地文件。使用类似的东西

    $path = "http://212.120.15.80/tmp/NMJ.pdf"
    

    【讨论】:

    • 感谢您的建议。我不能使用 http 协议,因为它不允许在写入模式下使用 fopen
    • 最后,我更改了我的代码,以便能够使用“http”协议 感谢您的帮助。
    【解决方案2】:

    我将我的代码发布给需要它的人

    $docup = 'http://212.120.15.80'
    $filename = $_FILES [$fileElementName] ['name'];
    
    $ch = curl_init ( $docup );
    curl_setopt ( $ch, CURLOPT_URL, $docup . "/uploadfilescript.php" );
    curl_setopt ( $ch, CURLOPT_HEADER, false );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_POST, true );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, array (
            'file' => '@' . $url,
            'filename' => $filename 
    ) );
    
    if (curl_exec ( $ch ) === FALSE) {
            $return = 'Le fichier n\' a pas pu être téléchargé';
    } else {
        curl_exec ( $ch );
        $return = 'Le fichier a été déposé sur le serveur';
    }
    
    curl_close ( $ch );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 2020-09-15
      • 2013-03-24
      • 1970-01-01
      • 2013-06-07
      相关资源
      最近更新 更多