0

I'm making an uploader for a website which is designed to upload videos. As of now, it doesn't check if they're videos, it's simply uploads them. I do this through a simple form that selects a file and submits it to upload.php. Here is the HTML which I do this with:

<form action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startUpload();">
Video Name: <input type="text" name="name" class="maininput" style="width:300px;" maxlength="80"><br>
File: <input name="myfile" type="file" class="mainbutton"/ style="clear:both;"><br>
Description: <br><textarea cols="43" rows="10"></textarea><br>
      <input type="submit" name="submitBtn" value="Upload" class="mainbutton"/>
</form><br><br><br>
<p id="f1_upload_process">Loading...<br/><img src="/images/loader.gif" width="20" height="20" /></p>
<p id="result"></p>

<iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;">
<script language="javascript" type="text/javascript">
window.top.window.stopUpload(<?php echo $result; ?>);
</script> </iframe>

Here are my javascript functions which accompany this:

function startUpload(){
    document.getElementById('f1_upload_process').style.visibility = 'visible';
    return true;
}
function stopUpload(success){
      var result = '';
      if (success == 1){
         document.getElementById('result').innerHTML =
       '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
      }
      else {
         document.getElementById('result').innerHTML = 
           '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
      }
      document.getElementById('f1_upload_process').style.visibility = 'hidden';
      return true;   
}

And finally, here is the contents of upload.php, which I use for actually uploading the file:

<?php
   $result = 0;

   $target_path = "videos/";
   $target_path = $target_path . basename( $_FILES['myfile']['name']); 

    if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
      $result = 1;
    }

   sleep(1);
?>

I believe that the issue is with the upload.php. The problem is not with anything client side, it's the fact that in the client, it uploads the file, but I can't find the file in the videos folder, or any folder in the server directory.

Any help is greatly appreciated. Thanks!

1 Answer 1

2

This is what I use, you can customize it to suit your script:

Simply change the *path and *variables.

<?php
// Configuration - Your Options
$allowed_filetypes = array('.mov','.mp3','.mp4','.flv'); // These will be the types of file that will pass the validation.
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = './files/'; // The place the files will be uploaded to (currently a 'files' directory).

$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');

// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');

// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');

// Upload the file to your specified path.
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
else
echo 'There was an error during the file upload. Please try again.'; // It failed :(.

?>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your response. My next problem is a limit imposed by the .htaccess file. Is this common?
@RyanSparks You're welcome. As for the limit imposed by an .htaccess file, it isn't common, however check what your php.ini file is set to and see what the upload max limit is set at.
@RyanSparks The default upload max is usually set to 10 or 20M. To check this, make up a file called php_info.php with this inside and run it from your browser. <?php phpinfo();?> It will also show you all your MAX limits and complete PHP server info.
Thanks! As a matter of fact, this was causing my error all along. Do most hosts let you edit the php.ini file to expand that limit? If not, is there a set of hosts that do? Like, does HostGator?
@RyanSparks Was the php.ini file giving you the problem or the .htaccess? Some hosts do let you edit the php.ini file, while some don't. For the ones that don't, there are ways to override those in many ways; one of which being to make up your own php.ini file and upload it to your cgi-bin. Least, that's how my host works. Do a search for PHP.ini override settings, there's a lot of info out there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.