如何更改上传到表单帖子的文件名

时间:2015-04-23 20:20:27

标签: php html

我有一个上一个表单,该表单会发布此上传代码以及该文件的生成密钥。如何将文件名更改为$ post_id,其中文件名当前设置为profilepicture

<?php

    session_start();

if(!isset($_SESSION["user"]) or !is_array($_SESSION["user"]) or empty($_SESSION["user"])) {
      // redirect to login page
}


$post_id = $_POST['post_id'];

$target_dir = ('users/'.$_SESSION["user"]["id"].'/feed/');
$target_file = $target_dir . 'profilepicture.'.pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image


// Check if file already exists

// Check file size

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo header('Location: main.php');
    }
}
?>

1 个答案:

答案 0 :(得分:0)

$target_file = $target_dir . $post_id .'.'.pathinfo($_FILES["fileToUpload"]["name"], PATHINFO_EXTENSION); `