php-如何将文件移动到使用userid创建的文件夹(上传文件)?

时间:2016-05-17 10:06:32

标签: php

需要将上传的文件插入到受尊重的文件夹

我在这里创建基于唯一ID的文件夹。

我无法将文件插入文件夹。

上传文件时,文件和文件夹都是单独存储的。

using (var sqlConnection1 = new SqlConnection("ConnectionString"))
{
    using (var sqlCommand1 = new SqlCommand("INSERT INTO dbo.Orders(Title,Seats,Payment,DateNTime)" +
    "VALUES (@movieName, @numTickets, @creditCardType, @DateTime.Now)", sqlConnection1))
    {
        sqlCommand1.Parameters.Add("@movieName", SqlDbType.VarChar).Value = movieName;
        sqlCommand1.Parameters.Add("@numTickets", SqlDbType.VarChar).Value = numTickets;
        sqlCommand1.Parameters.Add("@creditCardType", SqlDbType.Int).Value = creditCardType;
        sqlCommand1.Parameters.Add("@movieName", SqlDbType.DateTime).Value = DateTime.Now;

        sqlCommand1.Connection.Open();
        sqlCommand1.ExecuteNonQuery();
    }
}

$ send id是唯一ID。

请告诉我哪里出错了?

1 个答案:

答案 0 :(得分:1)

你搞乱了你的逻辑。首先移动上传的文件

if (move_uploaded_file($file['tmp_name'], $upload_directory.$path)) {

并且只是尝试创建新目录

if (mkdir($path_user,0766,false )) {

并且仅当当前用户从未上传任何您重命名的文件时,将其移动到其他目录

rename($path,$path_move);

正确的逻辑:

  1. 格式化上传的文件路径$path = $upload_directory.DIRECTORY_SEPARATOR.$send_id
  2. 检查目录是否存在file_exists($path)
  3. 如果不存在,请创建mkdir($path, 0766, false)
  4. 上传文件move_uploaded_file($file['tmp_name'], $path)
  5. E.g:

    $path = $upload_directory.DIRECTORY_SEPARATOR.$send_id;
    
    if (!file_exists($path)) {
        mkdir($path, 0766, false);
    }
    
    move_uploaded_file($file['tmp_name'], $path);