crazyjedi98
crazyjedi98

Reputation: 23

PHP File Upload Isn't Working

So I built this simple script to upload files to an XAMPP server, and . . . it didn't work. When I ran it, it dies after the first if statement. EDIT: FIXED

Here's the FIXED html code:

<!DOCTYPE html>

<html>
<head>
<title>Awesome Life: File Upload</title>
<link rel="stylesheet" href="forms.css">
</head>
<body>
<div id="big_wrap">
    <section id="sign_up">
        <form action="http://localhost/mail.php" method="post" ENCTYPE="multipart/form-data">
            <span id="upText">File to Upload</span> 
            <br />
            <input style="margin-top:5px;" type="file" name="fileName">
            <br />
            <br />
            <input type="submit" id="button" name="submit" value="Upload">
        </form>
    </section>
</div>
</body>
</html>

And here's the FIXED script:

<?php
if(!isset($_FILES["fileName"])){
    die("It didn't work!");
}else{
    move_uploaded_file($_FILES["fileName"]["tmp_name"], "\xampp\htdocs\".$_FILES["fileName"]  ["name"]) or die("Didn't work");
}
?>

The file type isn't determined or tested, as I'm not going to be using this on the World Wide Web. I'm simply wondering why it doesn't work. Thanks!

EDIT: It was a simple matter of changing the edit permissions of the htdocs folder. I unchecked the read-only box and it worked fine. Thanks to all for contributing, and especially to Fred -ii-

Upvotes: 0

Views: 1097

Answers (3)

Funk Forty Niner
Funk Forty Niner

Reputation: 74220

Turns out it was a mix of many things, but as OP stated: (and more importantly)

"Fred -ii-, you are beast. All I had to do was remove the "read-only" permission from the htdocs folder and add another slash to the end. The other answers helped a lot, too"

Answer transformed from my comment:

(Main problem) Make sure folders have write permissions. and you probably need another \ in "\xampp\htdocs" as "\xampp\htdocs\" or may need to do "/xampp/htdocs/"

Upvotes: 0

Girish
Girish

Reputation: 12127

replace form attribute

ENCTYPE="multipart/file-data"

to

ENCTYPE="multipart/form-data"

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

multipart/file-data is incorrect. It should be multipart/form-data instead.

Also, you should use move_uploaded_file to move the uploaded file somewhere. Not copy.

Upvotes: 2

Related Questions