Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a PHP Expert!

Join 414,938 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,680 people online right now.Registration is fast and FREE... Join Now!



Page 1 of 1

video upload script Rate Topic: -----

#1 chipshot  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 30-April 06


Dream Kudos: 0

Share |

video upload script

Post icon  Posted 13 January 2008 - 10:14 PM

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

		<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);
  }
?>

Was This Post Helpful? 0
  • +
  • -


#2 alecoder  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 11-January 08


Dream Kudos: 0

Re: video upload script

Posted 14 January 2008 - 03:55 PM

Will do refresh page loading starting Again and php script Begins anew
Was This Post Helpful? 0
  • +
  • -

#3 ap0c0lyps3  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 19-June 07


Dream Kudos: 25

Re: video upload script

Posted 17 January 2008 - 08:30 AM

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.

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


Was This Post Helpful? 0
  • +
  • -

#4 chipshot  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 30-April 06


Dream Kudos: 0

Re: video upload script

Posted 17 January 2008 - 10:01 PM

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?
Was This Post Helpful? 0
  • +
  • -

#5 ap0c0lyps3  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 2
  • View blog
  • Posts: 82
  • Joined: 19-June 07


Dream Kudos: 25

Re: video upload script

Posted 19 January 2008 - 05:48 AM

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
if($name == $_SESSION['last_file_name'])
die('File already uploaded');

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

and at the end of the script you put
$_SESSION['last_file_name'] = $name

Was This Post Helpful? 0
  • +
  • -

#6 developerhu  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-January 08


Dream Kudos: 0

Re: video upload script

Posted 21 January 2008 - 02:05 PM

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

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

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
Was This Post Helpful? 0
  • +
  • -

#7 we_undertaker  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 06-May 08


Dream Kudos: 0

Re: video upload script

Posted 06 May 2008 - 03:40 AM

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
Was This Post Helpful? 0
  • +
  • -

#8 hirensoni2005  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-March 09


Dream Kudos: 0

Re: video upload script

Posted 04 June 2009 - 09:55 PM

To solve this problem you just need to redirect your page on the same page with "header("location:yourfile.php")"

		<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>';
	}
  header("location:yourfile.php");
  }
 
  // 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);
  }
?>

[/quote]
Was This Post Helpful? 0
  • +
  • -

#9 Guest_ashok.dubbaka@gmail.com*


Reputation:

Re: video upload script

Posted 10 March 2010 - 04:14 AM

first you have to check if the video file name is already exists in that you can not upload the video if it is not there with same name then you can upload video file
Was This Post Helpful? 0

#10 Guest_liyaan*


Reputation:

Re: video upload script

Posted 18 March 2010 - 01:22 AM

you can create another php file that do the actual uploading. name it process.php and call it using <quote><form action="process.php"></quote>. inside process.php, add <quote>header("Location: yourfile.php");</quote> to redirect your code to the original php file.
Was This Post Helpful? 0

#11 Valek  Icon User is offline

  • The Real SkyNet
  • Icon

Reputation: 279
  • View blog
  • Posts: 1,177
  • Joined: 08-November 08


Dream Kudos: 175

Expert In: PHP, MySQL,HTML/CSS. Javascript,AJAX

Re: video upload script

Posted 18 March 2010 - 08:46 AM

View Postdeveloperhu, on 21 January 2008 - 05:05 PM, said:

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

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

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


I think you mean $_GET['cmd']. Using $_POST['cmd'] will fail unless you add it as a hidden input on your form. You've added it to the query string.
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Rama-llama-ding-dong
  • Icon

Reputation: 1512
  • View blog
  • Posts: 11,205
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

Re: video upload script

Posted 18 March 2010 - 09:53 AM

OK, time to close the necro topic. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users