Computer Science Canada

File Uploading

Author:  Mackie [ Tue Feb 19, 2008 12:16 am ]
Post subject:  File Uploading

Hey, just wondering. How I could make a file upload form?

Author:  uberwalla [ Sun Feb 24, 2008 12:09 pm ]
Post subject:  Re: File Uploading

Depending on your servers settings this may work: (just made it quickly)

This file can be named whatever. Index.html, index.php, poo.html (anything basically)
php:

<html>
<head>
</head>
<body>
<form action="upload.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>


This file MUST be named upload.php unless you change the action in the html form.
php:

<?php
$target_path = "uploads/";

$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!";
}
?>


Also i wouldn't recommend having this thing viewable to the public. If you do and it works, you may have a lot of random junk put on your site


: