Jump to content

Getting rid of new lines in a textfile


s0l1dsnak3123

Recommended Posts

Hi there, I am working on a script whereby the user would upload a music file, then it would be checked for duplicates via various ways (same name, MD5 hashes, ID3 tags, etc.) I am currently having problems storing my text file lists of MD5s and ID3 info. The problem I keep having is that the script sometimes doesn't detect the MD5s/ID3 tags correctly in the list. I believe that the way that the file is written is the culprit, as sometimes there are odd newlines (that I have tried to strip) that find themselves into the file.

 

Here is my code:

config.php:

<?php
$request_uploads = "/opt/lampp/htdocs/shoutcast webUI/request_uploads/";
$md5_list = "md5_list";
$id3info_list = "id3info_list";
$delimiter = "###";
define("REQUEST_UPLOADS", $request_uploads);
define("MD5_LIST", $md5_list);
define("DELIMITER",$delimiter);
define("ID3INFO_LIST",$id3info_list);
?>

 

<?php require_once("config.php"); ?>
<?php require_once("header.php"); ?>

<div id="top"><span class="title">Shoutcast UI</span></div>
<div id="body">
<span class="h1">Output:</span>
<br /><br />
<?php
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "mp3") && ($_FILES["uploaded_file"]["type"] == "audio/mpeg")) {
    //Determine the path to which we want to save this file
      $newname = REQUEST_UPLOADS.strtolower(str_replace(" ", "_", str_replace("'", "", str_replace("\\","", $filename))));
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
   echo "Creating MD5 hash to check for duplicates...";
           $md5 = md5_file($newname); //create an MD5sum of the uploaded file, to check for duplicates
		$fp = file_get_contents (REQUEST_UPLOADS . MD5_LIST); 
			$fp = explode(DELIMITER, $fp);
			foreach ($fp as &$value) {
				if ( $value == $md5 ) {
					unlink($newname);
					die("<b>" . htmlspecialchars($filename) . " matches with " . htmlspecialchars($value) . " on our system. This means we already have this file. </b><br />");
				}	
			}
			echo "[None found] <br />";
		require_once('./getid3/getid3.php'); //require the id3 class...
		echo "Analyzing MP3's ID3 tags to check for duplicates...";
		$getID3 = new getID3;
		set_time_limit(30);
		$FileInfo = $getID3->analyze($newname);
		getid3_lib::CopyTagsToComments($FileInfo);

		//creates a string with the title, artist, and bitrate, seperated by "*&"s.
		$id3tags = (!empty($FileInfo['comments_html']['title'])  ? implode('<BR>', $FileInfo['comments_html']['title'])  : ' ')."*&".(!empty($FileInfo['comments_html']['artist']) ? implode('<BR>', $FileInfo['comments_html']['artist']) : ' ')."*&".(!empty($FileInfo['audio']['bitrate']) ? round($FileInfo['audio']['bitrate'] / 1000)   : ' ');

		$fp = file_get_contents (REQUEST_UPLOADS.ID3INFO_LIST);  //get list of ID3s in the database
			$fp = explode(DELIMITER, $fp); //seperate
			$id3array = explode("*&", $id3tags);
			foreach ($fp as &$value) { //for each song
				$stat = explode("*&", $value); // explode into stats about song
				$strike=0; //2 strikes and it's out, unless it's better quality.
				if ( $stat[0] == $id3array[0] ) {
					echo "<br />We have a song with the same title...<br />";
					$strike++;
				}
				if ( $stat[1] == $id3array[1] ) {
					echo "<br />We have a song from the same artist... <br />";
					$strike++;
				}
				if ( $strike >= 2 ) {
					if ( $stat[2] > $id3array[2] ) { //check if it is better quality
						die("Although we already have this song, your version is better quality, so it has been added, and the Admin has been notified of a suggested replacement.");
					}
					else {
					unlink($newname);
					die("We found a match for this song in our ID3 database which is of equal or better quality.");
					}
				}
			}


		echo "[ok]<br />";
		echo "Title: ".(!empty($FileInfo['comments_html']['title'])  ? implode('<BR>', $FileInfo['comments_html']['title'])  : ' ')."<br/>";
		echo "Artist: ".(!empty($FileInfo['comments_html']['artist']) ? implode('<BR>', $FileInfo['comments_html']['artist']) : ' ')."<br/>";
		echo "Audio bitrate: ".(!empty($FileInfo['audio']['bitrate'])        ? round($FileInfo['audio']['bitrate'] / 1000).' kbps'   : ' ')."<br/>";

		//adding to ID3tag database
		$fp = fopen(REQUEST_UPLOADS.ID3INFO_LIST, 'a');
		$id3tags = str_replace("\r","",str_replace("\n","",trim($id3tags))); //try to filter out newlines
		fwrite($fp,$id3tags.DELIMITER);
		fclose($fp);
		//adding md5 to list
		$fp = fopen(REQUEST_UPLOADS.MD5_LIST, 'a');
		$md5 = str_replace("\r","",str_replace("\n","",trim($md5))); //try to filter out newlines
		fwrite($fp,$md5.DELIMITER); //add the md5 to the list
		fclose($fp);
		echo "<br />" . "<br />" . "<br />" . $tag . "<br />" . "<br />" . "<br />";
		echo "The file has been saved as: ".$newname;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Checking for duplicates... <b>Error: File ".$_FILES["uploaded_file"]["name"]." already exists</b>";
      }
  } else {
     echo "Checking extension and mime-type: <b>Error: Only .mp3s are allowed</b>";
  }
} else {
echo "<b>Error: No file uploaded</b>";
}

echo "<br />Taking you back <a href=\"index.php\">home...</a>";
//echo "<meta http-equiv=\"refresh\" content=\"10;url=index.php\">";
?>
</div>

<?php require_once("footer.php"); ?>

 

Thanks for help in advance,

John H

Link to comment
https://forums.phpfreaks.com/topic/119816-getting-rid-of-new-lines-in-a-textfile/
Share on other sites

Can you show us what the input file looks like.

 

Just a comment on your code. Instead of doing:

<?php
$newname = REQUEST_UPLOADS.strtolower(str_replace(" ", "_", str_replace("'", "", str_replace("\\","", $filename))));
?>

you can do

<?php
$newname = REQUEST_UPLOADS.strtolower(str_replace(array(" ","'","\\"),array("_","",""), $filename)));
?>

 

Ken

Hi there, here is an example of the input that is generated by the script: This is for the song "Boom!" by System of a Down, with a bitrate of 320Kbps:

 

md5_list:


5861aec84cd1f137f50eeb98ca9e3383###

 

id3info_list:


BOOM!*&System Of A Down*&320###

 

Notice the Newline before the text on both pages.

Archived

This topic is now archived and is closed to further replies.

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