How to upload a file in PHP?
The following is an example of how to upload a file into a server using PHP. In this example you display a form and users will input their pictures. The file will be saved as “uploadingafile.php” in the PHP supported server.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitionl.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>An example of how to upload a file into a server</title>
</head>
<body>
<?php // uploadingafile.php
// address error handling.
ini_set (‘display_errors’,1);
error_reporting (E_ALL & ~E_NOTICE);
if (isset ($_POST[‘submit’])) { //handle my form…
// upload my file.
if (move_uploaded_file ($_FILES[‘thefile’][‘tmp_name’],”../ul/{$_FILES[‘thefile’][‘name’]}”)) {
print ‘<p> Your file was uploaded.</p>’;
} else { // we have a problem.
print ‘<p>Your file could not be uploaded because:</p>’;
switch ($_FILES[‘thefile’][‘error’]) {
case 1:
print ‘The file exceeds the upload_max_filesize setting in php.ini’;
break;
case 2:
print ‘The file exceeds the upload_max_filesize setting in HTML’;
break;
case 3:
print ‘The file was only partially uploaded’;
break;
case 4:
print ‘No file was uploaded’;
break;
}
}
}
// display the form.
<form action=”uploadingafile.php” enctype=”multipart/form-data” method=”post”>
<p>Upload your file to our server.<br /><br />
<input type=”hidden” name=”MAX_FILE_SIZE” value=”9000000000” />
<input type=”file” name=”thefile” />.<br /><br />
<input type=”submit” name=”submit” value=”Upload your file” /></p>
</form>
</body>
</html>
|