Libellula Posted March 28, 2013 Share Posted March 28, 2013 Hello! So I am completely new to the PHP side of the house, so forgive me if I am making very simple basic errors here.. the learning process has been selective. I am trying to create a simple video upload form. I have set restrictions on the types of files the user can upload. I have sucessfully got it to work uploading images, but not videos. I don't know what I am doing wrong? Form Code: <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="Submit"> </form> PHP Script: <?php $allowedExts = array("avi", "mpg", "mov"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "video/avi") || ($_FILES["file"]["type"] == "video/mpg") || ($_FILES["file"]["type"] == "video/mov")) && ($_FILES["file"]["size"] < 100000000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> Original PHP Script that worked for images: <?php $allowedExts = array("jpg", "gif", "png"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 100633448) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> As I said, I am new to this... I figured if the image only version worked, the video only version would work if I changed the extionsions. I changed image/ to video/, just to see if that was why it didn't work. No luck either way. Hoping someone can figure this out! I would so appriciate the assistance. Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/ Share on other sites More sharing options...
computermax2328 Posted March 28, 2013 Share Posted March 28, 2013 Well Hello, What are you getting out of it? Are you getting any errors or any of your echo messages? That would help to figure out what is happening. Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421625 Share on other sites More sharing options...
Libellula Posted March 28, 2013 Author Share Posted March 28, 2013 Thanks for replying. I am getting my echo message "Invalid File." Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421635 Share on other sites More sharing options...
computermax2328 Posted March 28, 2013 Share Posted March 28, 2013 Well what kind of file is it? avi, mpg, mov? How big is the file as well? You are failing your very first if statement that asks for the file type and file size. Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421648 Share on other sites More sharing options...
PaulRyan Posted March 28, 2013 Share Posted March 28, 2013 (edited) Here are the mime types for your selected allowed range: .aviapplication/x-troff-msvideovideo/avivideo/msvideovideo/msvideo.mpgvideo/mpeg.movvideo/quicktime Edited March 28, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421650 Share on other sites More sharing options...
Libellula Posted March 28, 2013 Author Share Posted March 28, 2013 ^computermax My test video is an .avi and it is about 41 mb. It should be accepted? ^PaulRyan So essentually, using video/avi for the accept avi format should work? My test video is .avi and I get the invalid file echo. Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421651 Share on other sites More sharing options...
PaulRyan Posted March 28, 2013 Share Posted March 28, 2013 Nope, it could be anyone of those 4, you need to allow all 4 types to be uploaded. Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421652 Share on other sites More sharing options...
Libellula Posted March 28, 2013 Author Share Posted March 28, 2013 Okay, I have added those you listed to be allowed in all the formats you listed. Same error, "Invalid File". <?php $allowedExts = array("avi", "mpg", "mov"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "application/x-troff-msvideo") || ($_FILES["file"]["type"] == "video/avi") || ($_FILES["file"]["type"] == "video/msvideo") || ($_FILES["file"]["type"] == "video/mpg") || ($_FILES["file"]["type"] == "video/mpeg") || ($_FILES["file"]["type"] == "video/quicktime")) && ($_FILES["file"]["size"] < 100633448) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421656 Share on other sites More sharing options...
PaulRyan Posted March 28, 2013 Share Posted March 28, 2013 The last one on the avi list should be: video/x-msvideo Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421658 Share on other sites More sharing options...
PaulRyan Posted March 28, 2013 Share Posted March 28, 2013 If none of the above work, you should echo out $_FILES["file"]["type"] to see what the files mime type is. Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421660 Share on other sites More sharing options...
Libellula Posted March 28, 2013 Author Share Posted March 28, 2013 ^PaulRyan No, still same error. How would I echo out that to see the mime type? Lost here... Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421665 Share on other sites More sharing options...
PaulRyan Posted March 28, 2013 Share Posted March 28, 2013 Really? Did you build the code above? If you don't know how to echo, then you're gunna struggle. echo $_FILES["file"]["type"]; Put that above the if statement and below extension variable. Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421667 Share on other sites More sharing options...
PaulRyan Posted March 28, 2013 Share Posted March 28, 2013 Infact, you should use var_dump instead. You have too many checks in one place, you should be doing them sequentially. First which should be checked is for file upload errors $_FILES["file"]["error"] Then file size, then file type, then any other checks. Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421673 Share on other sites More sharing options...
Libellula Posted March 28, 2013 Author Share Posted March 28, 2013 No, I didn't. W3 Schools site had the framework for an image uploader, so I was hoping I could adapt it similarly for video. As I said I am brand new to PHP. This specific function is just something one of my clients absolutly needs on their site and it is the only thing I do not know how to do. Frustrating! Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421674 Share on other sites More sharing options...
PaulRyan Posted March 28, 2013 Share Posted March 28, 2013 (edited) I've given you a good start with this, I usually wouldn't as this is a paid project for you. <?PHP $allowedMimes = array('application/x-troff-msvideo', 'video/avi', 'video/msvideo', 'video/x-msvideo', 'video/mpeg', 'video/quicktime'); if(!is_uploaded_file($_FILES['file']['tmp_name'])) { echo 'Possible file upload attack.'; } else if($_FILES['file']['error'] > 0) { echo 'File upload error. Error Code: '. $_FILES['file']['error']; } else if(!in_array($_FILES['file']['type'], $allowedMimes)) { echo 'File type not permitted.'; } else if($_FILES['file']['size'] > 100633448) { echo 'File is too big.'; } else { //## Checks passed, do file moving } ?> You can do the rest from what I've given you above. *Edit - I made a few errors, so I've fixed them. Edited March 28, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421678 Share on other sites More sharing options...
Libellula Posted March 28, 2013 Author Share Posted March 28, 2013 Thank you! So you know, I am doing this without pay for a non-profit organization that supports school fine arts programs. That is why I'm trying to do it, instead of finding someone to hire - as thier isn't really a budget. I really appriciate the time and help. I will try your above soultion out. Quote Link to comment https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/#findComment-1421682 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.