gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:47:25 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com