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

时间:2014-08-12 03:12:42

标签: php mysql apache xampp

我创建了一个简单的上传表单,所有代码都运行得很好,但是当我上传2kb,20kb,26kb时,pic会上传但是当我尝试上传60kb,66kb时页面会卡在加载上, 我不知道为什么会这样,我也改变了php.ini中的max-upload-size。 谁能告诉我为什么会这样?

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

这就是我所有请检查它...

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

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

3 个答案:

答案 0 :(得分:1)

正如@Shikata写的那样,小心:

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

而只是使用:

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

和其他答案一样,你必须检查验证!也删掉strar_session();来自connection.php

同样作为sugestion尝试检查PDO准备语句http://php.net//manual/en/pdo.prepared-statements.php(我真的建议阅读此内容,请试一试,它可以通过绑定参数为您提供更安全的方式来实现查询,并通过这种方式避免一些SQL注入问题)

(抱歉没有达到上一个答案的要点) 关于上传问题,关于代码的问题(尽可能少)我可能会检查upload_max_filesizepost_max_size。如果问题仍然存在,请尝试检查max_input_timemax_execution_time。我希望你能得到解决方案。

答案 1 :(得分:0)

可能是其文件权限错误

在linux开放终端和启用文件权限

  

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

停用: -

  

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

或转到您的文件&gt;右键单击&gt;将权限从只读更改为读写

答案 2 :(得分:0)

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

相关问题