Implementing file uploads with PHP3

Source: Internet
Author: User

PHP3 is a very powerful CGI scripting language, with its linguistic features largely rooted in C, which is more like Perl in terms of its implementation, and its intrinsic support for the database makes it a powerful competitor for ASPs.

First, there must be an interface at the front desk

< HTML >
< head >
< TITLE > File Upload interface </title >
< body >< TABLE >< CENTER >
< FORM enctype = "Multipart/form-data" NAME = "SubmitForm"
ACTION = "Upload.php3" method = "POST" >
< INPUT TYPE = "hidden" NAME =
"Max_file_size" VALUE = "1000000" >
< TR >
< TD >< INPUT NAME = "UploadFile"
TYPE = "file" SIZE = "></td >
</tr >
< TR >
< TD >< INPUT NAME = "submit" VALUE =
"Submit" TYPE = "Submit" ></td >
< TD >< INPUT NAME = "reset" VALUE =
"Reset" TYPE = "reset" ></td >
</tr >
</form ></center ></table ></body >

It is worth noting that a max_file_size hidden range, by setting its value, can limit the size of the uploaded file.
The file upload operation is then implemented in the background. If it's just the most basic upload, a few lines can be done.

<?
If ($UploadFile!= "None")
{
Copy ($UploadFile, "$UploadFile _name");
Unlink ($UploadFile);
}
Else
{
echo "You didn't choose any file upload!" ";
}
? >

Because the value passed by form is automatically assigned to a variable of the same name, the uploaded file can be accessed directly via $uploadfile, but since this is a variable that saves the file, the file name must be obtained by another $uploadfile_name variable. Delete the temporary file (unlink) after copying the document.

The above code can work, but the practical application of the loopholes, let us gradually improve the

First of all, the uploaded file must have a fixed directory save, where we save it with a $uploadpath variable, such as

$UploadPath = "/home/flier/upload/";

Or a complex bit of automatic positioning, such as

$UploadPath = Addslashes (dirname
($PATH _translated)). " \\upload\\ ";
$PATH _translated is the name of the current routing directory,

We assume that the uploaded file is saved with a subdirectory named upload. The DirName function returns its directory name and then adds the subdirectory name
Then use a variable $filename to save the full uploaded filename and path $filename = $UploadPath. $UploadFile _name;

Secondly, we also want to let users know the upload file's brief information, such as the size of the uploaded file
if ($UploadFile _size < 1024)
Upload file size
{
$FileSize = (string) $UploadFile _size. "Byte";
}
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";
}

The Number_format function plays the role of formatted output, please refer to the manual for specific usage

Next we must take into account the existence of the file and the failure of the copy operation, and provide the appropriate information

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

Then we should take into account the large file upload is prone to timeout, you can use Set_time_limit ($TimeLimit); Increase the timeout limit time.
Finally, by combining the section and implementation code into a single file, we add an implied value < INPUT TYPE = "hidden" NAME = "uploadaction" value = "1" > indicate the current shape in the form, in order to achieve this idea State (interface or implementation) in order to differentiate the treated completion code as follows (filename upload.php3)

<?
if (! $UploadAction):
? >
< HTML >
< head >
< TITLE > File Upload interface </title >
< body >< TABLE >< CENTER >
< FORM enctype = "Multipart/form-data" NAME =
"SubmitForm" ACTION = "upload.php3" method = "POST" >
< INPUT TYPE = "hidden" NAME =
"Max_file_size" VALUE = "1000000" >
< INPUT TYPE = "hidden" NAME = "uploadaction" VALUE = "1" >
< TR >
< TD >< INPUT NAME = "UploadFile" TYPE =
"File" SIZE = "></td >"
</tr >
< TR >
< TD >< INPUT NAME = "submit" VALUE =
"Submit" TYPE = "Submit" ></td >
< TD >< INPUT NAME = "reset" VALUE =
"Reset" TYPE = "reset" ></td >
</tr >
</form ></center ></table ></body >
<?
Else
? >
< HTML >
< head >
< TITLE > File Upload code </title >
< BODY >
<?
$UploadAction = 0;

$TimeLimit = 60; /* Set timeout limit time
The default time is 30 seconds
Set to 0 when not limited to * *
Set_time_limit ($TimeLimit);

If (($UploadFile!= "None") &&
($UploadFile!= ""))
{
$UploadPath = Addslashes (dirname
($PATH _translated)). " \\upload\\ "; Upload File Store path
$FileName = $UploadPath. $UploadFile _name;
Upload file name
if ($UploadFile _size < 1024)
Upload file size
{
$FileSize = (string) $UploadFile _size. "Byte";
}
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 succeeded! ";
}
Else
{
echo "File $UploadFile _name upload failed! ";
}
Unlink ($UploadFile);
}
Else
{
echo "File $UploadFile _name already exists! ";
}
}
Else
{
echo "You didn't choose any file upload!" ";
}

Set_time_limit (30); Restore default timeout settings
? >
< BR >< A HREF = "Upload.php3" > Back </A >
</body >
<?
endif



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.