文件上传的文件作者的php名称

问题描述 投票:0回答:2

HTML:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

PHP:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// 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 "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

这个文件上传对我很有帮助。但我想要更多:如果有人上传文件,我只会看到他上传的文件。我没有看到该文件作者的姓名。

有可能做出类似的东西吗?

php file upload
2个回答
0
投票

我有文件上传代码,你可以将它与你的代码进行比较,看看问题出在哪里。

关于扩展,您需要确保上传到数据库并上传文件夹正确的扩展名。

注意:在您使用代码之前,请确保您有文件夹调用(上传),并在此文件夹中使另一个文件夹调用它(头像),这样您可以在使用我发布的代码时将其上传到服务器的文件夹中。

      <form action="" method="POST" enctype="multipart/form-data">
      <input type="file" name="avatar">
      </form>
if(isset($_FILES['avatar'])){
       //If you want to see the array for file do this
       //print_r($_FILES['avatar']);

        // avatar array
        $avatarName = $_FILES['avatar']['name'];
        $avatarSize = $_FILES['avatar']['size'];
        $avatarTmp = $_FILES['avatar']['tmp_name'];
        $avatarType = $_FILES['avatar']['type'];

        // list of allowed file typed to upload
        $avatarAllowedExtension = array('jpeg', 'jpg', 'png', 'gif');

        // get avatar extension
        $tmp = explode('.', $avatarName);
        $avatarExtension = strtolower(end($tmp));

        if (!empty($avatarName) && !in_array($avatarExtension, $avatarAllowedExtension)) {
            echo 'This extension is not allowed';
        }

        if (empty($avatarName)) {
            echo 'Avatar is required';
        }

        if ($avatarSize > 4194304) {
            echo 'Avatar cant be larger than 4mb';
        }
       //using rand(min,max) to insert in database different name
       $avatar = rand(0, 100000000000) . '_' . $avatarName;
//to keep file insert folder if you want to show it or if you want to download it in your website
       move_uploaded_file($avatarTmp, 'uploads\avatar\\' . $avatar);
//If you want to upload file use $avatar because you need different name in your database but when you show it to user you can show the original name for user file
}

-1
投票

如果您的系统未保存名称,则无法显示该名称。那里的图形文件不包括作者的名字。

© www.soinside.com 2019 - 2024. All rights reserved.