Jump to content

[SOLVED] File Read Issue


glenelkins

Recommended Posts

Hi

 

Im writing a program to enable a user to select a part of an MP3 song and save it. Now I wrote a quick test before I started on the final app, and that worked fine.

 

Here is the test page code

<?
ob_start();


// Open the mp3
$open_file = fopen( "1.mp3", "r");

// Take first 20 seconds assuming bitrate of 1
/* Time worked out as:
 * ( 128kbits * 1024 / 8 ) * 20 = 320000;
*/

$content = fread( $open_file, 320000 );

fclose( $open_file );

echo ( "<b>WE ARE LOOKING AT 20seconds OF 1.mp3</b>" );
echo ( "<br><br><br>" );
echo ( $content );

$open_file = fopen ( "2.mp3", "w" );

fwrite ( $open_file, $content );

fclose ( $open_file );

//header ( "Location: 2.mp3" );


?>

 

This works perfect, it writes the 2.mp3 file fine with the correct number of seconds. here is the code from the main app. this one when i comes to reading the file seems to go and download the index.php file its running from rather than just opening the mp3 file...any ideas on this?

 

<?php
ob_start();

// Mp3 Splitting

if ( $_GET['aCt'] != "upload" ) {

// Show the user the upload form
$CONTENT = <<<TPL
<form action="index.php?aCt=upload" method="post" enctype="multipart/form-data">
<table border="0" align="center" cellpadding="0" cellspacing="2">
	<tr>
		<td><b>MP3 File: </b></td>
		<td><input type="file" name="mp3file" size="15"></td>
	</tr>
	<tr>
		<td colspan="2" align="center"><input type="submit" value="Upload MP3 File"></td>
	</tr>
</table>
</form>
TPL;

} else {

// Check the file is uploaded

	// debug
	//echo "File Temporary Name: " . $_FILES['mp3file']['tmp_name'] . "<br>";
	//echo "File Name: " . $_FILES['mp3file']['name'] . "<br>";
	//echo "Moving File To: ./tmp/" . $_FILES['mp3file']['name'] . "<br>";

if ( is_uploaded_file ( $_FILES['mp3file']['tmp_name'] ) ) {

		//debug
		//echo "File Upload Success<br>";

	if ( move_uploaded_file ( $_FILES['mp3file']['tmp_name'], "./tmp/" . $_FILES['mp3file']['name'] ) ) {

			//debug
			//echo "File Moved Success<br>";

		$tmp_file = "./tmp/" . $_FILES['mp3file']['name'];

		// Get the file info
		include_once ( "./getid3/getid3/getid3.php" );
		$getID3 = new getID3;
		$fileinfo = $getID3->analyze( $tmp_file );

			// debug
			//echo "<br><br>";
			//echo "File Type: " . $fileinfo['fileformat'] . "<br>";
			//echo "Song Length: mm:ss " . $fileinfo['playtime_string'] . "<br>";
			//echo "Overall bitrate: (kbps) " . $fileinfo['bitrate'] . "<br>";

		// No convert the minutes to seconds
		$tmp_length = explode ( ":", $fileinfo['playtime_string'] );
		$tmp_mins = $tmp_length[0]; // Minutes
		$tmp_secs = $tmp_length[1]; // Seconds

		if ( intval ( $tmp_mins ) > 0 ) {

			$MinsSecs = $tmp_mins * 60;

		} else {

			$MinsSecs = 0;

		}

		$TotalSeconds = $tmp_secs + $MinsSecs;

			//debug
			//echo "<br><br>Total Song Length In Seconds: $TotalSeconds<br>";		

		$CONTENT = "<p style='color: red; font-weight: bold;'>File Information</p>";
		$CONTENT .= "<p>
						<b>File Name: </b>" . $_FILES['mp3file']['name'] . "<br>
						<b>Song Length In Seconds: </b>$TotalSeconds
					</p>";
		$CONTENT .= "<p style='color: red; font-weight: bold;'>Sample Selection</p>";

		// Generate a list of seconds
		for ( $i = 0; $i <= $TotalSeconds; $i++ ) {

			$START_POINT .= "<option value='$i'>$i</option>";

		}

		$CONTENT .= "<form action='index.php?aCt=select' method='post'>
						<b>Starting Point ( in seconds ): </b><select name='start'>$START_POINT</select><br>
						<b>Length ( in seconds ): </b><select name='length'>$START_POINT</select><br>
						<input type='submit' value='Cut Selection From Song'>
						<input type='hidden' name='filename' value='$tmp_file'>
						<input type='hidden' name='bitrate' value='$fileinfo[bitrate]'>
					</form>";

	} else {

			//debug
			echo "File Move Error<br>";

		$CONTENT = "<span style='color: red;'>File Move Error</span><br>";

	}


} else {

		//debug
		echo "File Upload Error<br>";

	$CONTENT = "<span style='color: red'>There Has Been An Error Uploading Your File</span><br>";

}

}

if ( $_GET['aCt'] == "select" ) {

// Open the mp3 file for reading
$filename = $_POST['filename'];
$start = $_POST['start'];
$length = $_POST['length'];
$bitrate = $_POST['bitrate'];

	echo "Reading File: $filename<br>";
	echo "Start: $start<br>";
	echo "Length: $length<br>";
	echo "Bitrate: $bitrate<br>";

// First work out how much to read from the bitrate
// and length
$Bits = ( $bitrate * 1024 ) / 8;
$Bits = $Bits * intval ( $length );

	// debug
	echo "Bits: $Bits<br>";

// Open the file
$open_file = fopen ( "$filename", "r" );

// Read file
$file_content = fread ( $open_file, $Bits );

// close file
fclose ( $open_file );

	//debug
	echo "<br>Content Of The File<br><br>";
	echo $file_content;

}


echo ( $CONTENT );

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.