Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can you please help me with the code not sure what the problem is, it upload the file but gives a error message on the web browser, try all of the browsers and still the same problem,
index.html code
HTML
<form action="upload.php" method="post" enctype="multipart/form-data"> 
 <input type="file" name="myFile">
 <br>
 <input type="submit" value="Upload">
</form>


upload.php code
define("/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
$i = 0;
$parts = pathinfo($name);
while (file_exists( $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}

// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],

$name);
if (!$success) {
echo "

Unable to save file.

";
exit;
}
// set proper permissions on the new file
chmod("upload_DIR . $name, 0644");
}
it als not uploading it to the folder set in the php code
error message on the web browser
C#
Warning: define() expects at least 2 parameters, 1 given in C:\wamp\www\upload.php on line 2


Call Stack


#

Time

Memory

Function

Location

1 0.0000 133992 {main}( ) ..\upload.php:0 
2 0.0000 134088 define ( ) ..\upload.php:2 


What I have tried:

check the premision on the server and the code,
Posted
Updated 17-Jun-16 14:53pm
Comments
Peter_in_2780 15-Jun-16 3:15am    
Your highlighted code block tells you your (first) problem. The define() on line 2 does not have enough parameters. I can't tell what you are trying to define, but you need to fix this before you go any further.
Sinisa Hajnal 15-Jun-16 4:45am    
As the error message says, you need to correct your define call. The rest looks generally OK.

1 solution

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");
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900