當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP ftp_alloc()用法及代碼示例


ftp_alloc()函數是PHP中的內置函數,用於為FTP服務器上載的文件分配空間。

用法:

ftp_alloc( $ftp_connection, $filesize, $result );

參數:此函數接受上述和以下所述的三個參數:


  • $ftp_connection:它是必填參數。它指定用於分配空間和上載文件的現有FTP連接。
  • $filesize:它是必填參數。它以字節為單位指定文件的大小,即要分配的字節數。
  • $result:此參數是可選的。它指定一個變量來存儲響應。

返回值:成功返回True,失敗返回False。

注意:

  • 此函數可用於PHP 5.0.0和更高版本。
  • 以下示例無法在在線IDE上運行。因此,請嘗試使用適當的ftp服務器名稱在某些PHP托管服務器或localhost中運行。

以下示例說明了PHP中的ftp_alloc()函數:

範例1:

<?php 
  
// Connect to FTP server 
  
// Use a correct ftp server  
$ftp_server = "localhost"; 
  
// Use correct ftp username 
$ftp_username = "user"; 
  
// Use correct ftp password corresponding 
// to the ftp username 
$ftp_userpass = "user"; 
  
// File name or path to upload to ftp server 
$file = "filetoupload.txt"; 
   
// Establishing ftp connection  
$ftp_connection = ftp_connect($ftp_server)  
    or die("Could not connect to $ftp_server"); 
  
if($ftp_connection) { 
    echo "successfully connected to the ftp server!"; 
      
    // Logging in to established connection 
    // with ftp username password 
    $login = ftp_login($ftp_connection, 
            $ftp_username, $ftp_userpass); 
      
    if($login) { 
          
        // Checking whether logged in successfully or not 
        echo "<br>logged in successfully!"; 
          
        // Allocating space for the file as specified 
        // i.e. in $file 
        if(ftp_alloc($ftp_connection, filesize($file), $result)) { 
            echo "<br>space successfully allocated on the FTP server"; 
              
            if(ftp_put($ftp_connection,  
                "uploadedfile_name_in_server.txt", $file, FTP_ASCII)) { 
                echo "<br>Uploaded successful $file."; 
            } 
            else { 
                 echo "<br>Error while uploading $file."; 
            } 
        } 
        else { 
            echo "<br>space allocation failed!";    
        } 
    } 
    else { 
        echo "<br>login failed!"; 
    } 
  
    // Closeing  connection 
    if(ftp_close($ftp_connection)) { 
        echo "<br>Connection closed Successfully!"; 
    }  
} 
  
?>

輸出:

successfully connected to the ftp server!
logged in successfully!
space successfully allocated on the FTP server
Uploaded successful filetoupload.txt.
Connection closed Successfully!

範例2:使用端口號21連接到ftp服務器,然後分配和上傳文件。

<?php 
  
// Connect to FTP server 
  
// Use a correct ftp server  
$ftp_server = "localhost"; 
  
// Use correct ftp username 
$ftp_username = "user"; 
  
// Use correct ftp password corresponding 
// to the ftp username 
$ftp_userpass = "user"; 
  
// File name or path to upload to ftp server 
$file = "filetoupload.txt"; 
   
// Establishing ftp connection  
$ftp_connection = ftp_connect($ftp_server, 21)  
    or die("Could not connect to $ftp_server"); 
  
if($ftp_connection) { 
    echo "successfully connected to the ftp server!"; 
      
    // Logging in to established connection 
    // with ftp username password 
    $login = ftp_login($ftp_connection, 
            $ftp_username, $ftp_userpass); 
      
    if($login) { 
          
        // Checking whether logged in successfully or not 
        echo "<br>logged in successfully!"; 
          
        // Allocating space for the file as specified 
        // i.e. in $file 
        if(ftp_alloc($ftp_connection, filesize($file), $result)) { 
            echo "<br>space successfully allocated on the FTP server"; 
              
            if(ftp_put($ftp_connection,  
                "uploadedfile_name_in_server.txt", $file, FTP_ASCII)) { 
                echo "<br>Uploaded successful $file."; 
            } 
            else { 
                 echo "<br>Error while uploading $file."; 
            } 
        } 
        else { 
            echo "<br>space allocation failed!";    
        } 
    } 
    else { 
        echo "<br>login failed!"; 
    } 
  
    // Closeing  connection 
    if(ftp_close($ftp_connection)) { 
        echo "<br>Connection closed Successfully!"; 
    }  
} 
  
?>

輸出:

successfully connected to the ftp server!
logged in successfully!
space successfully allocated on the FTP server
Uploaded successful filetoupload.txt.
Connection closed Successfully!

參考: https://www.php.net/manual/en/function.ftp-alloc.php



相關用法


注:本文由純淨天空篩選整理自gekcho大神的英文原創作品 PHP | ftp_alloc() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。