Welcome, guest | Sign In | My Account | Store | Cart

This is a simple code for file uploading in php

PHP, 70 lines
 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<pre>
<form enctype="multipart/form-data" action="index.php" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
     <input type="submit" value="Send File" onClick="return checkfile(this.form);" />
</form>

write this code in the head section of your php in which you have written the above code.

<html>
<head>
<title>
</title>
<script langauge="javascript" >
function checkfile(form)
{

if (form.userfile.value == "")
{
alert('Please Select a File To Upload');

return false;
}
else
{
return true;
}


}
</script>

</head>


Now write this code in the action script i.e in my case it is index.php

$uploaddir = '/home/rohit/upload/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);


if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{

  echo "File is valid, and was successfully uploaded.\n";

}
 else

 {
   echo "file upload error\n";
}



Thats all
do mail me ur feedback at rajdsouza@yahoo.com










</pre>
Created by rohit d'souza on Fri, 5 May 2006 (MIT)
PHP recipes (51)
rohit d'souza's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks