Jump to content

[SOLVED] Encoding URLS


hellonoko

Recommended Posts

:'(y URLS with spaces fail to be processed by fopen fread fwrite etc.

 

EX:

www.trashmenagerie.com/audio/Freak You/Freak You - From Nowhere.mp3

 

However if I paste this link into my browser it becomes:

 

http://www.trashmenagerie.com/audio/Freak%20You/Freak%20You%20-%20From%20Nowhere.mp3

 

And works fine.

 

Which of the url encoding functions do I need to be using to fix these urls before I process them?

 

Thanks

 

Link to comment
Share on other sites

You only encode the string that you're adding.

 

www.trashmenagerie.com/audio/Freak You/Freak You - From Nowhere.mp3

 

If that's the link, then at some point you've added 'Freak You - From Nowhere.mp3'

 

$url = "www.trashmenagerie.com/audio/Freak You/";
$file = urlencode("Freak You - From Nowhere.mp3");
echo "<a href=\"$url$file\">this is the link</a>";

Link to comment
Share on other sites

Basically the links are in the DB as links without http://

In whatever format they where crawled from a page in but put into the DB with mysql_real_escape_string();

 

<?php
//get the link to copy from the DB
	$query = "SELECT * FROM `mp3links` WHERE `scraped` = 0 LIMIT 1";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result) or die (mysql_error());

	$url = "http://".$row[link];
	$last_slash = strripos( $url ,"/");
	$clean_file_name = substr( $url , $last_slash + 1 , strlen($url) );


	// do a file copy
	//$url = "http://www.beachtitti.com/summermix_01.mp3";
	$dirname = "/home2/sharingi/public_html/scrape/scraped/";

	// close th mysql connection because a large file copy may time out mysql
	mysql_close($connection);

	$result = copyFile($url , $dirname);

?>

<?php
function copyFile($url,$dirname)
{

//$url = urlencode($url);

@$file = fopen ($url, "rb");
if (!$file) 
{
	echo "Failed to copy $url !";

	return false;
}
else 
{	
	//copy file
	$filename = basename($url);
	$fc = fopen($dirname."$filename", "wb");
	while (!feof ($file)) 
	{
		$line = fread ($file, 1028);
		fwrite($fc,$line);
	}

	fclose($fc);
	//echo "File $url saved to PC!";

	$time_end = microtime(true);
	$time = $time_end - $time_start;

	if ( $time > 60 )
	{
		$time = $time / 60;
		echo "File: $filename coppied in $time minutes";
	}
	else
	{
		echo "File: $filename coppied in $time seconds";
	}

	return true;
}
}
?>

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.