File Upload Error (Apache & PHP)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chunk1978
    New Member
    • Jan 2007
    • 224

    #1

    File Upload Error (Apache & PHP)

    hi there...

    i have the following codes (HTML and PHP) on my Apache Localhost:

    HTML: titled "Form.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>
    PHP: titled "uploader.p hp"
    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!";
    }
    ?>
    i've also created a folder called "uploads" in the same directory as the .html and .php files.

    the problem i'm having is that i always get the message:

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

    i don't know what i'm doing wrong... this is the most basic php upload script... does Apache not allow files to be written to the localhost folders? am i suppose to change anything in my php.ini file? the file i attempted to upload is really small, and well below the php.ini's max file/memory size limits...

    anyone?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Hve you changed the permissions on the directory uploads to allow files to be written to it. If you haven't the move can fail because the default user does not have permissions to write in that directory.

    You should be able to check and alter the directory permissions via your ftp client, set them to 777 (assuming that you have a Linux server).

    Comment

    • chunk1978
      New Member
      • Jan 2007
      • 224

      #3
      Originally posted by Banfa
      Hve you changed the permissions on the directory uploads to allow files to be written to it. If you haven't the move can fail because the default user does not have permissions to write in that directory.

      You should be able to check and alter the directory permissions via your ftp client, set them to 777 (assuming that you have a Linux server).
      hi banfa... thanks for replying... i included the $_Files['error'] code into the script and it gave me a "0" error, which means that everything was fine and there is no error... but it says "0 - There was an error uploading the file, please try again!"... so there's defiantly something crazy going on...

      i don't understand how i can change directory permissions on my localhost (my mac's "sites" folder) using FTP... i don't use FTP to create folders or move files... i just drag and drop, or create a file or folder within the "sites" folder in Mac OS X. but maybe there's still a way to have permissions with a localhost running Apache?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I have no idea how to change directory permissions on a Mac running OS X, although I do believe that OS X is based on Linux and therefore it is possible that the same permissions structure is in place.

        You drag and drop to create files and folders but you are able to do it because you as a user have permissions to do it. The default user used by Apache when running may not have these permissions.

        Not your if statement that causes the message "0 - There was an error uploading the file, please try again!" to be output does not test the upload, it tests the ability to copy the file once it is uploaded (the error message is misleading).

        The move_uploaded_f ile documentation suggests that as long as the first parameter is a valid file name to a file uploaded to php via a post then if the function fails it will output a warning message. Do you have warnings switched on?

        Finally you say you created a uploads directory, are you sure the capitalisation is correct?

        Comment

        • chunk1978
          New Member
          • Jan 2007
          • 224

          #5
          Originally posted by Banfa
          I have no idea how to change directory permissions on a Mac running OS X, although I do believe that OS X is based on Linux and therefore it is possible that the same permissions structure is in place.

          You drag and drop to create files and folders but you are able to do it because you as a user have permissions to do it. The default user used by Apache when running may not have these permissions.

          Not your if statement that causes the message "0 - There was an error uploading the file, please try again!" to be output does not test the upload, it tests the ability to copy the file once it is uploaded (the error message is misleading).

          The move_uploaded_f ile documentation suggests that as long as the first parameter is a valid file name to a file uploaded to php via a post then if the function fails it will output a warning message. Do you have warnings switched on?

          Finally you say you created a uploads directory, are you sure the capitalisation is correct?
          hi again... about the naming of the folder, i've tried several folders and the names of the folder is the same as what's written in the script.

          Mac OS X is unix based, so it's for sure the same procedure of granting permissions... but i just don't have any idea how to grant the 777 permission... am i suppose to enter some commands in the terminal window?

          Comment

          • theorange
            New Member
            • May 2007
            • 2

            #6
            to change the permissions in OSX open terminal and type chmod 777 followed by the path to the directory or files you are changing.

            eg: chmod 777 sites/test1

            if you then type "cd" (without the "") followed by the directory containing the files you are modifying (eg: cd sites) then type "ls -al" you will be given a list of the files in that diectory with the permissions listed on the left hand side. 777 will grant all access (read, write, execute) to the owner, groups, and other users. To grant the reqired permissions that you want i sugest that you type "man chmod" which will open a manual file that explains the comand chmod further. this also has a description of the numerical combinations needed to get the desired result.

            i hope this helps! :P

            Comment

            Working...