M Smith
M Smith

Reputation: 430

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="10000000" />
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?

EDIT: I'm now able to upload files upto 1MB in size, but anything larger than this fails to upload. As you can see in the HTML script, I've set the max file size to 10MB - so this should work. Any idea why it isn't?

Upvotes: 0

Views: 9659

Answers (1)

Nana Partykar
Nana Partykar

Reputation: 10548

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']); 

Upvotes: 2

Related Questions