高分高手翻譯一PHP檔案上傳代碼

來源:互聯網
上載者:User
高分求助高手翻譯一PHP檔案上傳代碼
這個代碼翻譯成PHP的:

3. 在WEB端編寫上傳檔案處理檔案如上面的index.jsp

使用jspsmart處理代碼如下

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

//程式初始化--------------------------------------------------------------
String path_tmp = request.getRealPath("/") + "Upload";
String filename_p = TextHandle.getDirectory();
String path_new = request.getRealPath("/") + "Upload\\" + filename_p;

//檔案上傳----------------------------------------------------------------
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.upload();
int count = su.save(path_tmp);

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

isOK=true;

if(count>0){
com.jspsmart.upload.File file = su.getFiles().getFile(0);
if (!file.isMissing()){ //有上傳的檔案------------------------
file_name = file.getFileName().toLowerCase();

if(file.getSize() == 0){ //如果傳來的是空檔案則刪除------------
java.io.File fileName1=new java.io.File(path_tmp,file_name);
if(fileName1.exists()) fileName1.delete();
isOK=false;
}
else{ //看是否是正確的檔案------------------
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;
//將檔案換名儲存------------------------------------------
//首先看目錄是否存在,如果不存在則建立目錄
java.io.File dirOp = 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 fis = null;
java.io.FileOutputStream fos = null;
try{
fis = new java.io.FileInputStream(fromFile);
fos = new java.io.FileOutputStream(toFile);
int bytesRead;
byte[] buf = new byte[4 * 1024]; // 4K buffer ---------
while ((bytesRead = fis.read(buf)) != -1) fos.write(buf, 0, bytesRead);
fos.close(); fis.close();
}
catch(java.io.IOException e){
System.out.println(e);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.