简体   繁体   English

在php中上传用户个人资料图片

[英]upload user profile picture in php

I created a simple upload form, all the code is working perfectly but when I upload 2kb,20kb,26kb, then the pic will uploaded but when I try to upload 60kb,66kb then the page will stuck on loading, I don't know why this happen and I also changed the max-upload-size in php.ini.我创建了一个简单的上传表单,所有代码都运行良好,但是当我上传 2kb、20kb、26kb 时,图片会上传但是当我尝试上传 60kb、66kb 时页面会卡在加载时,我不知道为什么会发生这种情况,我还更改了 php.ini 中的 max-upload-size。 can anybody tell me why this is happening?谁能告诉我为什么会这样?

Signup.php注册.php

<!DOCTYPE html>
<head>
    <title>Index</title>
</head>
<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>

register.php注册.php

<?php
$target_dir = "uploads/";
$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
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.";
    }
}
?>

this is all I have please check it...这就是我所拥有的请检查它...

Note Just tell me a solution,When I test the above code on window xp, it will run correctly and i upload up to 103kb file, but it will not work on window 8, and create problem like I mention above... is the Xammp latest version have bugs, or what is the problem?注意只是告诉我一个解决方案,当我在 windows xp 上测试上述代码时,它会正确运行并且我上传了最多 103kb 的文件,但它在 windows 8 上不起作用,并且会产生我上面提到的问题......是Xammp 最新版本有bug,还是什么问题?

Note The error is not the code, the error is logical, so please be specific and to the point.注意错误不是代码,错误是合乎逻辑的,所以请具体点。 Please add the explanatory answer....请添加解释性答案....

As @Shikata wrote, be careful with:正如@Shikata 所写,请注意:

$TARGET_PATH = "images/";
$image['name'] =$image['name'];
$TARGET_PATH .= $image['name'];

Instead just use:而只是使用:

$TARGET_PATH = "images/".$image['name'];

And like the other answers, you must check the validation!和其他答案一样,您必须检查验证! Also erase the strar_session();同时擦除 strar_session(); from connection.php来自 connection.php

Also as sugestion try to check PDO prepared-statements http://php.net//manual/en/pdo.prepared-statements.php (I really suggest reading this, please give it a try, it can give you a more secure way to implement queries by binding parameters and by that way avoiding some SQL injections problems)另外作为建议尝试检查 PDO 准备好的语句http://php.net//manual/en/pdo.prepared-statements.php (我真的建议阅读这个,请试一试,它可以给你一个更安全的通过绑定参数来实现查询的方法,从而避免一些 SQL 注入问题)

(Sorry for not going to the point with the previous answer) That's about the code, as much as I try it (few time), about the upload problem maybe checking the upload_max_filesize and post_max_size . (抱歉,我没有在前面的答案中指出这一点)这是关于代码,尽管我尝试过(很少),关于上传问题,可能会检查upload_max_filesizepost_max_size If the problem is still there try checking max_input_time and max_execution_time .如果问题仍然存在,请尝试检查max_input_timemax_execution_time I hope you get a solution.我希望你得到一个解决方案。

may be its file permission error可能是它的文件权限错误

in linux open terminal and enable file permission在linux中打开终端并启用文件权限

chmod -R 777 /var/www/html/folder_name (or your path) chmod -R 777 /var/www/html/folder_name (或您的路径)

to disable :-禁用:-

chmod -R 775 /var/www/html/folder_name chmod -R 775 /var/www/html/folder_name

or go to your file>right click >change permission from read only to read and write或转到您的文件>右键单击>将权限从只读更改为读写

尝试在您的 wamp 或 xamp 文件夹中搜索 my.ini 并将 max_allowed_pa​​ckets 编辑为更大的尺寸。谢谢

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

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