SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Uploading files

  1. #1
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Uploading files

    How can you upload a file?

    Thanks,
    ~someonewhois

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    simple question; simple answer: www.php.net/manual/en/features.file-upload.php

    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't understand that post vars thing... Any help on it?

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's a sample file that should work for you. it should save the uploaded file in the same directory as the script (make sure it's writable by PHP), with the same name as it had when it was uploaded.

    PHP Code:
    <?php

    if (isset($_FILES['test']))
    {
        if (
    $_FILES['test']['tmp_name'] == '' || $_FILES['test']['tmp_name'] == 'none')
        {
            die(
    'No file selected.');
        }

        if (
    move_uploaded_file($_FILES['test']['tmp_name'], "./{$_FILES[test][name]}"))
        {
            echo 
    'File successfully saved to: 'dirname($_SERVER['SCRIPT_FILENAME']), "/{$_FILES[test][name]}";
        }
        else
        {
            echo 
    'File upload failed!';
        }
    }
    else
    {
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
        Select a file to upload: <input type="file" name="test" /><br />
        <input type="submit" value="Submit" />
    </form>
    <?php
    }

    ?>
    if your version of PHP is less than 4.1.0, change $_FILES to $HTTP_POST_FILES and $_SERVER to $HTTP_SERVER_VARS.

  5. #5
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, I'll try this!

  6. #6
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IT WORKED!



    Thank you very much,
    ~someonewhois

  7. #7
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I were to make something to display these, would I make a mysql table to store the filename?

    What would I store? The full path? What variable?

    And hwo could I make it go into a different folder?

    Would I change the upload and move path?

    I want it so that uploader.php upload to /uploads/ but it's at main root.

    Thanks,
    ~someonewhois

  8. #8
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your help, please continue!

    (This is equivilent to *bump* )

    Thanks,
    ~someonewhois

  9. #9
    SitePoint Enthusiast z00om's Avatar
    Join Date
    Dec 2001
    Location
    California
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats pretty easy, thankfully!

    Find this line:
    PHP Code:
        if (move_uploaded_file($_FILES['test']['tmp_name'], "./{$_FILES[test][name]}")) 
    specifically,
    PHP Code:
    move_uploaded_file($_FILES['test']['tmp_name'], "./{$_FILES[test][name]}
    more specifically:
    PHP Code:
    "./{$_FILES[test][name]}
    now you see the "./"? that says "current directory". If you wish to change it to uploads, just add uploads/!

    PHP Code:
    "./uploads/{$_FILES[test][name]}
    full:
    PHP Code:
        if (move_uploaded_file($_FILES['test']['tmp_name'], "./uploads/{$_FILES[test][name]}")) 
    hope that helps with that

    Now, when you are talking about listing all the files in a directory? Well, theirs a simplier way to do it without using mysql...

    PHP Code:
    <?php

    if ( $dir = @opendir("./uploads") )
    {
        while ( (
    $file readdir($dir)) !== false )
        {
            if ( 
    $file != "." && $file != ".." )
            {
                echo 
    $file "<br />\n";
            }
        }  
    closedir($dir);
    }
    else
    {
        echo 
    "Could not open directory!";
    }
    ?>
    that will simply list every file in a directory.
    Last edited by z00om; Apr 30, 2002 at 19:07.
    [z00om : home]
    [z00om@hotmail.com]
    [z00om : aim]
    [77968493 : icq]

  10. #10
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome, thank you very much!

    Thanks,
    ~someonewhois


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
  •