Jump to content

PHP header, no error, just doesnt work right...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
..actually it prob is working right, im just messing it up..

VastBrowser

Upload a text file, it will then give you a download link, download it..

.. You will see that it actually adds my HTML from the page, to your .txt file..

here is my exact code

			if($allow == true){

				$type = mime_content_type("/home/vastbrow/uploads/$file");

				// We'll be outputting the correct type

				header('Content-type: '.$type);


				// It will be called the correct name

				header('Content-Disposition: attachment; filename="'.$file.'"');


				// The source is in original location

				readfile("/home/vastbrow/uploads/$file");

			}

Checkout my new forum! http://adminreference.com/

#2
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
I tried this:

			if($allow == true){
				$type = mime_content_type("/home/vastbrow/uploads/$file");
				// We'll be outputting a PDF
				header('Content-type: '.$type);

				// It will be called downloaded.pdf
				header('Content-Disposition: attachment; filename="'.$file.'"');
				
				header('Content-Length: '. filesize($file)); 

				// The PDF source is in original.pdf
				readfile("/home/vastbrow/uploads/$file");
			}

Thinking it needed a file size, but it threw an error..

Quote

Warning: filesize() [function.filesize]: stat failed for 28.autopwn.txt in /home/vastbrow/public_html/download.php on line 111

Checkout my new forum! http://adminreference.com/

#3
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
I think its just text files....

MP3 File: VastBrowser

.TXT file: VastBrowser
Checkout my new forum! http://adminreference.com/

#4
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
im havin fun with this!
Check out my fav song:
1.Rise Against- Audience Of One.mp3
Checkout my new forum! http://adminreference.com/

#5
Guest_Jordan_*

Guest_Jordan_*
  • Guests
When you upload the text file, can you edit it from the OS and it have the right contents?

#6
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
Yes sir, the error is for sure on download
Checkout my new forum! http://adminreference.com/

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I had this same problem with ionFiles. First, turn off errors so you don't pipe errors into the download. Use the @ or error_report(0); do to this:

@readfile()

Second, you need to exit after the readfile. Depending on your template system this may or may not help you. In Joomla! if you didn't exit it would parse the website at the header of the file. Here is the code I used for ionFiles download (which now works well):

		header("Pragma:no-cache");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Cache-Control: public");
		header("Content-Description: File Transfer");
		header("Content-type: $mtype");
		header("Content-Transfer-Encoding: binary"); 
		header("Content-Disposition: attachment; filename=\"".basename($file)."\"");
				

		if (ini_get('allow_url_fopen')) {
		  	if (extension_loaded('curl')) {
		  		header("Content-Length: " . remoteFileSize($file));
		  	}
		}
		
		header("Accept-Ranges: bytes");
		@readfile("$file");

		/**
		 * Exit so that Joomla does not output
		 * the header and footer inside of our
		 * file.
		 */
		exit();

$mtype is the mime type and the variable gets assigned from this function:

/**
 * Fetches the mime type of a file by 
 * using the extension and finding it's value in a
 * hard-coded array.
 *
 * @param string $fext
 * @return string
 */
function getMimeType($fext) {

# Mime Types
	#-------------------------------
	$mine_types = array (
			
	# archives
	#-------------------------------
	 'zip'		=> 'application/zip',

	# documents
	#-------------------------------
	'pdf'		=> 'application/pdf',
	'doc'		=> 'application/msword',
	'xls'		=> 'application/vnd.ms-excel',
	'ppt'		=> 'application/vnd.ms-powerpoint',

	# executables
	#-------------------------------
	'exe' 	=> 'application/octet-stream',

	# images
	#-------------------------------
	'gif' 		=> 'image/gif',
	'png'		=> 'image/png',
	'jpg'		=> 'image/jpeg',
	'jpeg'	=> 'image/jpeg',

	# audio
	#-------------------------------
	'mp3'	=> 'audio/mpeg',
	'wav'		=> 'audio/x-wav',

	# video
	#-------------------------------
	'mpeg'	=> 'video/mpeg',
	'mpg'	=> 'video/mpeg',
	'mpe'	=> 'video/mpeg',
	'mov'	=> 'video/quicktime',
	'avi'		=> 'video/x-msvideo'
	
	);

	# Find the Mime Type
	#-------------------------------
	if (@$mine_types[$fext] == '') {
		$mtype = '';
	
		# mime type is not set, get from server settings
		#-------------------------------
		if (function_exists('mime_content_type')) {
		  $mtype = mime_content_type($file);
		}
		else 
		{
		  $mtype = "application/force-download";
		}
	
	}
	else
	{
		# get mime type defined by admin
		#-------------------------------
		$mtype = $mine_types[$fext];
	}
	
	# Return our Type
	#-------------------------------
	return $mtype;
}

You can use it or not but it does help identify the files for browsers.

#8
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
That works perfectly! Thanks!
Checkout my new forum! http://adminreference.com/

#9
Guest_Jordan_*

Guest_Jordan_*
  • Guests
No problem. The same problem about drove me crazy when I found it in ionFiles.