the main error is define has 2 parameters.U can write as define(UPLOAD_DIR,"/var/www/upload/");
function sp_random_string($len = 6) {
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars);
$output = "";
for ($i = 0; $i < $len; $i++) {
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
define(upload_DIR,"/var/www/upload/");
if (!empty($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
echo "
An error occurred.
";
exit;
}
// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
// don't overwrite an existing file
//THIS PARTS ,WE CAN WRITE AS THIS
/*
$i = 0;
$parts = pathinfo($name);
while (file_exists( $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
*/
$parts = pathinfo($name);
$name = time().$parts["filename"] . "-" . sp_random_string(9) . "." . $parts["extension"];
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
upload_DIR.$name);
if (!$success) {
echo "
Unable to save file.
";
exit;
}
// set proper permissions on the new file
chmod(upload_DIR . $name, "0644");
}
Updated 17-Jun-16 15:05pm
v2