Uploading files in PHP

Page 3 of 3
  1. Introduction
  2. The Code
  3. Handling the upload

Handling the upload

You can check if the file has been uploaded to the server using different attributes provided by the PHP. For example, if you dont want to allow your users to upload the same file again and again, you can put a restriction on the user to upload new files only. We can set limit on the size of the file to be uploaded by adding an <input> element with the NAME attribute set to MAX_FILE_SIZE and the VALUE to the upper limit. I haven't demonstrated this in my sample code but here the piece of code for your reference:

<input type=hidden name=MAX_FILE_SIZE value=1024>
<imput type=file name=userfile>

Remember, the hidden <input> tag should precede the file <input> tag.

Place a check before displaying a message, use the empty() function to check if $userfile variable contains something or not, if its empty then display the message telling the user that the file hasn't been uploaded otherwise display the message that the file has been uploaded successfully. And that's it!

You might also like...

Comments

About the author

S.S. Ahmed United States

S.S. Ahmed is a senior IT Professional and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If debugging is the process of removing software bugs, then programming must be the process of putting them in.” - Edsger Dijkstra