2017-08-12 121 views
0

我有2子域:如何將一個子域的move_uploaded_file移動到另一個子域?

  1. admin.example.com - 管理面板
  2. static.example.com - 上傳的文件

我怎麼能使用move_uploaded_file上傳文件從第一個子域根到第二個子域根?
上傳功能:

define('UPLOAD_DIR', '/home/.../static.example.com/htdocs/uploads/'); 
define('UPLOAD_ADDR', 'http://static.example.com/uploads/'); 

define('TEMP_DIR', '/home/.../admin.example.com/htdocs/temp/'); 

function upload_file($input, $upload_subdir) { 

    if (!file_exists($_FILES[$input]['tmp_name'])) 
     return false; 

    $sha1file = sha1_file($_FILES[$input]['tmp_name']); 
    $tmp_name = TEMP_DIR . $sha1file; 
    $new_name = $upload_subdir . '/' . substr($sha1file, 0, 5) . '_' . $_FILES[$input]['name']; 

    move_uploaded_file($_FILES[$input]['tmp_name'], $tmp_name); 
    rename($tmp_name, UPLOAD_DIR . $new_name); 

    if (file_exists(UPLOAD_DIR . $new_name)) 
     return $new_name; 
    else 
     return false; 
} 


測試:

upload_file('image' /* Input name */, 'images' /* subdir in UPLOAD_DIR (this is existing dir) */); 

注:

  1. 服務器的操作系統是CentOS的
  2. UPLOAD_DIRTEMP_DIR是正確的絕對目錄。另外我測試了相對目錄不工作。
  3. 我直接測試move_uploaded_file沒有複製文件到當前子域的臨時目錄)不起作用。
  4. 無警告顯示
+1

'move_uploaded_file()'不關心主機名/域。只需告訴它你想在哪裏存儲文件。如果要從腳本位置使用相對URL,則應該使用'__DIR__'來獲取腳本的絕對路徑:'upload_file(__ DIR__。'/ image')''。 –

+1

如果出現問題,它幾乎總是文件訪問權限之一。儘管在子域中不應該這樣。 – JBH

+0

Magnus Eriksson。 '__DIR __。'image /''上傳文件到當前的子域名 –

回答

0

問題解決了。
這不起作用,因爲php不能訪問DOCUMENT_ROOT以外的其他文件夾。

相關問題