Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to upload a file in Joomla?

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 521
    Comment on it

    Hello friends,

    Today we will learn how to upload a file in Joomla. In Joomla, upload() function is used to upload a file from source to destination. This function is similar to PHP move_uploaded_file() function.

    Syntax:

    1. JFile::upload($src, $dest);

    In above syntax, JFile is the class in which upload() function is defined. It is a pre-defined function. $src is the source of the file and $dest is the destination where we will upload the file.

    Let's take an example to understand this.

    Suppose we have a form with a input field of type file as follows:

    1. <form name="upload" method="post" enctype="multipart/form-data">
    2. <input type="file" name="fileupload" />
    3. <input type="submit" />
    4. </form>

    Now we will use the following code to upload the file:

    1. <?php
    2. $file = JRequest::getVar('file_upload', null, 'files', 'array');
    3.  
    4. //Import filesystem libraries. It is not necessary
    5. jimport('joomla.filesystem.file');
    6.  
    7. //Clean up filename to get rid of strange characters like spaces etc
    8. $filename = JFile::makeSafe($file['name']);
    9.  
    10. //Set up the source and destination of the file
    11. $src = $file['tmp_name'];
    12. $dest = getcwd() . "/uploads/" . $filename;
    13.  
    14. if ( JFile::upload($src, $dest) ) {
    15.     echo "uploaded successfully"; //or we can redirect to a page of our choice
    16. } else {
    17.     echo "not uploaded"; // or we can redirect and throw an error message
    18. }
    19. ?>

    In above example, the file will upload to the uploads folder in the server. Also please check the permission of the folder otherwise the file will not uploaded.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: