High score experts translate a php file upload code

Source: Internet
Author: User
Tags php file upload
Translate PHP file upload code this code is translated into PHP: 3. & nbsp; compile the index of the Upload file processing file on the WEB end. jsp uses jspsmart to process the code as follows & lt ;%@ & nbsp; page & nbsp; importcom. jspsmart. upload. *, common. textHandle % & gt; & lt; % boolean & nbsp; isO High Score Help expert translate a php file upload code
This code is translated into PHP:

3. compile the index. jsp file above for file upload and processing on the WEB end.

Use jspsmart to process the code as follows:

<% @ Page import = "com. jspsmart. upload. *, common. TextHandle" %> <%
Boolean isOK = false;

// Program initialization --------------------------------------------------------------
String path_tmp = request. getRealPath ("/") + "Upload ";
String filename_p = TextHandle. getDirectory ();
String path_new = request. getRealPath ("/") + "Upload \" + filename_p;

// File Upload ----------------------------------------------------------------
SmartUpload su = new SmartUpload ();
Su. initialize (pageContext );
Su. upload ();
Int count = su. save (path_tmp );

String file_name = "", file_ext, newFileName = "none ";

IsOK = true;

If (count> 0 ){
Com. jspsmart. upload. File file = su. getFiles (). getFile (0 );
If (! File. isMissing () {// Has an uploaded file ------------------------
File_name = file. getFileName (). toLowerCase ();

If (file. getSize () = 0) {// delete ------------ if an empty file is sent ------------
Java. io. File fileName1 = new java. io. File (path_tmp, file_name );
If (fileName1.exists () fileName1.delete ();
IsOK = false;
}
Else {// check whether the file is correct ------------------
File_ext = file_name.substring (file_name.lastIndexOf (".") + 1, file_name.length ());
If (file_ext.equals ("jpg") | file_ext.equals ("gif") | file_ext.equals ("jpeg ")){
String number = String. valueOf (new java. util. Date (). getTime ());
NewFileName = filename_p + number + "." + file_ext;
// Save the file by name ------------------------------------------
// First check whether the directory exists. if not, create a directory.
Java. io. File didrop = new java. io. File (path_new );
If (! DirOp. exists () dirOp. mkdir ();

String path1 = path_tmp + "\" + file_name;
String path2 = path_new + "\" + newFileName;
Java. io. File fromFile = new java. io. File (path1 );
Java. io. File toFile = new java. io. File (path2 );
Java. io. FileInputStream FCM = null;
Java. io. FileOutputStream fos = null;
Try {
FS = new java. io. FileInputStream (fromFile );
Fos = new java. io. FileOutputStream (toFile );
Int bytesRead;
Byte [] buf = new byte [4*1024]; // 4 K buffer ---------
While (bytesRead = FI. read (buf ))! =-1) fos. write (buf, 0, bytesRead );
Fos. close (); FCM. close ();
}
Catch (java. io. IOException e ){
System. out. println (e );

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.