简体   繁体   English

图像上传PHP的问题

[英]Issue with image uploading PHP

I'm trying to create a form where users can select an image and set their profile picture. 我正在尝试创建一个表单,用户可以在其中选择图像并设置其个人资料图片。 After this I want to get this specific information and display it within HTML. 在此之后,我想获取此特定信息并在HTML中显示。

I have the following code inside profile.php; 我在profile.php中有以下代码;

if(isset($_POST['submit']) ){
    $fileName = $_FILES["avatar"]["name"];
    $fileTmpLoc = $_FILES["avatar"]["tmp_name"];
    $fileType = $_FILES["avatar"]["type"];
    $fileSize = $_FILES["avatar"]["size"];
    $fileErrorMsg = $_FILES["avatar"]["error"];
    $mysql->setUserAvatar($fileName, $fileTmpLoc, $fileType, $fileSize, $fileErrorMsg, $s_email);
}


I have the following code inside mysql.php (this code is inside a class name mysql): 我在mysql.php里面有以下代码(这段代码在类名mysql里面):

function setUserAvatar($fileName, $fileTmpLoc, $fileType, $fileSize, $fileErrorMsg, $s_email){

    $kaboom = explode(".", $fileName);
    $fileExt = end($kaboom);

    list($width, $height) = getimagesize($fileTmpLoc);

    if($width < 10 || $height < 10){
        echo "Image is too small";
        exit(); 
    }

    $db_file_name = rand(100000000000,999999999999) . "." . $fileExt;
    echo $db_file_name;

    if($fileSize > 1048576) {
        echo "Image can't be larger than 1MB";
        exit(); 
    } else if (!preg_match("/\.(gif|jpg|png)$/i", $fileName) ) {
        echo "The file extension should be .gif, .jpg or .png";
        exit();
    } else if ($fileErrorMsg == 1) {
        echo "An unknown error occurred";
        exit();
    }

    $sql = "SELECT avatar FROM users WHERE email='$s_email' LIMIT 1";

    $query = mysqli_query($this->db, $sql);
    $row = mysqli_fetch_row($query);

    $avatar = $row[0];

    if($avatar != ""){
        $picurl = "../user/$s_email/$avatar"; 
        if (file_exists($picurl)) { unlink($picurl); }
    } 

    $moveResult = move_uploaded_file($fileTmpLoc, SITE_ROOT . "/../user/$s_email/". $db_file_name);

    if ($moveResult != true) {
        echo "File upload failed";
        exit();
    }

    $sql = "UPDATE users SET avatar='$db_file_name' WHERE email='$s_email' LIMIT 1";
    $query = mysqli_query($this->db, $sql);

}

After this is done, I want to do something like: 完成后,我想做一些事情:

<img src="user/" . $s_email . "/" . $data['avatar'] . " />

How ever, when I try to reach the avatar element from the MySQL database, I always get the same number, which is: 2147483647 (but in the user folder everything went right). 然而,当我尝试从MySQL数据库中获取头像元素时,我总是得到相同的数字,即:2147483647(但在用户文件夹中,一切都正常)。 So there is a problem with the value that is getting inserted into the database. 因此,插入数据库的值存在问题。 Any suggestions what this problem might be? 有什么建议可能是这个问题吗?

EDIT: I've fixed the issue by decreasing the length of the random number. 编辑:我已经通过减少随机数的长度来解决问题。 However, the problem is still that the value in the database hasn't receive the extension? 但是,问题仍然是数据库中的值没有收到扩展名? The column datatype of avatar is VARCHAR. avatar的列数据类型是VARCHAR。

It's this line: 就是这条线:

$db_file_name = rand(100000,999999) . "." . $fileExt;

Yes, a nice feature of most random number generators is that they produce repeatable results unless you tell them not to. 是的,大多数随机数生成器的一个很好的特性是它们会产生可重复的结果,除非你不告诉它们。

In the case of PHP you tell it not to start at the same place using srand() 在PHP的情况下,你告诉它不要使用srand()在同一个地方开始

Your number is to big. 你的号码很大。 and not very random. 并不是很随机。

$db_file_name = rand(100000000000,999999999999) 

Your min and max are very close to each other/in wrong order, and to large because the number your getting 2147483647 = 2^30 -1 is most likely the size limit for the type of column input you decided to use for the name. 您的最小值和最大值彼此非常接近/排序错误,并且因为您获得的数字2147483647 = 2^30 -1很可能是您决定用于名称的列输入类型的大小限制。

rand documentation http://php.net/manual/en/function.rand.php 兰德文档http://php.net/manual/en/function.rand.php

Change your column type to something like varchar(40) 将列类型更改为varchar(40)

I'd also recommend not using random numbers greater than php's signed 32 bit integer rand(0,2147483647); 我还建议不要使用大于php的带符号32位整数rand(0,2147483647);随机数rand(0,2147483647);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM