Results 1 to 8 of 8

Thread: Upload Files

  1. #1

    * Upload Files

    Does anyone know a quick and easy way to upload files to my server via PHP? I am creating a PHP backend for my site and I want to be able to upload files directly from there if that helps. Thanks a lot.

  2. #2
    Join Date
    Aug 2002
    Assume that the file field in your form is called "file", make sure in your <form> tag you include:


    Then in your PHP script:

    PHP Code:
    $target "./upload_dir/" $file_name;

    copy($file,$target)) {

    "File Copied Successfully.");

    else {

    "File could not be copied.");

    If you want to include checking of the size of the file, stored in the variable: $file_size will be the size of the file (in bytes) so you can do error checking that way. Moreover, if necessary you can check the MIME type of the file which is stored in the variable $file_type.

    This is because when you upload a file, PHP will automatically created:


    Which are pretty self explanatory. $file is the link to the temporary file which in the code you copy to the desired location.

    Hope this helps.
    Mark Carruth

  3. #3
    Little note - it is adviced to use move_uploaded_file() instead of copy(), as there are some security issues. Although if you're using old version of php, you do not have any choice.
    It's kind of fun to do the impossible.

  4. #4
    Thanks. Couple Questions
    That 'upload_dir' in the $target definition... do I need the absolute path there? I am not positive the path that it is at on the server I am hosting with. Is there a variable that lets me use relative paths instead of absolute so that I can move this between systems? Thanks again.

  5. #5
    Join Date
    Aug 2002
    You can use absolute path but as you don't know the folder, you can use Server map path variables to get the HTTP_REFERER for knowing the current path or the MAP_PATH info.

  6. #6
    I just figured out what was happening. Stupif mistake on my part. Thanks a lot for the help! works like a charm as soon as I set the permissions correctly on the folder. Thanks again

  7. #7
    Join Date
    Jul 2001
    New York
    Sounds like you have everything under control but for those who may not want to write their own, here's a couple "uploaders" that work nicely.


  8. #8
    Join Date
    Aug 2002
    Baltimore, Maryland
    remember php and apache server limits the max file size on a http or https file transfer..

Posting Permissions

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