user2910074 user2910074 - 7 months ago 10
PHP Question

How can I allow users of my website to upload media files?

I'm currently following a tutorial for a basic media file upload form. I've tried adding it to my website, but I always get the message "There was an error uploading the file, please try again!" when I try to upload images using it.

I've put the following html file onto my server:

<DOCTYPE! html>
<html>


<head>
</head>


<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>


</html>


I have then created the file uploader.php, which looks like this:

<?php

// Where the file is going to be placed
$target_path = "../uploads/";

/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);



if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

?>


I've stored these files in the same directory. I've then created another directory called uploads - which is writable.

Why is this not working?

Answer

It will Work. I checked in my desktop.

Change value="100000" to value="1000000"

Like

<input type="hidden" name="MAX_FILE_SIZE" value="100000" />

And,

$target_path = "uploads/";

The MAX_FILE_SIZE hidden field (measured in bytes) must precede the file input field, and its value is the maximum filesize accepted by PHP.

For more info, click File Upload - php manual

User's Requirement (As, He asked in comment)

Append your file name with time(). There are many ways to do. But, this one you can use.

$target_path = $target_path .time() .basename( $_FILES['uploadedfile']['name']);