Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Image upload to a website

Options
  • 12-04-2006 10:53am
    #1
    Registered Users Posts: 871 ✭✭✭


    Hi I'm trying to add an image upload function to my project, can anyone suggest a good tutorial or resource, (maybe jsp or javascript??). I am trying to add this function to a website so that it will save the image to a predefined folder and rename the image too.


Comments

  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    look up php or asp - scripts are already available that do precisely what you want

    try searching for

    php image uploader script or something similiar


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    [PHP]<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="2048000">
    File: <input name="userfile" type="file" /><br />
    <input type="submit" value="Upload" />
    </form>

    <?php
    if (@is_uploaded_file($_FILES[&quot;userfile"]["tmp_name"])) {
    copy($_FILES["userfile"]["tmp_name"], "/images/" . $_FILES["userfile"]["name"]);
    echo "<p>File uploaded successfully.</p>";
    }
    ?>[/PHP]

    Something simple like this should get you started.


  • Registered Users Posts: 130 ✭✭irishfeller


    If you want to use jsp then the following code below is what u need.

    - Put the code in a jsp e.g. processAttachment.jsp
    - Use a multipart form as in example above and point form at the new jsp 'processAttachment.jsp'



    <%@ page import="java.util.Vector"%>
    <%@ page import="java.util.Date"%>
    <%@ page import="java.text.SimpleDateFormat"%>
    <%@ page import="com.oreilly.servlet.MultipartRequest" %>
    <%@ page import="java.io.*" %>
    <%@ page import="java.util.*" %>


    <%@ page session="true"%>

    <%

    String returnMessage = "";
    //String saveDirectory = application.getRealPath("/") + "/attachments/";
    //String relativeDirectory = "../attachments/";
    String saveFile = "";
    String returnTo="listImages.jsp";
    String sql = "";


    boolean saveToDB = true;
    byte dataBytes[] = new byte[1];
    String file = "";
    String contentType = request.getContentType();

    try
    {

    MultipartRequest multi= new MultipartRequest(request,".",5*1024*1024);

    Enumeration files=multi.getFileNames();
    File f=null;
    if(files.hasMoreElements())
    {
    System.out.println("TEST 9");
    String name=(String)files.nextElement();
    saveFile=multi.getFilesystemName(name);

    String type=multi.getContentType(name);
    f=multi.getFile(name);

    InputStream is = new FileInputStream(f);
    byte b[]=new byte[is.available()];
    is.read(b);


    File dir = new File(saveDirectory);
    dir.mkdir();

    FileOutputStream fileOut = new FileOutputStream(saveDirectory +"/" + saveFile);


    fileOut.write(b);
    //fileOut.write(dataBytes, startPos, (endPos - startPos));
    fileOut.flush();
    fileOut.close();

    returnMessage +="<br>File saved as " +saveFile;

    }

    }
    catch(Exception e)
    {
    returnMessage += "<br>An error occurred reading file. Please try again. " + e.getMessage();
    // response.sendRedirect("attachments.jsp?requestId="+requestId+"&returnMessage="+returnMessage);
    saveToDB=false;
    }


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    ..............

    Thats why I love php! :D


Advertisement