【问题标题】:file upload in php name of the author of the file用php文件上传文件作者的名字
【发布时间】:2018-09-08 14:11:56
【问题描述】:

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.";
    }
}
?>

这个文件上传非常适合我。但我想要更多的东西: 如果有人上传文件,我只会看到他上传的文件。 我没有看到该文档的作者姓名。

有可能做这样的事情吗?

【问题讨论】:

  • name of the author of that document 是什么意思?
  • 查看文件上传者的姓名。实际上它只显示文件本身

标签: php file upload


【解决方案1】:

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

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

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

      <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
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      相关资源
      最近更新 更多