将多个文件上载到新创建的目录

时间:2015-03-23 11:10:08

标签: 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用于尝试将图像存储在新DIR中,并将文件存储在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);


?>

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

1 个答案:

答案 0 :(得分:0)

代替,

mkdir($directoryPath, 0644);

试,

mkdir($directoryPath, 0777, true);