Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Image upload... good code example

    does anoyone here have some good code for uploading files to a server as I have search and found many confusing ones. So does have any commented code? whcih they can post so i can learn from



    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <html>
    <body>
    <?php

    $file_dir 
    "c:\www\uploads"// directory where you want file to be uploaded to
    $file_url "http://localhost/uploads"//same directory, but as link

    foreach( $HTTP_POST_FILES as $file_name => $file_array ) {
        if (
    substr($file_array['type'], 05)=="image") {
            echo 
    "path: ".$file_array['tmp_name']."<br>\n";
            echo 
    "name: ".$file_array['name']."<br>\n";
            echo 
    "type: ".$file_array['type']."<br>\n";
            echo 
    "size: ".$file_array['size']."<br>\n";
            if ( 
    is_uploaded_file$file_array['tmp_name'] ) ) {
                
    move_uploaded_file$file_array['tmp_name'], "$file_dir/".$file_array['name']);
                echo 
    "<a href=\"$file_url/$file_name\">".$file_array['name']."</a>\n\n";
            }
        } else {
            echo 
    "File is not an image.";
        }
    }
    ?>
    <form enctype="multipart/form-data" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="3000000000000">
    <input name="fupload" type="file"><br>
    <input type="submit" value="Send File">
    </form>
    </body>
    </html>

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks so far, but

    i get a parse error for the following line:
    $file_dir_=_"c:wwwuploads";_// directory where you want file to be uploaded to

    yet i have a directory on my C: called wwwuploads

    does anyone know the solution? do you not need to place slashes in directory paths?
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    It should be
    PHP Code:
    $file_dir "c:\\wwwuploads"// directory where you want file to be uploaded to 
    not
    PHP Code:
    $file_dir_=_"c:wwwuploads";_// directory where you want file to be uploaded to 

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macintosh

    it still doesn`t seem to work on my PC running apache.

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If I was to use this with .ZIP files could I get away with changing this part
    Code:
        if (substr($file_array['type'], 0, 5)=="image") {
    To this
    Code:
        if (substr($file_array['type'], 0, 5)==".zip") {
    If not how would I do it?

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For zips try:

    PHP Code:
    $ftype $HTTP_POST_FILES['fupload']['name'];
    $ftype substr($ftypestrlen($ftype)-4strlen($ftype));
    if ( 
    $ftype == ".zip" ) { 

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by scroots
    it still doesn`t seem to work on my PC running apache.

    scroots
    Do you have PHP installed? Do other PHP scripts work?

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have php installed and enabled as the hello world script works


    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    my current code is:
    PHP Code:
    <html>

    <body>

    <?php

    $file_dir_
    =_"C:\phpdev\www\upload\";_// directory where you want file to be uploaded to

    $file_url_=_"
    http://localhost:5632/uploads";_//same directory, but as link



    foreach(_$HTTP_POST_FILES_as_$file_name_=>_$file_array_)_{

    ____if_(substr($file_array['type'],_0,_5)=="image")_{

    ________echo_"path: ".$file_array['tmp_name']."<br>\n";

    ________echo_"name: ".$file_array['name']."<br>\n";

    ________echo_"type: ".$file_array['type']."<br>\n";

    ________echo_"size: ".$file_array['size']."<br>\n";

    ________if_(_is_uploaded_file(_$file_array['tmp_name']_)_)_{

    ____________move_uploaded_file(_$file_array['tmp_name'],_"$file_dir/".$file_array['name']);

    ____________echo_"<a_href=\"$file_url/$file_name\">".$file_array['name']."</a>\n\n";

    ________}

    ____}_else_{

    ________echo_"File is not an image.";

    ____}

    }

    ?>

    <form enctype="multipart/form-data" method="post">

    <input type="hidden" name="MAX_FILE_SIZE" value="3000000000000">

    <input name="fupload" type="file"><br>

    <input type="submit" value="Send File">

    </form>

    </body>

    </html>
    can anyone help?

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how come loads of ______ appear in my code on this board? is it because i use editpad light?

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Location
    United Kingdom Confused: Often
    Posts
    859
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmmm... you might want to read this tutorial:
    http://www.developer.com/lang/php/article.php/1456041
    redhead

  • #13
    Senior Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    1,137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    redhead thanks for the tutorial i will have a read and a fiddle.

    scroots
    Spammers next time you spam me consider the implications:
    (1) that you will be persuaded by me(in a legitimate mannor)
    (2)It is worthless to you, when i have finished


  •  

    Posting Permissions

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