Welcome to Dream.In.Code
Click Here
Getting PHP Help is Easy!

Join 117,576 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 2,122 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



video upload script

 
Reply to this topicStart new topic

video upload script

chipshot
post 13 Jan, 2008 - 11:14 PM
Post #1


New D.I.C Head

*
Joined: 30 Apr, 2006
Posts: 38


My Contributions


I have this php script to upload video files from my membership

If I refresh the page the video uploads again.

how can I prevent this?

Thank You..

CODE
        <form action="" method="post" enctype="multipart/form-data">
          <table width="400" cellpadding="3" >
            <tr>
              <td colspan="3">&nbsp;</td>
            </tr>
            <tr>
              <td width="10" rowspan="2">&nbsp;</td>
              <td width="120"><strong>Choose a file to upload:</strong></td>
              <td width="242"><input type="file" name="uploaded_file" /></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
              <td><input type="submit" name="sendForm" value="Upload File" />
                <br /></td>
            </tr>
            <tr>
              <td colspan="3">&nbsp;</td>
            </tr>
          </table>
        </form>
        <?php

  error_reporting(E_ALL ^ E_NOTICE); // Show all major errors.

  // Check to see if the button has been pressed
  if (!empty($_REQUEST['sendForm']))
  {
    // Assign the name to a variable
    $name = $_FILES['uploaded_file']['name'];
    // Assign the tmp_name to a variable
    $tmp_name = $_FILES['uploaded_file']['tmp_name'];
    // Assign the error to a variable
    $error = $_FILES['uploaded_file']['error'];
    // Assign the size to a variable
    $size = $_FILES['uploaded_file']['size'];
    // No trailing slash
    $uploadFilesTo = 'uploads';
    // Create safe filename
    $name = ereg_replace('[^A-Za-z0-9.]', '-', $name);
    // Disallowed file extensions
    //what files you don't want upoad... leave this alone and you should be fine but you could add more
    $naughtyFileExtension = array("php", "php3", "asp", "inc", "txt", "wma","js", "exe", "jsp", "map", "obj", " ", "", "html", "mp3", "mpu", "wav", "cur", "ani");    // Returns an array that includes the extension
    $fileInfo = pathinfo($name);
    // Check extension
    if (!in_array($fileInfo['extension'], $naughtyFileExtension))
    {
      // Get filename
      $name = getNonExistingFilename($uploadFilesTo, $name);
      // Upload the file
      if (move_uploaded_file($tmp_name, $uploadFilesTo.'/'.$name))
      {
          // Show success message
          echo '<center><p>Your Video File has uploaded successfully<br />'.$uploadFilesTo.'/'.$name.'</p></center>';
      }
      else
      {
          // Show failure message
          echo '<center><p>File failed to upload to /'.$name.'</p></center>';
      }
    }
    else
    {
        // Bad File type
        echo '<center><p>The file uses an extension we don\'t allow.</p></center>';
    }
  }

  // Functions do not need to be inline with the rest of the code
  function getNonExistingFilename($uploadFilesTo, $name)
  {
      if (!file_exists($uploadFilesTo . '/' . $name))
          return $name;
    
      return getNonExistingFilename($uploadFilesTo, rand(100, 200) . '_' . $name);
  }
?>
User is offlineProfile CardPM

Go to the top of the page


alecoder
post 14 Jan, 2008 - 04:55 PM
Post #2


New D.I.C Head

*
Joined: 11 Jan, 2008
Posts: 25


My Contributions


Will do refresh page loading starting Again and php script Begins anew
User is offlineProfile CardPM

Go to the top of the page

ap0c0lyps3
post 17 Jan, 2008 - 09:30 AM
Post #3


D.I.C Head

Group Icon
Joined: 19 Jun, 2007
Posts: 75



Dream Kudos: 25
My Contributions


You can use a session that holds the name of the last file uploaded if it is the same as the current file being uploaded then it shows an error.

CODE

if($name == $_SESSION['last_file_name'])
  die('File already uploaded');
User is offlineProfile CardPM

Go to the top of the page

chipshot
post 17 Jan, 2008 - 11:01 PM
Post #4


New D.I.C Head

*
Joined: 30 Apr, 2006
Posts: 38


My Contributions


Thank You,

I'm not much a programmer yet, still learning php.

Question:
what about this line that I have here:

// Create safe filename
$name = ereg_replace('[^A-Za-z0-9.]', '-', $name);

would you take that out, and put in:

if($name == $_SESSION['last_file_name'])
die('File already uploaded');

and ask that the file name must be renamed?

or could they work together somehow?



User is offlineProfile CardPM

Go to the top of the page

ap0c0lyps3
post 19 Jan, 2008 - 06:48 AM
Post #5


D.I.C Head

Group Icon
Joined: 19 Jun, 2007
Posts: 75



Dream Kudos: 25
My Contributions


To tell you the truth. I suck at regex.
As far as I can tell they do completely different things. The first one (I think) makes the file name safe for saving and the other one makes sure that the user doesn't upload the same file twice.

You put
CODE
if($name == $_SESSION['last_file_name'])
die('File already uploaded');

just after
CODE
$name = $_FILES['uploaded_file']['name'];

and at the end of the script you put
CODE
$_SESSION['last_file_name'] = $name
User is offlineProfile CardPM

Go to the top of the page

developerhu
post 21 Jan, 2008 - 03:05 PM
Post #6


New D.I.C Head

*
Joined: 20 Jan, 2008
Posts: 2

easy boys...

lets say you have index.php to upload your video file. After you submit your form, you do the same what now, but instead of echoing out "Your video was successfully uploaded" you place the following line

CODE
header("Location: index.php?cmd=videosuccess");


after the file uploads, the browser redirects to the new page.
To handle the cmd parameter you have to add an extra if to your code

CODE

if(isset($_POST['cmd']) and $_POST['cmd'] == 'videosuccess') {
   echo 'Your video was successfully uploaded';
} else {
   display the form here, and place upload script
}


thats all. After file upload if user hits refresh it only refreshes the result, and does not upload your video again.

hope this helps
dev
User is offlineProfile CardPM

Go to the top of the page

we_undertaker
post 6 May, 2008 - 04:40 AM
Post #7


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 34

anyone can provide some code , that after uploading success, we can playback that video?

i just new in PHP , plzz help me
thank you so much
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 08:52PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month