C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 24th, 2006, 05:37 PM
thejdog thejdog is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 39 thejdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 42 m 15 sec
Reputation Power: 6
Send a message via AIM to thejdog
Outputting file through web browser

Ok, so I'm making a script that connects to the database, get's the file info from the db (path & file name), it gets the mime type form the database too.

Now the problem is sending that info to the web browser so they can view movies through Windows Media/IE.

Here's the code I can't get right (full code below):

Code:
	/* FILE VARIABLES */
    char filename[255];
	FILE *videofile;
	char buffer[1024];	

	int videosize = atoi(row[3]);

	/* SEND HEADERS */
	printf("Content-Type: %s\n",row[2]);
	printf("Content-Length: %i\n\n",videosize); 

	/* BULID FULL FILENAME FROM DB ROW */
	sprintf(filename,"%s/%s",row[1],row[0]);

	mysql_free_result(res);
	mysql_close(conn);

	videofile = fopen(filename,"r");

	while(fgets(buffer,1024,videofile)!=NULL) printf("%s",buffer);

	fclose(videofile);
	free(buffer);


The full code:
Code:
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <getval.h>

main (int argc, char *argv[]) {

	/* SET MYSQL VARIABLES */
	MYSQL     *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char sqlbuff[255];

	/* SET MYSQL CONNECTION VARIABLES */
	char *server   = "dbserver";
	char *user     = "username";
	char *password = "password";
	char *database = getval("dbname");

	/* SET VIDEOID VARIABLE */
	char *str1     = getval("videoid");
	int  VideoId   = atoi(str1);

	conn = mysql_init(NULL);

	/* CONNECT TO MYSQL DATABASE */
	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
		printf("Content-type: text/html\n\n");
		fprintf(stderr, "%s\n", mysql_error(conn));
		exit(0);
	}

	/* Do QUERY */
	sprintf(sqlbuff, "SELECT FileName,FilePath,MimeType,FileSize FROM ContentAV WHERE Id = \'%i\'", VideoId);
	if(mysql_query(conn, sqlbuff)) {
		printf("Content-type: text/html\n\n");
		fprintf(stderr, "%s\n", mysql_error(conn));
		exit(0);
	}

	res = mysql_use_result(conn);
	row = mysql_fetch_row(res);

	/* FILE VARIABLES */
    char filename[255];
	FILE *videofile;
	char buffer[1024];	

	int videosize = atoi(row[3]);

	/* SEND HEADERS */
	printf("Content-Type: %s\n",row[2]);
	printf("Content-Length: %i\n\n",videosize); 

	/* BULID FULL FILENAME FROM DB ROW */
	sprintf(filename,"%s/%s",row[1],row[0]);

	mysql_free_result(res);
	mysql_close(conn);

	videofile = fopen(filename,"r");

	while(fgets(buffer,1024,videofile)!=NULL) printf("%s",buffer);

	fclose(videofile);
	free(buffer); 
}


Thanks for any help! I've been sitting here for a while

Reply With Quote
  #2  
Old August 25th, 2006, 12:55 AM
salem's Avatar
salem salem is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2005
Posts: 2,026 salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)salem User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 1 Month 2 Weeks 2 h 57 m 12 sec
Reputation Power: 839
> char buffer[1024];
> free(buffer);
You can't free what you didn't malloc.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour

Reply With Quote
  #3  
Old August 25th, 2006, 03:42 AM
thejdog thejdog is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 39 thejdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 42 m 15 sec
Reputation Power: 6
Send a message via AIM to thejdog
Quote:
Originally Posted by salem
> char buffer[1024];
> free(buffer);
You can't free what you didn't malloc.


I'm still very lost on this. For some reason it just isn't working right. It gets to the last few hundred bites and the download stops, or sometimes it only get's half way.

New code:

Code:
        /* FILE VARIABLES */
        char filename[255];
        FILE *videofile;
        int  buffersize = 2;
        char buffer[buffersize];
        int  videosize = atoi(row[3]);

        /* SEND HEADERS */
        printf("Content-Type: %s\n",row[2]);
        printf("Content-Disposition: attachment; filename=%s\n",row[0]);
        printf("Content-Length: %i\n\n",videosize);

        /* BULID FULL FILENAME FROM DB ROW */
        sprintf(filename,"%s/%s",row[1],row[0]);

        mysql_free_result(res);
        mysql_close(conn);

        videofile = fopen(filename,"rb");

/*      while(fgets(buffer,1024,videofile)!=NULL) printf("%s",; */
        int i=0;
        while(i <= videosize) {
                fgets(buffer, buffersize, videofile);
                fputs(buffer, stdout);
                i=i+buffersize;
        }

        fclose(videofile);

Reply With Quote
  #4  
Old August 25th, 2006, 04:39 AM
ZuK ZuK is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2005
Posts: 580 ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level)ZuK User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 9 h 47 m 23 sec
Reputation Power: 92
You call it videofile but treat it like a text-file.
Code:
        int i=0;
        while(i <= videosize) {
                fgets(buffer, buffersize, videofile); // read a line of text
                fputs(buffer, stdout);                // print that line. Is it really text ?
                i=i+buffersize;                       // add buffersize 
                                                      // but that buffer is most propably not filled completly
                                                      // fgets stops at newline
        }

Guess you have to read the file in binary mode and use fread().
Looks your are trying to output to stdout because the webserver reads from stdin. I have never tried but I think you will have to switch stdout to binary mode as well.
I might be wrong, fwrite() could work.
Kurt
Comments on this post
salem agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Outputting file through web browser


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT