File Upload Problem in PHP

rothana
10:20:10
Hi,

I'm having a code in PHP to upload the file. I set condition that if the file is bigger than 1MB, it would display message. But I can still upload the bigger file (4MB) with successful message and that big file does not go to the server.

Here is my code:

<?php

// Upload and Rename File

if (isset($_POST['submit'])) {

$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name


if ($_FILES["file"]["size"] > 1000000) {

//die ("ERROR: Large File Size");


echo "<br><p class='style6' align='center'>Your submission FAILED!</p>";
echo "<p class='style5' align='center'>Please upload the file which does not exceed 1MB.</p>";
echo "<p class='style5' align='center'>OR please read the instruction in the How to Apply carefully and submit it again.</p>";
echo "<p align='center'><a href='javascript:history.back()'>Back to Previous Page</a></p>";

} else {

// rename file
$newfilename = md5($file_basename) . $file_ext;
$random_digit = rand(0000,9999);
$new_file_name = $random_digit.$newfilename;

move_uploaded_file($_FILES["file"]["tmp_name"], "SharedFolder/" . $new_file_name);
echo "File uploaded successfully.";
}

}

?>

Please help me with this.

Many thanks in advance,
Rothana
smo1234
10-20-2010
move_uploaded_file($_FILES["file"]["tmp_name"], "SharedFolder/" . $new_file_name);
echo "File uploaded successfully.";

Try this line by keeping inside a if condition like this
if(move_uploaded_file($_FILES["file"]["tmp_name"], "SharedFolder/" . $new_file_name){
echo "File uploaded successfully.";
}
else { echo " There is error " ;}

Even if you keep a limit, there is another limit set at php.ini to restrict maximum upload size. So you will get a success message but actually file may not go. I remember it is 2 MB by default.
rothana
10-21-2010
Dear smo1234,

Thank you for your quick response. I have tried it, but it not working well. At first, it's not working because you forgot ) in the condition. That's brilliant......!

One more thing, how can I change the default value of file upload from 2MB to a larger one?

MANY MANY thank in advance,
Rothana
smo1234
10-21-2010
You should have access to your php.ini file. If it is your PC then open this file and search for this lines

; Maximum allowed size for uploaded files.
upload_max_filesize = 2M

Change this 2M to 5M and you may need to restart.
rothana
10-21-2010
Dear smo1234,

I can see that file and edit on my PC, but how can make it effected to the website? I can't find that file on my hosting too.

Anyway, I heard that if I change it, it will effect to the whole site. So do you have any script that can do this?

Many thanks again and again,
Rothana
smo1234
10-21-2010
I don't think you can do this by any script in a shared hosting environment. Contact your host and ask about this.

You can read the value set at your hosting server php.ini by using phpinfo() function.

From the list displayed search for the line upload_max_filesize It will show you the size in MB.
rothana
10-22-2010
Dear smo1234,

I have asked the hosting admin about this and he said he can't change Php.ini for me because it will effect to others on his hosing service. He said there are some script to put on the page for this matter.

Any more idea? I'm really sorry for disturbing you and thanks for you time help me.

Best regards,
Rothana
smo1234
10-22-2010
$max_file_size = 1048576

Add this line at top of the form processing page. This may work. It depends version of php, server etc .
deepakv312
11-01-2010
Hi can i file upload in php with different format(.jpg,.png,.gif,.txt,.doc)
rothana
11-09-2010
Dear smo1234,

I heard that we can upload large file by ftp script. I have no idea on that.

Having a look at my script, is there any way to do it with ftp in PHP?

Best regards,
Rothana
smo1234
11-10-2010
You can use ftp and upload large files but your visitors are not expected to use ftp. Most of them may not be aware of ftp.
rothana
11-17-2010
Dear smo1234,

Finally, my ISP agreed to change the configuration in the php.ini. Thanks for your helpful help so far.

One more thing to ask for help, smo1234. Now I set wampserver in my new machine. The localhost works ok, BUT the index page doesn't view the contents of pages that I include in the index. Here is some of the code:

<?

$page=(empty($_GET["page"])?"":$_GET["page"]);
if($page=="" || $page=="home"){include("home.php");}
elseif($page=="history"){include("history.htm");}
elseif($page=="job"){include("job.htm");}

?>

The header and footer of index.php displays properly, but not the content of pages in the middle. Or is there any error with my local configuration?

Any idea? I have been searching for help for near 2 weeks, but not help.

Many thanks in advance,
Rothana
smo1234
11-17-2010
Try by changing the first PHP tag like this
<?Php

PHP Code

?>
rothana
11-17-2010
Wow....

It works perfectly. Thanks for your help in all matters. I'm sure I'll disturb you more when I need your help. :-)

May god bless you, smo1234......

Rothana
Please Login to post your reply or start a new topic