gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:10:38 25 4
gpt4 key购买 nike

我创建了一个简单的上传表单,所有代码都运行良好,但是当我上传 2kb、20kb、26kb 时,图片会上传,但是当我尝试上传 60kb、66kb 时,页面会卡在加载中,我不知道为什么会这样,我还更改了 php.ini 中的最大上传大小。谁能告诉我为什么会这样?

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

这就是我的全部,请检查...

注意 告诉我一个解决方案,当我在window xp上测试上面的代码时,它会正确运行并且我上传了103kb的文件,但它在window 8上不起作用,并创建像我上面提到的问题...是 Xammp 最新版本有错误,还是有什么问题?

注意 错误不是代码,错误是合乎逻辑的,所以请具体、中肯。请添加解释性答案....

最佳答案

正如@Shikata 所写,注意:

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

只需使用:

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

和其他答案一样,您必须检查验证!同时删除 strar_session();来自 connection.php

同时建议尝试检查 PDO 准备语句 http://php.net//manual/en/pdo.prepared-statements.php (强烈建议阅读此文,请尝试一下,它可以为您提供一种更安全的方式来通过绑定(bind)参数实现查询,从而避免一些 SQL 注入(inject)问题)

(很抱歉之前的回答没有进入重点)这是关于代码的,尽管我尝试了很多次(几次),关于上传问题可能会检查 upload_max_filesizepost_max_size。如果问题仍然存在,请尝试检查 max_input_timemax_execution_time。我希望你能找到解决方案。

关于php - 在php中上传用户个人资料图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25255582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com