tags:

views:

61

answers:

2

I'm trying to gain access to a file on a client computer so that i can then later attach it to an outgoing email (resume.pdf)

Ive found a few clips of code but I'm having trouble getting it to work for me.

the code below seems to demonstrate all the things ill need to gather about my file but i can't quiet seem to get it to work yet.

does anyone have any idea what im doing wrong?

html code:

<input id="resumeup" name="resumeup"  type="file"/>

php code:

   $_FILES = $_POST["resumeup"];

                if ((($_FILES["file"]["type"] == "image/gif")
                || ($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/pjpeg"))
                && ($_FILES["file"]["size"] < 20000))
                {
                        if ($_FILES["file"]["error"] > 0)
                        {
                                echo "Error: " . $_FILES["file"]["error"] . "<br />";
                        }
                        else
                        {
                                echo "Upload: " . $_FILES["file"]["name"] . "<br />";
                                echo "Type: " . $_FILES["file"]["type"] . "<br />";
                                echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
                                echo "Stored in: " . $_FILES["file"]["tmp_name"];
                        }
                }
+1  A: 

For starters, don't overwrite $_FILES with $_POST; the former is created by parsing the multi-part form data in the request.

Second, make sure your form tag has said multi-part form encoding:

<form action="..." method="post" enctype="multipart/form-data">
Rob
what does that get me?
Crash893
Er, well what it "gets you" is your $_FILES array, intact as PHP created it (which was done while the request was being parsed), and not being overwritten by something else, which is what you're doing. The encoding type on the form is necessary to ensure that the uploaded file is actually submitted with the request in the correct manner. From here on, you should be able to access the temporary file through $_FILES['field']['tmp_name'], although as Lachlan points out below, if you want to do anything with it after that script run, you'll need to move it elsewhere.
Rob
+1  A: 

The uploaded file will only be stored temporarily, you will need to use move_uploaded_file() to relocate the file to a new location so that you can access it in the future.

You can read more on the PHP:

http://www.php.net/manual/en/features.file-upload.post-method.php

Lachlan McDonald
Im very new to php and web programing in general. I don't want to keep the file i just want to attach it to an email. Im not sure how to upload a file to start with so moving it is putting the cart before the horse so to speak.
Crash893