SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: Video Upload

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Video Upload

    Hi to all you helpful people here at sitepoint, I keep putting this to the back burner, but now that I've had so much help with other problems that are now all sorted out I thought it was time to address this once again. Firstly is it possible to upload a video file to the same machine i.e. I'm running windows 7 with wamp server as my testing or development server and I want to upload a video file from a second hard drive, can that be done? I know theres a ton of tutorials out there, to show how to do this, however, I just simply can't get it to work. I've changed the php.ini file to allow up to 500mb both the post data size and max allowed file size, there may be other setting in the php.ini file that need to change as well but I don't know what. The best I've been able to get is a blank page loaded with no errors and nothing echoed and definetly no file in the upload directory. As I've hacked most of the tutorials to bits, trying to get it to work, I don't have any php to post here, But I do have the form that I'm using, maybe one of you gurus might be able to write me something for it, here it is

    <form method="post" action="videoupload.php" enctype="multipart/form-data"/>
    <input type='hidden' name='MAX_FILE_SIZE' value='500000' />
    <input type="hidden" name="FILE_FORMAT" value="mp4" />
    <input type="file" name="upload" size='20' />
    <input type="submit" class="style1" value="Upload" />
    </form>

    And as always any help is greatly appreciated

  2. #2
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    So after you attempt to upload have your tried copying the the tmp file to a new location or it just doesn't work? This process should not care whether it is uploading it to the same or remote machine. I have this working on the same machine, but I use Linux, so it may be different.

    Have you output $_FILES by using var_dump($_FILES) after you post this should look something like:
    array(1) { ["upload"]=> array(5) { ["name"]=> string(11) "video.mp4" ["type"]=> string(15) "application/mp4" ["tmp_name"]=> string(14) "/tmp/php4fdiQJ" ["error"]=> int(0) ["size"]=> int(20318) }}
    If you see something similar in your output then you can copy the file using something like:

    Code PHP:
    if($_FILES['upload']['name'] != ''){
            $target_path = "/path/to/upload/folder/";
            $target_path = $target_path . basename( $_FILES['upload']['name']);
            if(move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) { //Move files to your target directory from the tmp directory
                 $msg = "The file <strong>".  basename( $_FILES['upload']['name']). "</strong> has been uploaded";
            } else{ //Failure
                 $msg = "There was an error uploading the file  ".  basename( $_FILES['upload']['name']). ", please try again!";
            }
    }
    ?>

    Hope this helps.

    Steve
    ictus==""

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx for the reply Server Storm, yes I have tried moving the tmp file with very similar code to what you just posted, but I haven't used the var_dump($_FILES) as its not something I'm very familiar with, only come across it once before, and I didn't understand it back then, this is all very new to me, as I'm trying to teach myself. However, I'll copy this across and get back to you soon.Cheers

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I just got "array empty" and an error undefined index upload on this line "if($_FILES['upload']['name'] != ''){" .Cheers

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've managed to get the undefined index error fixed by changing the above php code to what I've posted underneath, however, the var_dump($_FILES) says the array is still empty, I did some googling and found out that the post data size needs to be greater than the max allowed file size, so I corrected that by increasing the post data size by 100mb, but still no file uploaded and array empty, what needs to be done to get array to show a string?
    Here's the code I changed.

    <?php


    ini_set('display_errors',1);
    error_reporting(E_ALL);



    var_dump($_FILES);

    if ($_FILES)//['upload']['name'] != '')
    {
    $name = $_FILES['upload']['name'];
    $target_path = "upload/";
    $target_path = $target_path . basename( $_FILES['upload']['name']);
    if(move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) { //Move files to your target directory from the tmp directory
    $msg = "The file <strong>". basename( $_FILES['upload']['name']). "</strong> has been uploaded";
    } else{ //Failure
    $msg = "There was an error uploading the file ". basename( $_FILES['upload']['name']). ", please try again!";
    }
    }
    ?>

  6. #6
    SitePoint Member
    Join Date
    May 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I have modified your code and managed to get it working for you:
    HTML Code:
    <html>
    <head>
    </head>
    <body>
    
    <form method="post" action="upload.php" enctype="multipart/form-data"/>
    	<input type='hidden' name='MAX_FILE_SIZE' value='500000' />
    	<input type="hidden" name="FILE_FORMAT" value="mp4" />
    	Choose file to upload:<input type="file" name="upload" size='20' />
    	<input type="submit" name="submit" value="Upload" />
    </form>
    PHP Code:
    <?php

    ini_set
    ('display_errors',1); 
    error_reporting(E_ALL); 

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

    if(!empty(
    $_FILES['upload']) && ($_FILES['upload']['error'] == 0))    {
        
    $name $_FILES['upload']['name'];
        
    $target_path "upload/"//PUT IN THE PATH WHERE YOU WANT THE UPLOADED FILE TO GO.
        
    $target_path $target_path basename$_FILES['upload']['name']);
        
    if(
    move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) { //Move files to your target directory from the tmp directory
        
    echo "The file <strong>"basename$_FILES['upload']['name']). "</strong> has been uploaded";
    } else { 
    //Failure
        
    $msg "There was an error uploading the file "basename$_FILES['upload']['name']). ", please try again!";
    }

    } else {
        echo 
    'No file was uploaded.';
    }

    }

    ?>
    HTML Code:
    </body>
    </html>
    This code is by no means secure and still needs more work but I'll leave that up to you.

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Codarz360, thankyou very much for your effort, but unfortunately it still does not work, you seem to have written this as if all the code is on the same page, it is not, the html form is on a completely seperate page to the php. And when I added the var_dump($_FILES); to your code I'm still getting array empty!!! Nothing echoed and no uploaded video. It'exactly the same as if I was still using my code. Cheers

  8. #8
    SitePoint Member
    Join Date
    May 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by studentofIT View Post
    Hi Codarz360, thankyou very much for your effort, but unfortunately it still does not work, you seem to have written this as if all the code is on the same page, it is not, the html form is on a completely seperate page to the php. And when I added the var_dump($_FILES); to your code I'm still getting array empty!!! Nothing echoed and no uploaded video. It'exactly the same as if I was still using my code. Cheers
    Ok I have now split the pages up into two separate ones.

    form.html file:
    HTML Code:
    <html>
    <head>
    </head>
    <body>
    
    <form method="post" action="upload.php" enctype="multipart/form-data"/>
    	<input type='hidden' name='MAX_FILE_SIZE' value='500000' />
    	<input type="hidden" name="FILE_FORMAT" value="mp4" />
    	Choose file to upload:<input type="file" name="upload" size='20' />
    	<input type="submit" name="submit" value="Upload" />
    </form>
    
    </body>
    </html>
    Upload.php file

    PHP Code:
    <?php

    ini_set
    ('display_errors',1); 
    error_reporting(E_ALL); 

    var_dump($_FILES);

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

    if(!empty(
    $_FILES['upload']) && ($_FILES['upload']['error'] === 0))    {
        
    $name $_FILES['upload']['name'];
        
    $target_path ""//PUT IN THE PATH WHERE YOU WANT THE UPLOADED FILE TO GO.
        
    $target_path $target_path basename($name);

    if(
    move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) { //Move files to your target directory from the tmp directory
        
    echo "The file <strong>"basename($name). "</strong> has been uploaded";
    } else { 
    //Failure
        
    $msg "There was an error uploading the file "basename($name). ", please try again!";
    }
    } else {
        echo 
    'No file was uploaded.';
    }
    }
    ?>
    That is working perfectly for me and is showing the array.

  9. #9
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx Codarz360 but I put it into 2 pages when I tested it before, I don't think the problem is with the code, I think it might be more to do with the php.ini file or maybe permissions or something like that, it really beats me as to why it shows the array as empty. Cheers mate.

  10. #10
    SitePoint Member
    Join Date
    May 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok here is a link to everything that is in my PHP.ini file on my WAMP server http://pastebin.com/rZLsMgmn just copy and paste that over yours and test to see if it works correctly.

  11. #11
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your upload_max_filesize = 2M. That is somewhat small for video file. Perhaps you need to set it to a larger number.

  12. #12
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firstly, what version of php is this? Secondly, how big a file have you been using to try this with as your max allowed file size is still set at 2mb where I have mine set to 500mb and I've been trying to upload an 18mb file, as its getting rather late here now, I won't be back to reply until the morning. Cheers mate.

  13. #13
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Beat me to it tom8 .Cheers mate

  14. #14
    SitePoint Member
    Join Date
    May 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've just been uploading a small text file that is why it is still set to 2MB. It's been working fine with the text file and maybe you want to try it using a text file to test your settings are correct.

  15. #15
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    You do realize your form's max file size is set to 500k, not 500M, right? That number is measured in B, not KB.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  16. #16
    SitePoint Member
    Join Date
    May 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So then that would mean that in order to equal 500MB you would need to set the form value to 524,288,000 bytes?

  17. #17
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    correct.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  18. #18
    SitePoint Member
    Join Date
    May 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that this should solve your problem then studentofIT.

    Changed the value of the MAX_FILE_SIZE to 524288000 bytes which should now allow you to upload your file.
    HTML Code:
    <html>
    <head>
    </head>
    <body>
    
    <form method="post" action="upload.php" enctype="multipart/form-data"/>
    	<input type='hidden' name='MAX_FILE_SIZE' value='524288000' />
    	<input type="hidden" name="FILE_FORMAT" value="mp4" />
    	Choose file to upload:<input type="file" name="upload" size='20' />
    	<input type="submit" name="submit" value="Upload" />
    </form>
    
    </body>
    </html>
    Added in a file-type check.
    PHP Code:
    <?php

    ini_set
    ('display_errors',1); 
    error_reporting(E_ALL); 

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

    if(!empty(
    $_FILES['upload']) && ($_FILES['upload']['error'] === 0))    {
        
    $name $_FILES['upload']['name'];
        
    $target_path ""//PUT IN THE PATH WHERE YOU WANT THE UPLOADED FILE TO GO.
        
    $target_path $target_path basename($name);
        
    if(
    $_FILES['upload']['type'] == 'video/mp4') {    

    if(
    move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) { //Move files to your target directory from the tmp directory
        
    echo "The file <strong>"basename($name). "</strong> has been uploaded";
    } else { 
    //Failure
        
    $msg "There was an error uploading the file "basename($name). ", please try again!";
    }
    } else {
        echo 
    'Incorrect file type. Must be .mp4';
    }
    } else {
        echo 
    'No file was uploaded.';
    }
    } else {
        
    header('location: form.html');
    }

    ?>
    I've tested it out now with a 6.75mb .mp4 file and everything seems to working ok.

  19. #19
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx StarLion, I was wondering about that, I did have it set at 500mb at one stage, but I think I changed it back again before I tested, Codarz360 there is no need to keep posting the same code over again, as there is nothing wrong with my code, except for the max file size limit as just mentioned by StarLion, I will be using that. Now all I have to do is change it and test it. Be back soon with the results. Cheers very muchly guys.

  20. #20
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok changing the form to 500000000 bytes had no effect whatsoever, however, I did what Codarz360 suggested and tried 2 smaller files, seperately of course, the first one was a .txt file, the size was just 49bytes, and the second was a microsoft word document, the size was 2.22mb, the .txt file gave this
    array
    'upload' =>
    array
    'name' => string 'Rockbits URL.txt' (length=16)
    'type' => string 'text/plain' (length=10)
    'tmp_name' => string 'C:\wamp\tmp\phpC5C5.tmp' (length=23)
    'error' => int 0
    'size' => int 49
    but still no file was uploaded and nothing echoed, the word document gave this
    array
    'upload' =>
    array
    'name' => string 'All great websites have a great server behind them2.docx' (length=56)
    'type' => string '' (length=0)
    'tmp_name' => string '' (length=0)
    'error' => int 1
    'size' => int 0
    once again no file uploaded and nothing echoed here either, obviously the word document didn't even get to the tmp dir and showing an error with no size at all, the .txt file made it to the tmp dir with no errors and there wasn't a problem with it recognisining the size, so beats me as to whats going on.

  21. #21
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like I might of been editing the wrong php.ini file, the one I've been editing is located here C:\wamp\bin\php\php5.3.9\php.ini, I just found this one and have just finished editing it, located here C:\wamp\bin\apache\Apache2.2.21\bin\php.ini, so I tested it with a larger video file and got the following results
    array
    'upload' =>
    array
    'name' => string 'The Beatles - Help (1965).mp4' (length=29)
    'type' => string 'video/mp4' (length=9)
    'tmp_name' => string 'C:\wamp\tmp\phpB7AE.tmp' (length=23)
    'error' => int 0
    'size' => int 19164828
    but still no file uploaded and nothing echoed, at least thats some improvement, I guess the problem now, lies with either the move function and/or the target dir. The path of the upload page is www.mysite.com/html/upload.php, the path of the target dir is www.mysite.com/html/upload/ . The target path is set to this $target_path = "upload/"; is this incorrect?

  22. #22
    SitePoint Member
    Join Date
    May 2012
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by studentofIT View Post
    Looks like I might of been editing the wrong php.ini file, the one I've been editing is located here C:\wamp\bin\php\php5.3.9\php.ini, I just found this one and have just finished editing it, located here C:\wamp\bin\apache\Apache2.2.21\bin\php.ini, so I tested it with a larger video file and got the following results
    array
    'upload' =>
    array
    'name' => string 'The Beatles - Help (1965).mp4' (length=29)
    'type' => string 'video/mp4' (length=9)
    'tmp_name' => string 'C:\wamp\tmp\phpB7AE.tmp' (length=23)
    'error' => int 0
    'size' => int 19164828
    but still no file uploaded and nothing echoed, at least thats some improvement, I guess the problem now, lies with either the move function and/or the target dir. The path of the upload page is www.mysite.com/html/upload.php, the path of the target dir is www.mysite.com/html/upload/ . The target path is set to this $target_path = "upload/"; is this incorrect?
    The target path looks fine, however just try changing it to $target_path = ""; and see if it just uploads to the html directory. If that fails to work check your CHMOD permissions and make sure you are able to write to the upload folder.

  23. #23
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Changing the target path to $target_path = ""; did nothing Codarz360, and you can't CHMOD a windows directory, and as I can fwrite to the html directory and the upload directory inherits its permissions from there, I don't think its a permission issue. The page loads very fast, which makes me think it hasn't had time to move the file, and I still don't get anything echoed just a blank page, no file name, no error saying it didn't upload, absolutely nothing. Cheers

  24. #24
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Throw in a bunch of echos. One at the very top, one in every branch of your IF's, so you can see where the program is going.

    If nothing STILL shows up, you've got a syntax error and display_errors is being stubborn and not showing you it.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  25. #25
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Location
    Brisbane Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx StarLion I'll give it a try, but I'm beginning to think that I can't upload to a windows system on the same machine. Even though the array now shows C:\wamp\temp\filename.tmp, I've had the temp directory open on my second monitor while I tried to upload and nothing is being written not even for a split second. Cheers


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •