user3931708
user3931708

Reputation:

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. can anybody tell me why this is happening?

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

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?

Note The error is not the code, the error is logical, so please be specific and to the point. Please add the explanatory answer....

Upvotes: 1

Views: 16983

Answers (3)

user12323974
user12323974

Reputation: 1

try to search my.ini in your wamp or xamp folder and edit max_allowed_packets to a little bit larger sized.. thanks

Upvotes: 0

RinShan Kolayil
RinShan Kolayil

Reputation: 23

may be its file permission error

in linux open terminal and enable file permission

chmod -R 777 /var/www/html/folder_name (or your path)

to disable :-

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

or go to your file>right click >change permission from read only to read and write

Upvotes: 0

As @Shikata wrote, be careful with:

$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(); from 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)

(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. If the problem is still there try checking max_input_time and max_execution_time. I hope you get a solution.

Upvotes: 1

Related Questions