Results 1 to 5 of 5

Thread: Random Directory

  1. #1
    Join Date
    Sep 2010
    Location
    Hi Stalker.
    Posts
    148
    Thanks
    16
    Thanked 3 Times in 3 Posts

    Default Random Directory

    Hello all. I would like to have some PHP code where I can upload a file to a random directory. So say, a user upload foo.gif, instead of uploading it to /uploads I'd rather have it uploaded to a directory made by the user (like /dave/foo.gif). Then, If possible, when the another user types in a new directory (one that Dave made) that's called /dave then to notify that user and for him to rename it to something like /dave1 instead of overwriting Dave's directory. I need it to be in PHP.
    Thanks in advanced

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    What you're describing isn't random. You will need to use move_uploaded_file() and choose a directory. Of course you may need to mkdir() if that directory does not already exist. A log of downloads will be difficult if that is not a PHP file-- you'd need to route incoming requests through PHP or somehow have PHP read the logs from the server. Or you could just make a "downloads" page and log each time someone goes there (assuming there's a direct link on that page).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Sep 2010
    Location
    Hi Stalker.
    Posts
    148
    Thanks
    16
    Thanked 3 Times in 3 Posts

    Default

    Oh ok. Can I have some sample code please?

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    PHP Code:
    <?php

    // This is the root uploads folder from which users can add new folders / files
    // E.g. uploads/dave/foo.gif
    define('UPLOAD_DIR''uploads/');

    // On form submission
    if(isset($_POST['submit'])) {
        
        
    // Trim the user input for the directory
        
    $dir trim($_POST['dir']);
        
        
    // If it's not an empty string
        
    if(!empty($dir)) {
            
            
    // If the files array is not empty
            
    if(!empty($_FILES)) {
            
                
    // Do the following if no errors occurred on the file upload
                
    if(!$_FILES['fileUpload']['error']) {
                    
                    
    // $path will contain the full path to the directory (relative to the current working file)
                                    // For example, if this file is in /admin/, uploads will go to /admin/uploads/DIRECTORY_NAME/

                    
    $path UPLOAD_DIR $dir;

                    
    // Find out if a directory already exists with the same name
                    
    if(file_exists($path)) {
                        
    $error $dir ' already exists in the uploads folder.';
                        
    // You may want to allow the user to just add the file to the pre-existing directory
                        // Instead of returning an error.
                    
    } else {
                        
    // Attempt to make the directory
                        
    if(mkdir($path)) {
                            
                            
    // Get the temp name, and the destination file name
                            
    $src $_FILES['fileUpload']['tmp_name'];
                            
    $dst $_FILES['fileUpload']['name'];
                            
                            
    // Try to upload the file in the newly created folder
                            
    if(move_uploaded_file($src$path '/' $dst)) {
                                
                                echo 
    'Successfully created the new directory and uploaded the file.';
                                
                            } else {
                                
    $error 'Failed to upload the file.';
                            }
                            
                        } else {
                            
    $error 'An error occurred while trying to make the new directory.';
                        }
                    }
                    
                } else {
                
                    
    // Code for catching errors with file uploads here
                
                
    }
                
            }
            
        } else {
        
            
    $error 'Please specify a directory to upload to.';
            
        }
        
    }

    ?>

    <html>

        <body>

            <form method="post" enctype="multipart/form-data">
                
                <?php
                
    if(isset($error))
                    echo 
    '<p>' $error '</p>';
                
    ?>
            
                <input type="file" name="fileUpload">
                
                <p>
                    Directory Name: <input type="text" name="dir">
                </p>
                
                <p>
                    <input type="submit" name="submit" value="Upload File">
                </p>
            </form>

        </body>

    </html>
    I've written the above to help you understand how the file upload process should work. THIS IS IN NO WAY SAFE!. The code above needs a lot more error checking and sanitizing before it's fit for purpose. It should give you a better idea of how to code it though. Since it's just a text box, obviously the directory name could be abused in its current state, but again, this is just to give you a nudge in the right direction.

    Let me know if you have any questions.

    P.S : Make sure whatever you specify as the "UPLOAD_DIR" is already present on your system, as the code does not check for this. Working on the code above, you should already have a folder called "uploads" in the folder where you're running the code from.

  5. The Following User Says Thank You to Schmoopy For This Useful Post:

    [Nicolas] (11-16-2010)

  6. #5
    Join Date
    Sep 2010
    Location
    Hi Stalker.
    Posts
    148
    Thanks
    16
    Thanked 3 Times in 3 Posts

    Default

    @ Schmoopy:
    Thanks for the code dude. This will work wonders for me. Actually I'm needing this for a surprise for everybody, an amateur version though. You guys might like it

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •