Results 1 to 11 of 11

Thread: Upload File PHP

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Upload File PHP

    Hello i am using the following code:

    announcement.html:
    Code:
    <form enctype="multipart/form-data" action="uploader.php" method="POST"^gt;
    <input type="hidden" name="MAX_FILE_SIZE" value="1000" />
    Upload File: <input name="uploadedfile" type="file" />
    <input type="submit" value="Upload File" />
    </form>
    uploader.php:
    Code:
    $target_path = "uploads/";
    
    $target_path = $target_path . basename( 
    
    $_FILES['uploadedfile']['name']);
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], 
    
    $target_path)) {
    echo “The file “. basename( 
    
    $_FILES['uploadedfile']['name']). ” has been uploaded”;
    } else{
    echo “There was an error uploading the file, please try 
    
    again!”;
    }
    This is not currently on a server, i will upload it later. At the moment i am running this off my computer. When i click upload after i select my file internet explorer prompts me to download the file uploader.php .

    I want the code in that file to take action i don't want to download it. What is wrong?
    Last edited by noahssite; Jan 21st, 2009 at 07:04 PM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: PHP wont work...

    you need PHP running on a server of somekind.... even if it's your local machine... for quick and easy installation I suggest XAMPP (or LAMPP for Linux, what ever boats your float).
    http://www.apachefriends.org/en/xampp.html

    I've got it installed onto a flash drive so I can take my test server where ever I go.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: PHP wont work...

    thanks also with the code of uploader.php changed to:
    Code:
    <?php
    $target_path = "uploads/";
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
    ?>
    When i try to upload an image file or at least i havnt tried any other type, it always displays the error, There was an error uploading the file, please try again!...

    What is wrong? Is there something off in the code?

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Upload File PHP

    Do some hosting providers disable the ability to upload?

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Upload File PHP

    Not that I am aware of... I've never had a problem with my host at least.

    Unfortunately I've never tried to upload a file via PHP, so I'm a little out of my element at this point... sorry.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Upload File PHP

    Then it may just be something wrong with my code?
    How can that be since i got it off a php tutorial...

  7. #7
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: Upload File PHP

    I know this may sound like a dumb question but is the file you are trying to upload over the max file size that you specified? Also, not sure why but on you form line I se ^gt; instead of > it shoud be:

    Code:
    <form enctype="multipart/form-data" action="uploader.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000" />
    Upload File: <input name="uploadedfile" type="file" />
    <input type="submit" value="Upload File" />
    </form>
    If I helped you please rate me.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Upload File PHP

    I was actually changing my code a bit and i got my code from different sources. Here is my latest code i am using i am not sure if it is any different then the code i have shown above.

    announcement.html
    Code:
    <form enctype="multipart/form-data" action="uploader.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Choose a file to upload: <input name="uploadedfile" type="file" /><br />
    <input type="submit" value="Upload File" />
    </form>
    uploader.php
    Code:
    <?php
    $target_path = "uploads/";
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
    ?>
    EDIT:
    I think the uploading is fine but not the move_uploaded_file part...
    Since i have been testing the feature with pictures and text files but when i tried a flash file it took a while longer untill the error message appeared..

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Upload File PHP

    I even tried the code on this page (tutorial):
    http://www.w3schools.com/PHP/php_file_upload.asp

    It is fine with puting the file in the temp folder as i thought. Though there is an issue with uploading.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: Upload File PHP

    Maybe it does come back to a server issue? i will see if my server doesn't support uploading... i don't understand why it wouldn't.

    EDIT:
    To prevent posting 4 times in a row im just adding another question into this post.

    Could it be that i have to enable directory permissions on my hosting provider?
    Last edited by noahssite; Jan 22nd, 2009 at 10:01 PM.

  11. #11
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Upload File PHP

    yes, your script will need to have write permissions for whatever folder you're moving files to.

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