PHP Script to eat:
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:- upload_file_form.php
- upload_file_script.php
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.
Code: upload_file_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html xmlns= "http://www.w3.org/1999/xhtml" > <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> <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> |
Code: 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
47
| <?php //=============Configuring Server and Database======= $host = 'localhost' ; $user = 'root' ; $password = 'vertrigo' ; //=============Data Base Information================= $database = 'dbsneaker' ; $conn = mysql_connect( $host , $user , $password ) or die ( 'Server Information is not Correct' ); //Establish Connection with Server 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 >0) { //====$res will be greater than 0 only when File is uploaded Successfully====:) echo 'You have Successfulluy Uploaded File' ; } } } ?> |
0 komentar:
Posting Komentar