Jump to content

Question about the upload of large files


Recommended Posts

Hi there,

 

I have a question about the upload of large files, like videos (files generally above 20Mo).

 

Is it better to use the FTP functions of php or the normal upload fonctions using additional features, like:

- php_value upload_max_filesize 40M

- php_value post_max_size 40M

- php_value max_execution_time 400

- php_value max_input_time 400

 

Thanks in advance for your input... :)

It does not seem to work... Or it is in my code that there is a problem...

 

I've put this before the header

<?php
ini_set('memory_limit','30 M');
?>

 

And this is my form:

<form action="edit_showreel.php?actor=<?php echo $sel_actor['id'];?>&currentpage=<?php echo $currentpage;?>" method="post"
enctype="multipart/form-data">
<label for="uploadedfile">Filename:</label>
<input type="file" name="uploadedfile" id="uploadedfile" />
 
<input type="submit" name="submit1" value="Submit" />
</form>
<?php
if (isset($_POST['submit1'])) {
echo "<br/>Please wait while we attempt to upload your file...<br><br>";

$target_path = "files/showreel/";

$flag = 0; // Safety net, if this gets to 1 at any point in the process, we don't upload.

$filename = $_FILES['uploadedfile']['name'];
$filesize = $_FILES['uploadedfile']['size'];
$mimetype = $_FILES['uploadedfile']['type'];
$filesize2 = substr($_FILES['uploadedfile']['size'] / 1000000, 0, 4);

$filename = htmlentities($filename);
$filesize = htmlentities($filesize);
$mimetype = htmlentities($mimetype);

$target_path = $target_path . basename( $filename ); 

if ($filename != "") {
	echo "Beginning upload process for file named: ".$filename."<br>";
	echo "Filesize: ".$filesize2." Mo<br>";
	echo "Type: ".$mimetype."<br><br>";
}

//First generate a MD5 hash of what the new file name will be
//Force a MP3 extention on the file we are uploading

$hashedfilename = md5($filename);
$hashedfilename = $hashedfilename.".flv";

//Check for empty file
if ($filename == "") {
	$error = "No File Exists!";
	$flag = $flag + 1;
}

//Whitelisted files - Only allow files with MP3 extention onto server...

$whitelist = array(".flv");
foreach ($whitelist as $ending) {
	if(substr($filename, -(strlen($ending))) != $ending) {
	$error = "The file type or extention you are trying to upload is not allowed! You can only upload MP3 files to the server!";
	$flag++;
	}
}

if ($filesize > 25000000) {
	//File is too large
	if($flag == 0){
	$error = "The file you are trying to upload is too large! Your file can be up to 25 MB in size only. Please upload a smaller FLV file or encode your file with a lower bitrate.";
	}
	$flag = $flag + 1;
}

if ($filesize < 5000000) {
	//File is too small
	if($flag == 0){
	$error = "The file you are trying to upload is too small! Your file has been marked as suspicious because our system has determined that it is too small to be a valid FLV file. Valid FLV files must be bigger than 5 MB and smaller than 25 MB.";
	}
	$flag = $flag + 1;
}

//Check the mimetype of the file
if ($mimetype != 'video/x-flv') {
	if($flag == 0){
	$error = "The file you are trying to upload does not contain expected data. Are you sure that the file is an MP3?";
	}
	$flag = $flag + 1;
}

//All checks are done, actually move the file...
if ($flag == 0) {
	//Now we check that the file doesn't already exist.
	$existname = "files/showreel/".$hashedfilename;

	if (file_exists($existname)) {
		unlink($existname); 
		move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
		rename("files/showreel/".$filename, "files/showreel/".$hashedfilename);
	    echo "The file ".  basename( $filename ). " has been uploaded. You can <a href=\"edit_actor_media.php?actor=$sel_actor[id]&currentpage=$currentpage\">go back to his profile</a><br/>";
		$id = mysql_prep($_GET['actor']);
		$query = "UPDATE actors SET
					show_reel = '{$hashedfilename}'			
				WHERE id = {$id}";
		$result = mysql_query($query, $connection);
	} else {
		if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
		    //Change the filename to MD5 hash and FORCE a MP3 extention.
		    if (@file_exists("files/showreel/".$filename)) {
			    //Rename the file to an MD5 version
			    rename("files/showreel/".$filename, "files/showreel/".$hashedfilename);
			    echo "The file ".  basename( $filename ). " has been uploaded. You can <a href=\"edit_actor_media.php?actor=$sel_actor[id]&currentpage=$currentpage\">go back to his profile</a><br/>";
				$id = mysql_prep($_GET['actor']);
				$query = "UPDATE actors SET
							show_reel = '{$hashedfilename}'			
						WHERE id = {$id}";
				$result = mysql_query($query, $connection);
		    } else {
				echo "There was an error uploading the file, please try again!";
		    }
		} else {
		    echo "There was an error uploading the file, please try again!";
		}
	}
} else {
	echo "File Upload Failed!<br>";
	if ($error != "") {
		echo $error;
	}
}
}
?>

 

