创建目录并将多个文件上传到创建的目录PHP
Posted
技术标签:
【中文标题】创建目录并将多个文件上传到创建的目录PHP【英文标题】:Make directory and upload multiple file into created directory PHP 【发布时间】:2021-05-25 23:30:03 【问题描述】:我一直在寻找答案很多天。如何将多个文件上传到新创建的目录。如果您查看 file_upload.php,您会发现两个 $upload_dir
变量。所以,让我们首先调用$upload_dir
作为直接文件夹,然后调用$upload_dir
作为make dir。简单
所以,当我选择第一个 $upload_dir
时,它会直接将所有文件上传到文件夹中,而当我选择第二个 $upload_dir
时,它所做的是创建一个随机文件夹,但无法上传任何文件。
我想将多个文件上传到新创建的文件夹中
我确实提到了这个 php - Upload multiple photos into newly created Directory 和 Multiple file upload and store them in a directory 但对我不起作用
index.php
<form action="file_upload.php" method="POST"
enctype="multipart/form-data">
<h2>Upload Files</h2>
<p>
Select files to upload:
<!-- name of the input fields are going to
be used in our php script-->
<input type="file" name="files[]" multiple>
<br><br>
<input type="submit" name="submit" value="Upload" >
</p>
</form>
file_upload.php
<?php
// session_start();
// Check if form was submited
if(isset($_POST['submit']))
// Configure upload directory and allowed file types
$rand_name = rand(1, 10000);
// $upload_dir = 'C:/fileUpload/'.DIRECTORY_SEPARATOR;
// $permit = 0777;
$allowed_types = array('jpg', 'png', 'jpeg', 'gif');
$upload_dir = mkdir('C:/fileUpload/'. $rand_name .'/'.DIRECTORY_SEPARATOR, 0777);
// Define maxsize for files i.e 10MB
$maxsize = 10 * 1024 * 1024;
// Checks if user sent an empty form
if(!empty(array_filter($_FILES['files']['name'])))
// Loop through each file in files[] array
foreach ($_FILES['files']['tmp_name'] as $key => $value)
$file_tmpname = $_FILES['files']['tmp_name'][$key];
$file_name = $_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
// Set upload file path
$filepath = $upload_dir.$file_name;
// Check file type is allowed or not
if(in_array(strtolower($file_ext), $allowed_types))
// Verify file size - 10MB max
if ($file_size > $maxsize)
echo "Error: File size is larger than the allowed limit.";
// If file with name already exist then append time in
// front of name of the file to avoid overwriting of file
if(file_exists($filepath))
$filepath = $upload_dir.time().$file_name;
if( move_uploaded_file($file_tmpname, $filepath))
echo "$file_name successfully uploaded <br />";
else
echo "Error uploading $file_name <br />";
else
if( move_uploaded_file($file_tmpname, $filepath))
echo "$file_name successfully uploaded <br />";
else
echo "Error uploading $file_name <br />";
else
// If file extention not valid
echo "Error uploading $file_name ";
echo "($file_ext file type is not allowed)<br / >";
else
// If no files selected
echo "No files selected.";
?>
请帮忙!
提前谢谢你
【问题讨论】:
mkdir
返回一个 boolean
所以分配 $upload_dir=mkdir(.....)
可能不是你想要的
转储 $_FILES 数组以查看它的结构...然后根据真实结构修复您的代码...老实说,我想知道如果人们不知道他们是如何编程的数据看起来像......
仅供参考 - mkdir
的第三个参数值得使用,因为它将确保生成路径中有用的所有文件夹
@LarsStegelitz 我已经相应地构建了 $_FILES 然后也无法解决我的问题
@ProfessorAbronsius 对不起,先生。我无法与您的回答联系起来。如果可能的话,您能否提供有关该问题的更多详细信息。
【参考方案1】:
mkdir
返回一个布尔值(true 或 false),而不是创建的目录路径。
您可能希望在$upload_dir
中定义路径,但不将 mkdir 的结果分配给它:
$upload_dir = 'C:/fileUpload/'. $rand_name .'/';
if (mkdir($upload_dir, 0777))
//process images
【讨论】:
以上是关于创建目录并将多个文件上传到创建的目录PHP的主要内容,如果未能解决你的问题,请参考以下文章