Working with Files in PHP

Hi Experts,

Can any one help me get started to work with files in PHP?

For example, in order to upload a file, I think I need <input type="file">? I also think I need to add something to my <form> tag for this?

Finally, once submitted, how can I...
if folder/sub-folder !exist {
  create folder/sub-folder
}
save file as folder/sub-folder/filename

Open in new window


Thank you!
APD TorontoAsked:
Who is Participating?
 
gr8gonzoConnect With a Mentor ConsultantCommented:
1. Your form needs to use the "multipart/form-data" encoding type and the "POST" method:
<form enctype="multipart/form-data" method="POST" action="your_upload_processing_script.php">
</form>

Regular "POST" data just sends the form fields in a "field1=value1&field2=value2&etc...." type of format. But when you want to send file data, you want to use an encoding that will properly keep the field/value data AND also have a way to send the raw file data without it screwing up the rest of your POST data. You want the raw data to be treated separately. That "multipart" encoding means "there are multiple parts to what I'm sending to the server - in this case ("form-data"), one part is the regular form data and the other part is going to be the file data".

2. You also need the <input type="file" name="somethinghere"> within you form. This lets the browser pick a file from the local computer so it knows what file to send.

3. When you submit the form, the browser will encode the data properly (putting the POST data in one part and the raw file data in another part), and then PHP gets it. It takes the raw file data part and looks at the NAME attribute of the <input type="file" name="somethinghere"> tag, and then uses that to identify the file inside the resulting $_FILES array that gets created.

So in your your_upload_processing_script.php script, you can access a little bit of info about the uploaded file via the $_FILES array, in this example, it would be $_FILES["somethinghere"] that would have that info (like the original filename, the file size, etc...).

4. One of the pieces of info in the $_FILES array is the place where PHP has temporarily stored the uploaded file. It's a security risk if PHP were to upload files directly to one specific spot every time, so PHP instead puts it into a temporary folder with a temporary/random name and then tells you what that path is in the $_FILES array. It's up to you to then use the move_uploaded_file() method to rename and move the temporary file into its final destination on the server.

You can optionally access the temporary file first to see if it looks okay before you use move_uploaded_file(), just in case. For example, maybe you want to have PHP execute a virus scanner on the uploaded file and delete any infected files instead of accepting the files permanently.

That's the basic gist, and Dave's already provided the link to the PHP documentation about uploads so you can get the full scoop on all the other features.
0
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
The PHP man pages have a detailed explanation of how to upload files.  There are several steps and requirements to getting it right and working.  Please read and come back with any questions you have.

http://php.net/manual/en/features.file-upload.php
0
 
APD TorontoAuthor Commented:
I forgot to include this in my original question, but is there any way I can do this without refreshing my page,, as post will, like with AJAX? or in this case I'll need to use an iframe?
0
 
Dave BaldwinFixer of ProblemsCommented:
There probably is.  But I would get it working as a standard form page first.  Troubleshooting AJAX uploads can be difficult.
0
 
gr8gonzoConsultantCommented:
Sure. You can do it via AJAX on newer browsers that support the FormData object. I'm guessing you're probably using a library like jQuery or something to do your AJAX calls, and the documentation will indicate whether it's possible with that framework or not.

That said, it's probably SAFER to do it via an iframe, though. You'll find more community support for that methodology since it's been a fairly established method for a while now and it'll have more browser support for a while.

Like Dave said above, get it working on a standard page first. Then you can work on changing the client-side behavior to match your needs. (When in doubt, follow Dave's advice.)
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.