gpt4 book ai didi

PHP 上传给定名称的图像

转载 作者:行者123 更新时间:2023-11-29 12:26:07 26 4
gpt4 key购买 nike

我正在使用此功能上传图像:

<?php

session_start();

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



$target_dir = ('users/'.$_SESSION["user"]["id"].'/');
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$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');
}
}
?>

如何将文件名更改为:profilepicture例如。任何上传图像的用户都会将其上传到用户 ID 文件,但使用实际图像的名称,而不是个人资料图片。

最佳答案

您可以更改以下行:

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$_FILES["fileToUpload"]["name"] 是原始文件名,因此将其更改为如下内容:

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

关于PHP 上传给定名称的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28288233/

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