Setting up file uploads with php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jungabunga
    New Member
    • Mar 2007
    • 15

    Setting up file uploads with php

    Heya guys, i got this simple script to work and now it doesnt.Its far from completed, i know, i just want it to gogogo so i can expand it to include my preferences and a few hidden fields.
    I can't tell if i screwed up the code or its a server related problem. here goes....


    Code:
     <html> 
    <body>
    <form action="upload.php" method="post" ENCTYPE="multipart/form-data"> 
    <input type="file" name="file" size="30" /> 
    <input type="submit" value="gogogo" /> 
    </form> 
    </body>
    </html>
    [PHP]
    $uploaddir = "/var/www/html/gallery";
    if(is_uploaded_ file($_FILES['file']['tmp_name']))
    {
    move_uploaded_f ile($_FILES['file']['tmp_name'],$uploaddir.'/'. $_FILES['file']['name']);
    };
    echo "Completed" ;

    [/PHP]
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    [PHP]$uploaddir = "/var/www/html/gallery"; [/PHP]
    remove "/" from the beginning of the path and put a "/" to the end check it.
    [PHP]$uploaddir = "var/www/html/gallery/"; [/PHP]

    if your files are listed with gallery folder {all are in same DIR} simply this also work.
    but make sure to assign the write permission to the folder if its remote server.

    [PHP]$uploaddir = "gallery/"; [/PHP]

    Comment

    • jungabunga
      New Member
      • Mar 2007
      • 15

      #3
      thanx for you're input ajax, i tried the different variations but to no avail, (on a remote server btw). it seems such a simple script.

      [directories and permissions]

      / -- var -- 755 / www-- 755 / html -- 755 | -- gallery -- 777
      | -- form.php -- 755
      | -- upload.php -- 755
      I just noticed that my phpinfo() returns upload_tmp_dir = novalue but file_uploads = On. is that correct?

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Originally posted by jungabunga
        thanx for you're input ajax, i tried the different variations but to no avail, (on a remote server btw). it seems such a simple script.

        [directories and permissions]

        / -- var -- 755 / www-- 755 / html -- 755 | -- gallery -- 777
        | -- form.php -- 755
        | -- upload.php -- 755
        I just noticed that my phpinfo() returns upload_tmp_dir = novalue but file_uploads = On. is that correct?
        Put your form.php,upload .php and gallery in side one dir we will say www dir.then set your variable like this.
        [PHP]$uploaddir = "gallery/"; // Note only single "/"[/PHP]

        make sure to set 777 for gallery dir and give it a try again.
        and remove the file size="30" from the form for now.

        Comment

        • jungabunga
          New Member
          • Mar 2007
          • 15

          #5
          [QUOTE=ajaxrand]Put your form.php,upload .php and gallery in side one dir we will say www dirQUOTE]
          thx ajax, but my root.dir is html. i put them i a subfolder, rechecked my permissions, still no go. tried the new path, still no go, tried variations of the path, still no go. its hard to believe it actually worked.

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            [QUOTE=jungabung a]
            Originally posted by ajaxrand
            Put your form.php,upload .php and gallery in side one dir we will say www dirQUOTE]
            thx ajax, but my root.dir is html. i put them i a subfolder, rechecked my permissions, still no go. tried the new path, still no go, tried variations of the path, still no go. its hard to believe it actually worked.
            Sorry I didn't get u.Is it working or Not?

            Comment

            • jungabunga
              New Member
              • Mar 2007
              • 15

              #7
              no sry, no good so far

              Comment

              • ak1dnar
                Recognized Expert Top Contributor
                • Jan 2007
                • 1584

                #8
                Try this one just execute this after creating directory named myfiles with 777.
                put php script and myfiles folder in same place.

                [PHP]<?php
                $target_path = "myfiles/"; //Directory Name to Upload
                if($_POST['upload'])
                {
                $newName = md5(basename($_ FILES['upload_fle']['name']));
                $ext = strrchr(basenam e($_FILES['upload_fle']['name']), ".");
                $fullName = $newName.$ext;

                if(move_uploade d_file($_FILES['upload_fle']['tmp_name'], $target_path.$f ullName))
                {
                echo "The file ". basename( $_FILES['upload_fle']['name']).
                " has been uploaded with this name : ".$fullName ;
                } else
                {
                echo "There was an error uploading the file, please try again!";
                }

                }
                ?>
                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
                <html xmlns="http://www.w3.org/1999/xhtml">
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
                <title>Untitl ed Document</title>
                </head>

                <body>
                <form action="<? echo $PHP_SELF; ?>" method="post" enctype="multip art/form-data" name="frmAlbum" id="frmAlbum">
                <input name="upload_fl e" type="file" id="upload_fle "/>
                <BR>
                <input name="upload" type="submit" value="Upload">
                </form>
                </body>
                </html>[/PHP]

                Comment

                • jungabunga
                  New Member
                  • Mar 2007
                  • 15

                  #9
                  cheers, but i got the

                  There was an error uploading the file, please try again!

                  Comment

                  • ak1dnar
                    Recognized Expert Top Contributor
                    • Jan 2007
                    • 1584

                    #10
                    This script is working in my web server. even your one also working. if it is not working in your end its your bad luck.BTW what type of files you tried to upload.
                    Don't go for massive files, just upload simple text files first of all. there are limitation for huge files from the php level.

                    Comment

                    • jungabunga
                      New Member
                      • Mar 2007
                      • 15

                      #11
                      yeah im only trying small files. i wonder what i did. i might just reinstall web then, thx for the persistance dude.

                      Comment

                      • ak1dnar
                        Recognized Expert Top Contributor
                        • Jan 2007
                        • 1584

                        #12
                        Originally posted by jungabunga
                        yeah im only trying small files. i wonder what i did. i might just reinstall web then, thx for the persistance dude.
                        Don' t give up, give it a try. hope you will get the solution. ;)

                        Comment

                        • jungabunga
                          New Member
                          • Mar 2007
                          • 15

                          #13
                          Originally posted by ajaxrand
                          Don' t give up, give it a try. hope you will get the solution. ;)
                          I musta screwed up the permissions on teh server or something. i resinalled my micro version of fedora and it works fine now :P

                          Comment

                          • ak1dnar
                            Recognized Expert Top Contributor
                            • Jan 2007
                            • 1584

                            #14
                            Originally posted by jungabunga
                            I musta screwed up the permissions on teh server or something. i resinalled my micro version of fedora and it works fine now :P
                            Great work.. :) c u l8r.

                            Comment

                            Working...