Any suggestion/idea?

You can raise this amount to 50M because your still checking the upload size with $_FILES["uploadedfile"]["size"]

 

ini_set('memory_limit','50 M');

 

Also what errors do you get, do you get this echo:

 

The file you are trying to upload is too large! Your file can be up to 25 MB in size only. Please upload a smaller FLV file or encode your file with a lower bitrate

 

You can add this :

 

if($_FILES["uploadedfile"]["size"] > (25* 1024)){

echo 'Too Large!'

die();
}

AFAIK, the memory_limit is really only a concern if you are trying to resize a photo on upload. You need to up your upload_max_filesize, post_max_size, and max_execution_time to handle larger uploads. You can not change any of those with ini_set, so you must do it with either htaccess or your Apache config. I prefer htaccess.

 

Regardless of whether you use PHP's FTP functions or its normal upload features, either way the user is uploading a file through a web form and the limits still apply.

Also what errors do you get, do you get this echo:

 

The file you are trying to upload is too large! Your file can be up to 25 MB in size only. Please upload a smaller FLV file or encode your file with a lower bitrate

 

Nope, I did not get that error message. I did not get anything. It's like the page is refreshing itself and nothing happens. No error message saying that it did not work, or it did work. And the file is not moved to where it should be moved so it definitelly does not work...

 

I made this script work for pdf and mp3 files, but can't make it work for flv files. I guess that's an issue of size of the file... I tried to put the "ini_set('memory_limit','50 M');" at the top of the page, as well as this in a .htaccess file:

- php_value upload_max_filesize 40M

- php_value post_max_size 40M

- php_value max_execution_time 400

- php_value max_input_time 400

 

But it still does not work... :(

1. Is it taking longer than 6 minutes to upload?

2. Run phpinfo() at the top of the file and ensure your htaccess setting changes are being picked up.

3. It is possible that Apache's limit defined in the config is being reached.  See the limitRequestBody setting.

1. Is it taking longer than 6 minutes to upload?

2. Run phpinfo() at the top of the file and ensure your htaccess setting changes are being picked up.

3. It is possible that Apache's limit defined in the config is being reached.  See the limitRequestBody setting.

 

1. I don't know how long it is going to take as it will depend on the user's connection I guess.

2. As for the phpinfo, where in the phpinfo do you see that the .htaccess setting changes are being picked up or not?

 

Thanks for your reply...

Just run phpinfo() and look for

- upload_max_filesize

- post_max_size

- max_execution_time

- max_input_time

 

Make sure they have the correct values.  Also, I'm not sure what your target audience's connection speeds are, but a 20-30mb file may take longer than 6 minutes to upload.

Finally, we're getting somewhere. Is your htaccess in the site's root directory?  If not, try placing it there and check the values with phpinfo() again.

 

If that still does not work, then your host may not be allowing you to change php config values via htaccess.  If this is the case, you'll have to have your host edit the php.ini file (unless you are able to).

 

Edit: Another thing I just thought of, although I dont think its an issue. In your htaccess file, try surrounding your string values with double quotes, like this:

php_value max_execution_time 620
php_value max_input_time 600
php_value post_max_size "42M"
php_value upload_max_filesize "40M"

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.