Jump to content

Uploading large files with PHP


InGale
 Share

Recommended Posts

Hello!I'm new to php programming, now I'm trying to build a small site using this language and I encountered into this problem: I'm trying to make a form for uploading files (this form will be for me only, not for other users), later I'll want to add these files to MySQL DB, but for now I'm just trying to make a form that will upload the files. I used a tutorial found on this site: http://www.w3schools.com/php/php_file_upload.aspIt works just fine with small files, but once I tried to upload a bigger file (I test everything locally), 22 MB, it didn't work. I tried to google it and I found some info that php supports only files up to 2 MB, and I have to change settings in php.ini in order to allow bigger file sizes and longer time for execution of php (here's the link: http://www.sitepoint.com/upload-large-files-in-php/). So I changed the php.ini file, and I also, just in case, defined these constraints in my php code. so now my code looks like this:

ini_set('upload_max_filesize', '50M');  ini_set('post_max_size', '50M');  ini_set('max_input_time', 600);  ini_set('max_execution_time', 600);if ($_FILES["file"]["error"] > 0)  {  echo "Error: " . $_FILES["file"]["error"] . "<br />";  }else  {  echo "Upload: " . $_FILES["file"]["name"] . "<br />";  echo "Type: " . $_FILES["file"]["type"] . "<br />";  echo "Size: " . ($_FILES["file"][ceil("size")] / 1024) . " Kb<br />";  //echo "Stored in: " . $_FILES["file"]["tmp_name"];    if (file_exists("../magazines/" . $_FILES["file"]["name"]))	  {	  echo $_FILES["file"]["name"] . " already exists. ";	  }	else	  {	  move_uploaded_file($_FILES["file"]["tmp_name"],	  "../magazines/" . $_FILES["file"]["name"]);	  echo "Stored in: " . "../magazines/" . $_FILES["file"]["name"];	  }  }

I restarted the server after I changed the php.ini, but it still doesn't work, I still can't upload large files. Need some help with it... Thank you!

Link to comment
Share on other sites

The upload_max_filesize, post_max_size, and max_input_time don't have any effect when you set them in the script, they must be set in php.ini or another configuration file like .htaccess.To verify you are using the correct settings, run a phpinfo script:<?php phpinfo(); ?>It will show you all of the configuration options in effect, make sure the options you have changed are using the new values. If not, the phpinfo output will also list the location of the php.ini file that it's using. Make sure you're editing the correct php.ini file.Otherwise, look up the error code from the file upload. This is the list of error codes and messages:http://www.php.net/manual/en/features.file-upload.errors.php

Link to comment
Share on other sites

Make sure you're editing the correct php.ini file.
Yup, apparently I had 2 php.ini files and I edited the wrong one (I didn't even know the other exists). Changed the settings in the correct .ini file and everything worked like a charm! Thank you very much, appreciate your help!BTW, when it's time to upload the files to my host server, if I encounter the same problem, should I tell them to change these settings in their php.ini file as well? And if I do and they don't agree, is there any other way around to upload bigger files? Thanks again! Edited by InGale
Link to comment
Share on other sites

You can ask your host if they'll change it, but if they won't change it there's nothing you can do to increase the size on that server. You can try to put a php.ini or .htaccess file in your directory with only the settings you want to change, which will work if they allow that. Basically, if you need to change it, contact them and see what their policy is.

Link to comment
Share on other sites

Oh, and one more thing: I want to round up the echoed file size, cause now I have some huge number - 22705.782226562 Kb. So I tried this:

echo "Size: " . ceil(($_FILES["file"]["size"] / 1024)) . " Kb<br />";

It works, but I get a whole number. How can I round it up to have two digits after decimal point, so in this case I would have 22705.78 instead of 22706?

Edited by InGale
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...