Page 1 of 1

Uploading large flv files

Posted: Thu Nov 05, 2009 5:58 am
by krystof78
Hi there,

I have some trouble uploading large FLV files via a form. Here is my code:

Code: Select all

 
<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" />
&nbsp;
<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 FLV 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 FLV 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 a FLV?";
        }
        $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 FLV 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;
        }
    }
}
?>
 
I tried to include "ini_set('memory_limit','30 M');" at the top of the page or "php_value upload_max_filesize 40M, php_value post_max_size 40M, php_value max_execution_time 400, php_value max_input_time 400" in a .htaccess file, nothing works.

What it does is that it seems to refresh itself when I click on Submit. No error message or anything happens... I used this script for pdf and mp3 files and it worked well... :(

Any idea what went wrong?

Re: Uploading large flv files

Posted: Thu Nov 05, 2009 10:01 am
by jackpf
So what are your max post/file sizes?

Also, var_dump() $_FILES['filename']['error'] as well, to see if there's an error code.

Re: Uploading large flv files

Posted: Thu Nov 05, 2009 10:08 am
by Reviresco
I was having a similar problem (added post_max_size increase in the .htaccess file and it had no effect), and I finally found out what the problem was: the .htaccess file needed to go not in the root directory or the directory I was using, but in a directory called cgi_bin. This was for a Network Solutions hosted site. I believe it was because that was the directory where php was installed or configured.

Re: Uploading large flv files

Posted: Thu Nov 05, 2009 10:22 am
by Wolf_22
Sometimes this comes from the admin level of things as a similar issue occurred at my school and the only way to fix it was from having one of the administrators step in and adjust their PHP.ini file (and not our .htaccess files).

Re: Uploading large flv files

Posted: Thu Nov 05, 2009 10:29 am
by jackpf
You have PHP installed at school?!?!?!

Re: Uploading large flv files

Posted: Thu Nov 05, 2009 10:35 am
by Wolf_22
Yeah, why is that such a shocker?

I think they're trying to get away from it because they've been rolling over to Microsoft stuff (IIS, ASP, SharePoint, etc.), but I don't see it ever being completely left out of the club because there's too much depending on it.

Re: Uploading large flv files

Posted: Thu Nov 05, 2009 12:57 pm
by jackpf
Because mine doesn't :P

We're learning delphi in computing (sigh), but I code everything we're told to in C++ as well, so it's ok really...

It would be cool if they had PHP though. Although I don't see why they would. No one learns it, since they don't teach it. Except me cause i'm a nub.

Re: Uploading large flv files

Posted: Thu Nov 05, 2009 1:16 pm
by Wolf_22
Maybe I'm an idiot, but I always thought that UK was a central hub for people doing anything in PHP.

Re: Uploading large flv files

Posted: Thu Nov 05, 2009 1:56 pm
by jackpf
Even so, that doesn't mean that every school has it.

I might ask the technicians actually, just to see what they say. I know one of them codes in PHP too, so he'd probably be with me :D

Anyway...this is a bit off topic :P But since the OP hasn't replied for a while, I guess it's justified...