0
我有2子域:如何將一個子域的move_uploaded_file移動到另一個子域?
- admin.example.com - 管理面板
- 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) */);
注:
- 服務器的操作系統是CentOS的
UPLOAD_DIR
和TEMP_DIR
是正確的絕對目錄。另外我測試了相對目錄不工作。- 我直接測試
move_uploaded_file
(沒有複製文件到當前子域的臨時目錄)不起作用。 - 無警告顯示
'move_uploaded_file()'不關心主機名/域。只需告訴它你想在哪裏存儲文件。如果要從腳本位置使用相對URL,則應該使用'__DIR__'來獲取腳本的絕對路徑:'upload_file(__ DIR__。'/ image')''。 –
如果出現問題,它幾乎總是文件訪問權限之一。儘管在子域中不應該這樣。 – JBH
Magnus Eriksson。 '__DIR __。'image /''上傳文件到當前的子域名 –