Viewed   81 times

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!

 Answers

1

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 :(.

?>
Sunday, November 6, 2022
 
4

You need to set the enctype="multipart/form-data" attribute on your form.

Example 1 from php.net:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>
Thursday, September 15, 2022
 
edtech
 
1

A quick and easy solution:

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Upload test</title>
    </head>
    <body>
        <form action="upload.php" method="post" enctype="multipart/form-data">
            File: <input type="file" name="file"/> 
            <input type="submit" value="Upload" />
        </form>
    </body>
</html>

upload.php

<?php
    if (isset($_FILES['file'])) {
        $file = $_FILES['file'];

        $file_name = $file['name'];
        $file_tmp = $file['tmp_name'];
        $file_size = $file['size'];
        $file_error = $file['error'];

        $file_ext = explode(".", $file_name);
        $file_ext = strtolower(end($file_ext));

        $allowed = array("epub", "pdf", "html"); //The extensions you allow

        if (in_array($file_ext, $allowed)) {
            if ($file_error === 0) {
                if ($file_size <= 2097152) {
                    $file_destination = ' '.$file_name; // If ' ', the file will be placed in this directory
                    if (move_uploaded_file($file_tmp, $file_destination)) {
                        echo $file_destination;
                    } else {
                        echo "An error has been encountered while moving your file!";
                    }
                } else {
                    echo "Your file is too big!";
                }
            } else {
                echo "An error has been encountered while uploading your file!";
            } 
        } else {
            echo "You can't upload files of this type!";
        }
    }
?>

Notes:
- $file_destination = ' '.$file_name; -> The ' ' represents in which directory after this one the file will be place in, so ' ' means that it will be placed in this directory, 'test/' means it will be placed in the test subdirectory of this directory, etc.

- If you want something more secure you could try this

- You could also look through one of these solutions

I've tried the script above and it seems to move the file but that file actually doesn't exist in that directory.

So here's an updated script that actually does work:

<?php
    if (isset($_FILES['file'])) {
        $host = "ftp.example.com";
        $user = "username";
        $pass = "password";
        $destDir = "/public_html";    //The destination directory for the uploaded file (`/public_html` is the root directory for your website files, in some cases it could also be `/var/www`)
        $workDir = " ";

        $tmpName = basename($_FILES['file']['tmp_name']);
        move_uploaded_file($_FILES['file']['tmp_name'], $workDir.$tmpName) or die("Cannot move uploaded file to working directory");

        $conn = ftp_connect($host) or die ("Cannot initiate connection to host");
        ftp_login($conn, $user, $pass) or die("Cannot login");
        $upload = ftp_put($conn, $destDir."/".$_FILES['file']['name'], $workDir.$tmpName, FTP_BINARY);
        if (!$upload) {
            echo "Cannot uploadn";
        } else {
            echo "Upload completen";
        }
        ftp_close($conn);

        unlink($workDir.$tmpName) or die("Cannot delete uploaded file from working directory -- manual deletion recommended");
    }
?>
Wednesday, November 23, 2022
 
steved
 
4

From your thumbnail code:

if(preg_match('/[.](jpg)$/', $filename)) 

That will only match lowercase 'jpg'. If you want to match upper or lowercase, you can add the /i flag for case-insensitivity:

if(preg_match('/[.](jpg)$/i', $filename)) 
Sunday, October 2, 2022
 
2

Update: As of version 5.9.0 in May of 2016, Fine Uploader is now 100% Free Open Source Software, licensed under MIT.

Update: As of 19 November 2018, The Fine Uploader project has reached the end of its life.

Please note that there is a long and storied history of Fine Uploader. The original library was developed by Andrew Valums. In August of 2012, after the library at valums/file-uploader decayed for a while as Andrew ran out of time to work on the project, he handed it over to me. The name was then changed to Fine Uploader. At the time Andrew made me the maintainer of the library, there were many forks of the project. The one at valums-file-uploader.github.io was one such fork. All forks, as far as I can tell, have been abandoned for quite a long time. The only current actively maintained version is the "original", now located at Widen/fine-uploader (http://fineuploader.com). The one you referenced (valums-file-uploader.github.io) has had no activity for over 7 months. Meanwhile, the "official" Fine Uploader (a.k.a. valums/file-uploader) has been evolving rapidly since I took over development.

In late March of 2013, Andrew and I transferred ownership of Fine Uploader to Widen Enterprises (my employer). The valums/file-uploader repo was then transferred to Widen/fine-uploader. It is dual-licensed. The default license is GPL v3. We also offer a commercial license. Our licensing model (which is covered in the licensing FAQ) was written with the expectation that commercial uses of Fine Uploader would be licensed/paid. We put money and time into Fine Uploader, and it's important that the product remains financially viable in order for us to continue to maintain and evolve the library.

Wednesday, October 12, 2022
 
gaël_j
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :