我在here中找到了和我一样的案例。我尝试在PHP上创建带有mkdir
的文件夹并且它可以工作,同样在MySQL中它给我链接到文件。但是,为什么我的文件不会进入新文件夹?如果我从php中删除md5
,它就像正常一样工作。
我使用XAMPP 5.6.30,而我的XAMPP文件夹不在C:system上。我弄错了吗?
在这里我的代码:
<?php
include('conn.php');
foreach ($_FILES['upload']['name'] as $key => $name){
$newFilename = time() . "_" . $name;
move_uploaded_file($_FILES['upload']['tmp_name'][$key], 'upload/'.md5(time()).'/' . $newFilename);
$location = 'upload/'.md5(time()).'/' . $newFilename;
mkdir('upload/'.md5(time()).'/');
mysqli_query($conn,"insert into photo (location) values ('$location')");
}
header('location:index.php');
?>
答案 0 :(得分:0)
在这两个步骤之间:
$location = 'upload/'.md5(time()).'/' . $newFilename;
mkdir('upload/'.md5(time()).'/');
time()值不相同,您将其用作常量 你需要先将它存储在var中 另外,我认为为您上传的每个文件创建新文件夹都很有用
答案 1 :(得分:0)
首先,我会为上传创建一个名为uploads的目录
mkdir上传
然后确保它具有写权限
然后我会使用以下
include('conn.php');
foreach ($_FILES['upload']['name'] as $key => $name){
$newfilename = "";
$ts = time();
$newfilename = $ts."_".$name;
move_uploaded_file($_FILES['upload']['tmp_name'][$key], 'uploads/'.$newfilename);
mysqli_query($conn,"insert into photo (location) values ('$newfilename');')");
}
header('location:index.php');
除非你有理由让所有上传文件夹都是独一无二的,否则我认为随着时间的推移会变得混乱而难以管理。
答案 2 :(得分:0)
问题可能是md5(time())每次都会生成一个新结果。 我会把它变成一个变量并使用变量而不是每次重新计算它
答案 3 :(得分:0)
您尝试在创建文件夹
之前移动文件// Here you try to move the file but the directory is not created yet
move_uploaded_file($_FILES['upload']['tmp_name'][$key], 'upload/'.md5(time()).'/' . $newFilename);
$location = 'upload/'.md5(time()).'/' . $newFilename;
// after trying to move the file you create the directory, but the directory should be created first
mkdir('upload/'.md5(time()).'/');
这是一个有效的代码(参见顺序:首先是mkdir然后是move_uploaded_file:
include('conn.php');
foreach ($_FILES['upload']['name'] as $key => $name){
$ts = time();
$crptd = md5($ts);
$newFilename = $ts . "_" . $name;
$location = 'upload/'.$crptd.'/' . $newFilename;
mkdir('upload/'.$crptd.'/');
move_uploaded_file($_FILES['upload']['tmp_name'][$key], 'upload/'.$crptd.'/' . $newFilename);
mysqli_query($conn,"insert into photo (location) values ('$location')");
}
header('location:index.php');