0

**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 ?

1
  • file path : <input type='file' name='file'/> <input type='submit' name='do' value='upload'/> </form> " Commented Dec 22, 2010 at 19:30

6 Answers 6

4

$_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']; 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks alot :) the wrong was (tmp_name) I wrote it wrong . thanks for your help
@beginer in php - No worries. Incidentally, if you mark your preferred answer as accepted (click on the grey tick to the left of the answer) both you and the person who wrote the answer we receive reputation points. (The effective "currency" of Stack Overflow.)
1

The filesize reported by PHP is in bytes, not KB.

Try else if ($filesize > 2097152)

Comments

0

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

Comments

0

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!

2 Comments

Second part is wrong. filesize() needs the filename as input, and should be ` * 1024 ` not ` / 1024 `
Oh really!? Try learn basic mathmatics before converting bytes to kilobytes and commenting others! You shall review this before posting random comments: php.net/manual/en/features.file-upload.post-method.php
0

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 ); 

Comments

0

you can find here a tutorial on how to create simple upload form

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.