簡體   English   中英

PHP上傳具有給定名稱的圖像

[英]PHP Upload image with given name

我正在使用此功能上傳圖像:

<?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文件,但帶有實際圖像的名稱,而不是profilepicture。

您可以更改以下行:

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

$ _FILES [“ fileToUpload”] [“ name”]是原始文件名,因此請將其更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM