2015-10-28 18:59


  • file
  • php

I'm currently coding the most basic file upload to go to our server from an input type="file" attribute. This is my HTML:

<form enctype="multipart/form-data" action="register-complete.php" method="post">
        <h5>Register Now</h5>
        <input type="text" class="form-control" name="Username" placeholder="Login Name"/><br />
        <input type="text" class="form-control" name="Displayname" placeholder="Display Name"/><br />
        <input type="text" class="form-control" name="Email" placeholder="Email" /><br />
        <input type="radio" name="Paypal" value="1" /> This is my Paypal email.<br />
        <input type="radio" name="Paypal" value="0" /> I do not want payment. I wish to preserve anonymity.<br /><br />
        Avatar Picture: <br /><input type="file" name="AvatarPicture" id="AvatarPicture" />*500kb max file size.<br />*Accepted filetypes: .jpg, .png<br /><br />
        <input type="text" class="form-control" name="Description" placeholder="Account Description"/><br />
        <input type="password" class="form-control" name="Password" placeholder="Password"/><br />
        <input type="password" class="form-control" name="PasswordConfirm" placeholder="Confirm Password"/><br />
        <p class="text-center"><input type="submit" class="btn btn-primary" value="Register" name="submit" /></p>

Basically I'm just concerned with the AvatarPicture input, and just get it to upload a file to my server. Here is the PHP code I have to do that.

$username = $_REQUEST["Username"];
$displayname = $_REQUEST["Displayname"];
$email = $_REQUEST["Email"];
$paypal = $_REQUEST["Paypal"];

$target_dir = "images/avipictures/";
$target_file = $target_dir . basename($_FILES["AvatarPicture"]["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["AvatarPicture"]["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["AvatarPicture"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
    echo "Sorry, only JPG, JPEG, PNG 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["AvatarPicture"]["tmp_name"],$target_file)) {
        echo "The file has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";

This is literally the same exact thing as the w3schools code to the tee. However I am receiving an internal server 500 error unless I change the "tmp_name" in the move_uploaded_file() to "name", which just leads to the else statement. I've been messing around with this all day and I have just been tearing my hair out at just how simple this bit of code should be but doesnt seem to be at all. Any ideas? (Also, the file_uploads is set to on and the default largest file size is set to 50mb.)

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享