PHP Scripts – WordPress Tutorials

This script tells you that with PHP, it is possible to upload files to the server. In this script(PHP File Upload Script), two files are developed. Files are named as:

  1. upload_file_form.php
  2. upload_file_script.php

This tutorial follows a diagram, If one understand this diagram, its easy to develop a script. Because you are clear what you are going to do and in how many steps. So let’s move to the diagram:

Following the diagram, First step is to create a table on your database. Creating a table is very easy in MySQL UI(You can visit my tutorials about MySQL to know how to create table). Here, simply copy this query in SQL box.

To allow user to upload a file from form may prove very useful. Look at the following form to upload a file from the user’s local machine.


upload_file_form.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<html>
<head>
<title>Upload File Form</title>
</head>

<body>

<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="file_upload_script.php">
<h2>PHP Script to Upload File</h2>
Upload File:
<input type="file" name="fileField" id="fileField" />

<input type="submit" name="btnAdd" id="btnAdd" value="Upload" />
</form>
</body>
</html>

Once it is ready, you have to develop PHP script to complete this. Before start developing PHP script, i would like to shed some light on  the procedure that i am going to follow. Here in this tutorial, i have changed the name of the original file, before uploading it to the server, reason is that there may be chance of existance of a file with same name on the server already. The best solution is to change the name of every file, one is going to upload.

upload_file_script.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//=============Configuring Server and Database=======
$host = 'localhost';
$user = 'root';
$password = 'vertrigo';

$database = 'dbsneaker';

$conn = mysql_connect($host,$user,$password) or die('Server Information is not Correct');
mysql_select_db($database,$conn) or die('Database Information is not correct');

//===============End Server Configuration============

if(isset($_POST['btnAdd']))
{
$myFile = $_FILES['fileField']['name']; // Storing name into variable

//====If you want to change the name of the File====
$anyNum = rand(20,500789000); //Will generate a random number between 20and 500789000

$newFileName = $anyNum.$myFile;//===New string is concatenated====
//===Following Function will check if the File already exists========

if (file_exists("upload/".$newFileName))
{
echo $newFileName." already exists. ";
}
//======If file already exists in your Folder, It will return zero and Will not take any action===
//======Otherwise File will be stored in your given directory and Will store its name in Database===
else
{

$query = "insert into tblfileupload(file_name) values ('$newFileName')";

$res = mysql_query($query);

copy($_FILES['fileField']['tmp_name'],'upload/'.$newFileName);
//===Copy File Into your given Directory,copy(Source,Destination)

if($res&gt;0)
{
//====$res will be greater than 0 only when File is uploaded Successfully====:)
echo 'You have Successfulluy Uploaded File';
}
}
}
?&gt;

When the file is uploaded to your server, let us have look at the table on database, as an experience i have uploaded three files with same name and each time i have got different results.

 

 

Random Posts


Categories: Web Development
  • Madiha Akram

    great job ……………

  • Chris Seckler

    Great Tutorial! I like that you kept it simple yet included all the necessary information needed to accomplish a basic file upload! Thank you!

  • Chris Seckler

    Also I forgot to mention, your comments are really helpful so keep that up! Not enough tutorials explain code step-by-step.

    btw, do you know how I can resize images before they upload?

    • http://www.computersneaker.com/ Mudasir Nazir

      Thankyou for your feedback. I really do appreciate those who recommend me some better changes or sometimes they do appreciate my work.
      I will remember you, While i am commenting somewhere on my blog.

  • piya

    Nice post. Very helpful to me. It saved my time.
    Thank you so much….! :)

  • prashant ranjan

    great tutorial. thanks. hope to see more…

  • ROM

    A very great script!

    But I have to suggest that while creating the mysql table, 2 point should be taken in consideration

    1- The most important part while creating the mysql table is that in the file_id field use “AUTO_INCREMENT” because if not used only a single file will be uploaded.

    2-Remove the limit (11) from the file_id column so use just INT instead of INT(11) because this will restrict the user to upload only 11 files.

    Thanks.
    Keep up the good work.

    • http://www.computersneaker.com/ Mudasir Nazir

      You have some awesome skills great, I have also mention these things but i think i just forget to provide details of these, and i am happy that you have done this for me.

  • khushi g.c.

    Great job done here…Awesome work thanks for sharing this script……………….. really this script is very helpful to me

  • http://delcosys.com guru

    Good job, But it would be better if you have done the validation for this file upload. i.e., to limit the file size, file type and empty field indication etc.,

  • adnan

    Nice and simple tutorial as compared to other sites. It will be more nice if you include the file size limit, extension limit, automatically change the name of file if already exist in directory and then allow to store, number of files allowed to upload at a time…

  • Satyam

    Hi…

    A very nice script for newbies in PHP and it will help a lot.

    I would like to ask you to provide the script for renaming the uploaded file with the file ID of mysql.

    Thanks in advance…

    • http://www.computersneaker.com/ Mudasir Nazir

      I would love to write about this and will send you the update about this.

  • disqus_KBlxDNWJSV

    I am getting error in the line 37,
    Warning: copy(upload/420027_3288378921481_121143801_n.jpg653) [function.copy]: failed to open stream: No such file or directory in C:wampwwwNeuprocessUpload.php on line37

    what types of error is this and how to correct, plz i need help ASAP

    • http://www.computersneaker.com/ Mudasir Nazir

      Dear please do have a look at your directory path, Your directory path is not correct.

    • engrmudasirmalik

      Dear you might missed to change the name file in wrong way. have a look at the name

      upload/420027_3288378921481_121143801_n.jpg653

      Where it should be

      upload/420027_3288378921481_121143801_n.jpg

      So try this one again with new file and let me know if it works or not?

  • simon

    Great script, but the “upload”-folder is empty. Nothing is written into the database. Can some help? Thanks

  • Hassan

    It showing me an error at line no 39 and 46? can anyone help me plz