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
|
<?php define("UPLOAD_DIR", "student_photo/");
$msg = ""; $error = ""; if (!empty($_FILES["browse"])) { $myFile = $_FILES["browse"];
if ($myFile["error"] !== UPLOAD_ERR_OK) { $error="<p>An error occurred.</p>"; exit; } // ensure a safe filename $name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]); // don't overwrite an existing file $i = 0; $parts = pathinfo($name); while (file_exists(UPLOAD_DIR . $name)) { $i++; $name = $parts["filename"] . "-" . $i . "." . $parts["extension"]; } // preserve file from temporary directory $success = move_uploaded_file($myFile["tmp_name"], UPLOAD_DIR . $name); if (!$success) { $msg="<p>Unable to save file.</p>"; exit; } else{ $msg=$name; } // set proper permissions on the new file chmod(UPLOAD_DIR . $name, 0644); } echo "{"; echo "error: '" . $error . "',\n"; echo "msg: '" . $msg . "'\n"; echo "}"; ?>
|