PHP Novice on the road (eight)

Source: Internet
Author: User
Tags define file size file upload ini php file php3 file
7. File Upload





you can use PHP to implement file upload function, note that the client's browser should be Netscape3 above or above the IE3 version. Also, because this program is related to your PHP configuration file (PHP3 is PHP3.INI,PHP4 for php.in) settings. Please check your PHP profile before executing this program for any of the following settings:





, Upload_tmp_dir the line's annotation, that is, the preceding semicolon ";", so that the line works in the php.ini document. Upload_tmp_dir is used to define the temporary path of the upload file, where you can also define an absolute path, such as: Upload_tmp_dir = D:upload of course, your D:upload directory must have read and write permission at this time.





If you have already defined the path of the upload in your. PhP3 program, the path to the uploaded file is based on the path defined in the. php3 program. In the following example, the Receiver.php3 file specifies the directory in which to store the uploaded files: d:upload.





upload_max_filesize is used to limit the maximum size of uploaded files in php processing, in bytes, the default is 2097152 = 2*1024*1024 bytes (2 trillion), you can modify the default to define the maximum upload file size.





changes do not forget to restart Apache,iis or PWS service OH.


  


also in PHP, there are several points to note about file uploads:


1. In the form form, set the method property to the Post,enctype property to Multipart/form-data;





2. You can add a hidden type of input box to the form form, where the name is a hidden range of max_file_size, and you can limit the size of the uploaded file by setting its value. Of course, this value can not exceed the PHP configuration file (PHP3 for PHP3.INI,PHP4 php.ini) in the upload_max_filesize, note that this input box must be placed in all file type of the input box in front, otherwise it is invalid OH ;





3. After the PHP program is run, the upload file is placed in the temporary directory. If the uploaded file is not renamed or moved, the file will automatically be deleted from the temporary folder at the end of the request, so we'd better immediately upload the new upload file to a permanent directory or change the file name.








First we need a form page to upload a file (upload.htm):


<HTML>


<HEAD>


<title>upload Your file</title>


</HEAD>


<BODY>


<form action= "Receiver.php3"


enctype= "Multipart/form-data" method=post>


<input type= "HIDDEN"


name= "max_file_size" value= "2000000" >


<input type= "FILE"


name= "UploadFile" size= "maxlength=" >


<BR><BR>


<input type= "SUBMIT" value= "Upload file!"


Name= "Sendit" >


<input type= "SUBMIT" value= "Cancel"


name= "Cancelit" ><BR>


</FORM>


</BODY>


</HTML>





php file (receiver.php3) for processing uploaded files


;?


function do_upload ()


{


Global $uploadfile, $uploadfile _size;


Global $local _file, $error _msg;


if ($uploadfile = = "None")


{


$error _msg = "Sorry, you did not select any file upload!" ";


return;


}


if ($uploadfile _size > 2000000)


{


$error _msg = "Sorry, the file you want to upload is too big!" ";


return;


}


$the _time = time ();





//Here Specify the directory you use to store the uploaded files, and you will need to write permissions on the following directories


//At the same time, we can also give the upload file to specify another directory, such as: $upload _dir = "/local/uploads";





$upload _dir = "D:/upload";


$local _file = "$upload _dir/$the _time";


if (file_exists (' $local _file '))


{


$seq = 1;


while (file_exists ("$upload _dir/$the _time$seq")) {$seq + +;}


$local _file = "$upload _dir/$the _time$seq";


};


rename ($uploadfile, $local _file);


Display_page ();


}


function display_page ()


{


//This is your page content


}


?>


<HTML>


<HEAD>


<title>php3 receiving script</title>


</HEAD>


<BODY>


;?


if ($error _msg) {echo "<B> $error _msg</b><br><br>";}


if ($sendit)


{


do_upload ();


echo "File Upload succeeded!" ";


}


ElseIf ($cancelit)


{


header ("Location: $some _other_script");


echo "File upload failed!";


exit;


}


Else


{


Some_other_func ();


}


?>


</BODY>


</HTML>








Related Article

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.