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 );