0

I've been trying to get my PHP upload script working, the HTML side seems to work but the PHP keeps returning a failed result. I am using iPage hosting. Here is my script:

<?php
if(isset($_FILES['userfile'])){
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo "<p>";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File is valid, and was successfully uploaded.\n";
} else {
   echo "Upload failed";
}

echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
} else {
?>
<form enctype="multipart/form-data" action="" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>
<?php
}
?>
4
  • 2
    Check if the folder is writeable. If it is, then try a relative path instead of what you're using now. I.e.: $uploaddir = 'uploads/'; Commented Apr 17, 2014 at 23:54
  • 1
    Thanks Fred. Needed to do both to get it working :) Commented Apr 18, 2014 at 0:02
  • If you want the question closed, I can make it as an answer, it's up to you. Commented Apr 18, 2014 at 0:04
  • As per your request, it has been done. Cheers Commented Apr 18, 2014 at 0:09

2 Answers 2

2

Comment to answer to close the question, since that's what the issue was, both.

Check to see if the folder is writeable. If it is, then try a relative path instead of what you're using now.

I.e.: $uploaddir = 'uploads/';

Sign up to request clarification or add additional context in comments.

Comments

0

Make sure the upload directory has the correct permissions - Writable and Proper Owner

You will run into issues when the file size is over 512000, you should check the size of the file using the $_FILE array and return an error message. Just a suggestion as you have already closed this.

    // Error Checking Extended
    if($_FILES['userfile']['error'] == 2) {
       echo "You've exceeded the maximum file upload size of 512kb.";
       return false;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.