0

I'm trying to create an image uploader for my web application. I've tried searching online for solutions on how to create one, but haven't had much luck finding anything of real help. I'm also not sure what words I should be using to search for the right results. I did find something on the W3Schools website, (http://www.w3schools.com/php/php_file_upload.asp), but I still can't get it to upload a file to my server. Not certain of what the issue is since i followed the instructions on the website to set it up. I also found this (JavaScript: Upload file), but it's not quite exactly what I want it to do. I still need the php script since the image is not the only thing i'm going to be sending to the server. I will also be sending other information to be stored into my database on the server, such as image name and other information associated with the image.

The php script that I'm using, which i got off the W3S website, is down below. Where I am stuck at is that once I hit the submit button my page crashes or I should say I get a Server Error back, but with no information as to what caused it. Any help would be greatly appreciated

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjepg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts)) {
    if ($_FILES["file"]["error"] > 0) {
        echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
    else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " KB<br>";
        echo "Stored in: " . $_FILES["file"]["tmp_name"];

        if (file_exists("public_hmtl/uploads/" . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
        }
        else {
            move_uploaded_file($_FILES["file"]["tmp_name"], "public_hmtl/uploads/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "public_hmtl/uploads/" . $_FILES["file"]["name"];
        }
    }
}
else {
    echo "Invalid file";
}
?>

Here's also the html for the form.

    <!DOCTYPE html>
    <html>
    <head>
        <title>Location details</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>
    <body>
        <div>
            <form enctype="multipart/form-data" method="post" action="scripts/snap.php">
        <!--<p>
            <input id="plat" class="plat" name="plat" type="hidden" value="" />
            <input id="plon" class="plon" name="plon" type="hidden" value=""  />
        </p>-->
        <div class="row">
            <label for="file">Select an image to Upload</label><br />
            <input type="file" name="file" id="file" />
        </div>
        <div id="filename"></div>
        <div id="fileSize"></div>    
        <div id="fileType"></div>
        <div class="row">
            <input type="submit" name="submit" value="Submit" />
            <!--<input type="button" onclick="uploadFile()" value="Upload" />-->
        </div>
        <div id="progressNumber"></div>
    </form>
</div>

Community
  • 1
  • 1
Razpiento
  • 67
  • 1
  • 11

2 Answers2

2

In if statement, there is mismatch open and closed tag, correct it

if ( ( ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjepg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
)
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts) )
Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
  • Thanks, that seems to have fixed it. Now I just got to deal with the type of files it can accept. Even though i'm giving a .jpg file to upload it tells me that its an invalid file type. – Razpiento Apr 18 '13 at 18:59
  • 1
    @RenegadeScar i think it file size greater than 20000 – Tamil Selvan C Apr 18 '13 at 19:01
  • Yea, they file was bigger than the 20000 or 20KB. I just need to bump up that number and i'm good to go. Thanks a lot Man! – Razpiento Apr 18 '13 at 19:09
0

It would be good if you can expand on this server error that you get. I don't have enough rep to comment so have to ask for clarification here.

  • What server are you uploading to? Does the public_html/uploads/ directory actually exist? Is the path you are using to point to it correct?
  • What is your temporary upload directory set to?
  • Also, check that you may be picking up the file extension correctly. Use pathinfo(), perhaps. Example below.

    $extension = pathinfo($_FILES['file']['name']);

    $ext = $extension['extension'];

Meezaan-ud-Din
  • 1,213
  • 16
  • 21
  • Yes the public_html/uploads/ directory exists i just added today. The server that i'm using is a server which is being paid for in Chicago. As far for the server error I replied above in a comment saying that I am not getting any good information back as to what the error from the server is. It is just my browser telling me there is an error. I'm not using a temp upload directory. I'm following instructions that i found on a website, which i linked above. I'll also give that last point a try. – Razpiento Apr 18 '13 at 18:51