我有一个用 php 上传的文件,但这无关紧要,因为我真正的问题是我有这个代码:
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$real = $_FILES['Filedata']['name'];
$finfo = pathinfo($real);
$time = time();
$rand1 = substr(sha1($real.$time),0,4);
$rand2 = substr(sha1($real.$time),5,-22);
$fname = preg_replace("#[^A-Za-z1-9]#","_", $_FILES['Filedata']['name']);
mkdir(UPLOAD_TO."/".$rand2,0777);
$targetFile = UPLOAD_TO."/".$rand2."/".$real;
move_uploaded_file($tempFile,$targetFile);
$query = $db->prepare("INSERT INTO files (realname,filename,extension,filesize,rand1,rand2,uppedby,created) VALUES(:real,:filename,:extension,:filesize,:rand1,:rand2,:uppedby,:created)");
$query->execute(array(
":real" => $real,
":filename" => $fname,
":extension" => $finfo['extension'],
":filesize" => filesize($tempFile),
":rand1" => $rand1,
":rand2" => $rand2,
":uppedby" => $_SERVER['REMOTE_ADDR'],
":created" => time()
));
}
文件已上传,我正在从实际时间戳和真实文件名创建一个 sha1 哈希。在我切割它们之后,substr()
但有一个棘手的部分。mkdir 不是用剪切的字符串而是用另一种奇怪的组合创建目录。我不知道我做错了什么。你能看一下吗?