在Windows上创建文件夹后,文件上传不会移动

问题描述 投票:0回答:4

我在here找到了和我一样的案例。我尝试使用mkdiron PHP制作文件夹,它也可以工作,在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');
?>
php windows file-upload xampp
4个回答
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');

0
投票

在这两个步骤之间:

$location = 'upload/'.md5(time()).'/' . $newFilename;
mkdir('upload/'.md5(time()).'/');

time()值不一样,你将它用作常量,你需要先将它存储在var中另外,我不认为为你上传的每个文件创建一个新文件夹是有用的


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');

除非你有理由让所有的上传文件夹都是独一无二的,否则我认为随着时间的推移会变得混乱和难以管理。


0
投票

问题可能是md5(time())每次都会生成一个新结果。我会把它变成一个变量并使用变量而不是每次重新计算它

© www.soinside.com 2019 - 2024. All rights reserved.