重命名已存在的上传文件并重新上传

时间:2018-11-28 13:09:31

标签: php mysql upload rename

我正在尝试重命名要重命名和上传的上传文件,重命名的文本在我的数据库及其文件路径中被更改

请帮助我吗?

这是我的代码,我是php mysql的初学者。.我需要解决方案吗?

如果有人知道尽可能多地编辑我的代码,请帮助我兄弟吗?

    if(isset($_FILES["file"]))
        {
            $path = "../../assets/uploads/company/1_SourceSol/"; 

            if(!file_exists($path.$dir))
            {
                mkdir($path.$dir, 0777, true);
            }
            $target_dir = $path.$dir."/";
            $target_file = $target_dir . basename($_FILES["file"]["name"]);
            $uploadOk = 1;
            $fileType = pathinfo($target_file,PATHINFO_EXTENSION);

            if(trim($_REQUEST['file_rename'])!='')
            {
                $target_file=$target_dir . basename(trim($_REQUEST['file_rename'])).'.'.$fileType;
                $file_rename=basename(trim($_REQUEST['file_rename']));
            }

            // Check if file already exists
            if (file_exists($target_file)) {
                $error="Sorry, file already exists.";
                $uploadOk = 1;
            }
            // Check file size
            if ($_FILES["file"]["size"] > 500000) {
                $error="Sorry, your file is too large.";
                $uploadOk = 1;
            }

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                $error="Sorry, file was not uploaded.";
            } 


            else
            {
                if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file))
                {
                    sleep(1);
                    $message="The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";

                    $query="UPDATE `topview_logo`
                            SET 
                            `topview_id`        = '$topview_id',
                            `filename`      = '$target_file',
                            `file_rename`       = '$file_rename'    
                            WHERE 
                            `company_logo_id`   = '$company_logo_id'";
                    mysqli_query($link,$query) or die(mysqli_error($link));
                } 

            } 
        }

            else
            {
                $query="UPDATE `topview_logo`
                        SET 
                        `topview_id`        = '$topview_id',
                        `file_rename`       = '$file_rename'
                        WHERE 
                        `company_logo_id`   = '$company_logo_id'";
                mysqli_query($link,$query) or die(mysqli_error($link));
            }
        }           
    }

0 个答案:

没有答案