php tutorial file upload program (two simple file upload program)
/ *
These two file upload program is very simple, it is suitable for beginners to learn php file upload examples tutorial Oh.
* /
if (! $ uploadaction):
?>
<html>
<head>
<title> File Upload Interface </ title>
</ head>
<body>
<table>
<tr align = "center">
<td> <form enctype = "multipart / form-data" name = "submitform" action = "upload.php" method = "post">
<input type = "hidden" name = "max_file_size" value = "1000000"> <input type = "hidden" name = "uploadaction" value = "1">
</ td> </ tr>
<tr align = "center">
<td> <input name = "uploadfile" type = "file" size = "30"> </ td>
</ tr>
<tr align = "center">
<td> <input name = "submit" value = "Submit" type = "submit"> </ td>
<td> <input name = "reset" value = "reset" type = "reset"> </ td>
</ tr>
</ form>
</ table>
</ center>
</ body>
</ html>
<?
else:
?>
<html>
<head>
<title> file upload code </ title>
</ head>
<body>
<?
$ uploadaction = 0;
echo "good!";
$ timelimit = 60; / * Set the timeout limit The default time is 30 seconds Unlimited when set to 0 * /
set_time_limit ($ timelimit);
if (($ uploadfile! = "none"))
{
$ uploadpath = addslashes (dirname ($ path_translated)). "upload"; / / upload file storage path
$ filename = $ uploadpath. $ uploadfile_name; // upload file name
if ($ uploadfile_size <1024) // upload file size
{
$ filesize = (string) $ uploadfile_size. "Bytes";
}
elseif ($ uploadfile_size <(1024 * 1024))
{
$ filesize = number_format ((double) ($ uploadfile_size / 1024), 1). "kb";
}
else {
$ filesize = number_format ((double) ($ uploadfile_size / (1024 * 1024)), 1). "mb";
}
if (! file_exists ($ filename))
{
if (copy ($ uploadfile, $ filename))
{echo "file $ uploadfile_name ($ filesize) upload success!";}
else
{echo "file $ uploadfile_name upload failed!";}
unlink ($ uploadfile);
}
else
{echo "file $ uploadfile_name already exists!";}
}
else
{echo "You did not choose any file upload!";}
set_time_limit (30); // Restore the default timeout setting
?>
<br> <a href= "upload.php"> Back </a>
</ body>
</ html>
<?
endif;
?>
Method Two
<html>
<head>
<title> File Upload </ title>
</ head>
<body>
<table>
<form enctype = "multipart / form-data" name = myform method = "post">
<tr> <td> File Upload </ td> <td> <input name = "myfile" type = "file"> </ td> </ tr>
<tr> <td colspan = "2"> <input name = "submit" value = "upload" type = "submit">
</ form>
<? php
if (isset ($ submit)) {
if ($ myfile! = "none") {
$ upload_dir = "c: winnttemp";
$ up = copy ($ myfile, $ upload_dir);
if ($ up == 1) {
print ("file upload success!");
}
else {
print ("file upload failed.");
}
unlink ($ myfile);
}
else {
print ("You did not upload any files");
}
}
?>
</ td> </ tr>
</ table>
</ body>
</ html>