php - 将多个文件上传到新创建的目录

标签 php mysql upload

我正在尝试使用 username_entry 作为新创建目录的名称创建一个新目录,并将最多 5 张图像添加到该文件夹​​中,我对 php 还很陌生,看不出为什么这不起作用。

这是基本的 HTML

<form action="upload_images.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">

            <input type="hidden" name="username_entry" value="<?php echo $_POST['username_entry']; ?>">

            <input type="file" name="upload[0]" id="1"> Upload image 1 </input><p />

            <input type="file" name="upload[1]" id="2"> Upload image 2 </input><p />

            <input type="file" name="upload[2]" id="3"> Upload image 3 </input><p />

            <input type="file" name="upload[3]" id="4"> Upload image 4 </input><p />

            <input type="file" name="upload[4]" id="5"> Upload image 5 </input><p />


            <input name="upload" type="image" value="Submit" src="images/reg_next_button.png" alt="Submit" align="right"></div>

</form>

这里是 PHP 用来尝试将图像存储在新的目录中,并将文件名存储在 MySQL 数据库中。

<?php

// DataBase credentials

$user = 'root';
$password = 'root';
$db = 'welcometowarwick';
$host = 'localhost';
$port = 8889;

//! DataBase credentials

// Connect to DataBase

$link = mysqli_init();
$success = mysqli_real_connect(
   $link, 
   $host, 
   $user, 
   $password, 
   $db,
   $port
);

//! Connect to DataBase



$directoryPath = "user_images/".$username_entry;
mkdir($directoryPath, 0644);



if (isset($_FILES['upload']) === true) {
    $files = $_FILES['upload'];

    for($x = 0; $x < count($files['name']); $x++) {
        $name = $files['name'][$x];
        $tmp_name = $files['tmp_name'][$x];

        move_uploaded_file($tmp_name, "user_images/{$username_entry}/{$name}");

    //echo '<pre>', print_r($files['name'][0], true), '</pre>';

        $image_upload1 = ($files['name'][0]);
        $image_upload2 = ($files['name'][1]);
        $image_upload3 = ($files['name'][2]);
        $image_upload4 = ($files['name'][3]);
        $image_upload5 = ($files['name'][4]);

    }


    $insert_upload1 = "user_images/$username_entry/$image_upload1";
    $insert_upload2 = "user_images/$username_entry/$image_upload2";
    $insert_upload3 = "user_images/$username_entry/$image_upload3";
    $insert_upload4 = "user_images/$username_entry/$image_upload4";
    $insert_upload5 = "user_images/$username_entry/$image_upload5";

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$sql = "INSERT INTO images
(

image_1,
image_2,
image_3,
image_4,
image_5


) 
VALUES 
(

'$insert_upload1',
'$insert_upload2',
'$insert_upload3',
'$insert_upload4',
'$insert_upload5'

)";

if (mysqli_query($link, $sql)) {
    echo "New record created successfully $username_entry";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($link);
}

mysqli_close($link);


?>

我做错了什么?我不明白为什么这不起作用。提前感谢您的帮助。 :)

最佳答案

而不是,

mkdir($directoryPath, 0644);

尝试,

mkdir($directoryPath, 0777, true);

关于php - 将多个文件上传到新创建的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29208765/

相关文章:

php - 如果不存在具有相同值的行,则插入该行

python - Library not loaded : @rpath/libmysqlclient. 21.dylib Reason: image not found Django migrate error using mysqlclient DB 驱动程序和 MySQL 8 with macOS

php - 多个下拉列表发布到 foreach 循环;变量重复

php - 调用非对象上的成员函数 getClientOriginalExtension()

ruby-on-rails - 有没有一个rails方法可以循环遍历上传文件的每一行? "each_line"是一个IO方法,但它不起作用

php - 访问由 php 中的 explode 函数创建的数组

php - 交响乐2 : How to reuse variable(username) inside template?

php - 时间相加减去时间

javascript - 如何让评论显示在所有帖子下

iphone - 使用 iOS 异步上传音频文件