Results 1 to 13 of 13

Thread: Upload a file via PHP

  1. #1

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935

    Upload a file via PHP

    How might I go about such a task?

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    the process is fairly simple, you need the HTML obviously:

    Code:
    <form enc-type="multipart/form-data" action="upload.php" method="post"
        <input type="hidden" name="MAX_FILE_SIZE" value="1000">
        Upload: <input type="file" name="userfile">
        <input type="submit" value="Upload File">
    </form>
    when the file is uploaded, it will go into the temp directory on the server, all the php script has to do is move it to where you want it. Just use php's copy function to move it http://www.php.net/manual/en/function.copy.php , since if you don't move the temp file, it will get deleted when the script fnishes.

    The value in $userfile is the location of the temp file
    $userfile_name is the filename
    $userfile_size is the size (bytes)
    $userfile_type is the mime type

  3. #3

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Nifty, I'll try it out tomorrow considering I don't want to screw up my host and I don't have PHP running because I just reformatted

  4. #4

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Arrrrrrrg:
    Uploading toptitle.gif (16578 bytes)...
    Warning: Unable to create '/usr/home/m/r/mrskramercom/public_html/students/talabac/var/tmp/phpIewkV3': No such file or directory in /usr/home/m/r/mrskramercom/public_html/students/uploadfile.php on line 17
    Failed to upload!
    My code:
    <html>
    <head>
    <title>MrsKramer.com &gt; Upload File</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF" text="#000000">
    <?
    if (isset($userfile))
    {
    echo '<font face="Verdana, Arial, Helvetica, sans-serif" size="2">';
    echo "Uploading $userfile_name ($userfile_size bytes)...";
    if (!file_exists($studentname))
    {
    mkdir($studentname, 0777);
    }
    if (!copy($userfile, "/usr/home/m/r/mrskramercom/public_html/students/$studentname$userfile"))
    {
    echo '<b>Failed to upload!</b>';
    }
    else
    {
    echo '<b>Upload successful</b> (<a href="uploadfile.php">Upload another file</a>)';
    }
    echo '</body>';
    echo '</html>';
    exit;
    }
    ?>
    <FORM ENCTYPE="multipart/form-data" ACTION="uploadfile.php" METHOD="POST">
    <table border="0" cellspacing="0" cellpadding="5">
    <tr>
    <td bgcolor="#EEEEEE"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>Name:</b>
    <? require('studentlistdropdown.php'); ?>
    </font> </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td bgcolor="#EEEEEE">
    <p><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b>File
    to upload:</b></font></p>
    <ul>
    <li><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Only
    allowed types are GIF, JPG, JPEG, PNG, CLASS, HTM, and HTML)</font></li>
    <li><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Maximum
    file size is 10,000 bytes (about 10 KB)</font></li>
    <li><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Files
    that are not directly referenced (i.e., for storage only) may be deleted
    as space on the server is limited)</font></li>
    <li><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Accessing
    http://www.mrskramer.com/<i>lastname</i>/ will open http://www.mrskramer.com/<i>lastname</i>/index.html.</font></li>
    </ul>
    <p><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
    <input type="File" name="userfile" size="30" maxlength="255"></font></p>
    </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td bgcolor="#EEEEEE">
    <p><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#FF0000">Warning!
    Any existing file with the same name will be overwritten!</font><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><br>
    Click the Upload button only once.</font></p>
    <p>
    <input type="submit" value="Upload">
    </p>
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    Why? WHY!? WHYYYYYYYYYYYYYY! BTW, I chmod'ed all directories to 777 and it's STILL doing this. http://www.mrskramer.com/students/uploadfile.php

  5. #5
    scoutt
    Guest
    if (!copy($userfile, "/usr/home/m/r/mrskramercom/public_html/students/$studentname$userfile"))


    should be

    if (!copy($userfile, "/usr/home/m/r/mrskramercom/public_html/students/$studentname/$userfile"))

  6. #6

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    When I do that, I get two /s in a row.

  7. #7
    scoutt
    Guest
    just noticed,
    why are you using 2 $userfile? try this

    if (!copy($userfile, "/usr/home/m/r/mrskramercom/public_html/students/$studentname/$userfile_name"))

  8. #8

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Cheers, it works

  9. #9
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609
    *** is 'chmod' ???

  10. #10
    scoutt
    Guest
    that sets the permissions to a file so you can read or write or execute. for all users, owners, and groups

    777 is full access for all everybody
    755 is where you want it be most of the time.
    444 is read only.

  11. #11
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609
    thanx scoutt. where would i change these settings ?? i know something like that is accessable through some FTP browser i have though

  12. #12
    scoutt
    Guest
    it all depends on what ftp program you use. I use ws_ftppro and if I right click on a file on the server it gives me the choice of chmod. then it opens another window where I can check and uncheck boxes to get the desired result.

  13. #13
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609
    lol, sorry scoutt, i am @ home now and thats the same app i use (ws_ftp_pro) i'll check it out. thanx m8

Posting Permissions

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



Click Here to Expand Forum to Full Width