**when I worked on a simple upload form I found something wrong in the result
first my code was :**
$filename= $_FILES ['file']['name'];
$filesize= $_FILES ['file']['size'];
$tmpname= $_FILES ['file']['tmp_size'];
$filetype= $_FILES ['file']['type'];
$folder = "upload/";
if(isset($_POST['do']) and $_POST['do']== 'upload'){
if(empty($filename)){
echo "the file is not exist";
}
else if ($filesize > 2048){
echo " the file is biger than 2 MB";
}else{
echo "the file is uploaded";
move_uploaded_file($tmpname, $folder);
}
}
echo "
<form action='upload.php' method='post' enctype='multipart/form-data'>
file path : <input type='file' name='file'/>
<input type='submit' name='do' value='upload'/>
</form>
"
when the file size is less than 2048 the result always be "the file is bigger than 2048" although I'm sure it's less than 2 MB when i makes it 100000 to see the result what would be the result was "the file is uploaded" but I couldn't find the file in the upload folder anyone can help me ? what is the wrong ?
Why you can't find the file: 2nd argument of move_uploaded_file() needs to be in format 'path/to/file.jpg' so it should be:
$folder = "upload/" . $filename;
move_uploaded_file($tmpname, $folder );
$_FILES['xxx']['size'] is in bytes, not megabytes. As such, unless the file is less than 2KB, it'll be deemed too large.
To check the file is less than 2MB, use:
$filesize > 2097152
Additionally, you're setting $tmpname incorrectly. It should be...
$tmpname = $_FILES['file']['tmp_name'];
The filesize reported by PHP is in bytes, not KB.
Try else if ($filesize > 2097152)
One way to test early is to echo the values of all just before you process them like once you submit the form then do checking values of file size
You should include the filename in the destination string.
move_uploaded_file($tmpname, $folder . $filename);
Also, you are relaying in the browser calculated file size, you should use this instead:
$filesize= filesize($_FILES ['file']['tmp_size']) / 1024; //To be in kilobytes as you expect
Leave the rest as it is!
you can find here a tutorial on how to create simple upload form