File Upload

Provided that your PHP configuration allows files to be uploaded, creating a file upload form is fairly straightforward.

First you need to create the form. Two fields are needed, one to specify the file to be uploaded and a second to specify where to put it on your site.

Here's an example (formatting etc is omitted):

<form enctype="multipart/form-data" action="upload.php" method="post">
File: <input type="file" name="userfile" />
Destination Path: <input type="text" name="pth" />
<input type='submit' value='Upload' />

Next you need to add the load_file function to the web page called to process the form (upload.php in the above example).

function load_file($file,$path) {
$pth = $_POST[$path];
if ($pth == "") return "Destination Path not Specified";
$ufile = $_FILES[$file]['tmp_name'];
if ($ufile == "") return "No file specified";
if (!ereg("/$", $pth)) $pth .= "/";
$loc = $pth.$_FILES[$file]['name'];
if (!file_exists($loc)) {echo '1';
  return "Upload Successful";
return "File Already Exists";

Finally, all that remains is to call the function from within the destination page at the spot in the page where you want the message returned from the function to be displayed passing it the field names from your form.

echo load_file('userfile','pth');


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow