2013-06-12 92 views
0

我正在使用文件模塊從本地表單獲取某些文件並上傳到另一臺服務器。當我嘗試上傳大文件時,它給了我超時錯誤(我曾嘗試更改php.ini,但那不是我希望它工作的方式)。這就是爲什麼我試圖通過ftp函數上傳文件的原因。但是,我無法獲得我剛剛選擇上傳的文件的源路徑(例如filepath,而不是uri)。我想將這個文件路徑作爲源傳遞給fopen()函數。但我不斷收到錯誤:* ftp_nb_fput()[function.ftp-NB-fput]:無法打開該文件:在assets_managed_file_form_upload_submit沒有這樣的文件或目錄()(FILE_DIRECTORY線303)*Drupal 7文件模塊+ php ftp函數

function assets_managed_file_form_upload_submit($form, &$form_state) { 
    for ($i = 0; $i < $form_state['num_files']; $i++) { 
    if ($form_state['values']['files_fieldset']['managed_field'][$i] != 0) { 

    // Make the file permanent. 
    $file = file_load($form_state['values']['files_fieldset']['managed_field'][$i]); 

    $local_path = file_create_url($file->uri); 
    //drupal_set_message(t("file->uri: " . $file->uri . " local path: " . $local_path)); 

    $file->status = FILE_STATUS_PERMANENT; 
    $directory = 'private://cubbyhouse/'. $form_state['values']['allowed_user']; 
    file_prepare_directory($directory, FILE_CREATE_DIRECTORY); 


    $source = fopen($local_path,"r"); 

    $conn = ftp_connect("FTP SERVER") or die("Could not connect"); 
    ftp_login($conn,"USERNAME", "PASS"); 
    $ftp_directory = TheDirectoryIwantToPutTheFile . $form_state['values']['allowed_user']; 

    $uri_parts = explode("/",$file->uri); 
    $filename = $uri_parts[sizeof($uri_parts)-1]; 
    $target = $ftp_directory . "/" . $filename; 
    //drupal_set_message(t($target . " " . $file->uri)); 
    $ret = ftp_nb_fput($conn,$target,$source,FTP_ASCII); 

    while ($ret == FTP_MOREDATA) 
    { 
     // Do whatever you want 
     //echo "."; 
     // Continue upload... 
     $ret = ftp_nb_continue($conn); 
    } 
    ftp_close($conn); 


    //$file->uri = file_unmanaged_copy($file->uri, $directory, FILE_EXISTS_REPLACE); 
    $file->uid = $form_state['values']['allowed_user']; 
    drupal_chmod($file->uri); 
    file_save($file); 

    // Need to add an entry in the file_usage table. 
    file_usage_add($file, 'assets', 'image', 1); 

    drupal_set_message(t("Your file has been uploaded!")); 
} 

} }

回答

0

我解決了這個問題。問題主要是因爲我把錯誤的道路作爲目標。它必須是/ public_html/......而不是/ home/our_name/public_html