FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Download database.





[FuN]goku
Right ne one know how to make a script using mysql, that you can store download links in and then call them like...
example.com/download.php?id=1 etc, and it automatically starts.... sort of the idea of php nuke, and stuff like that.
Any help is much appreciated.
Thanks.
hexkid
[FuN]goku wrote:
Right ne one know how to make a script using mysql, that you can store download links in and then call them like...
example.com/download.php?id=1 etc, and it automatically starts....
The information you need to send a file to clients is the whereabouts of that file on the server. So, you need to save it to the database along with an id.

Get the location from the database based on the id in the URL.
Send the file

Code:
<?php
// connect to DB
$id = (int)$_GET['id'];
if ($id < 1) {
  exit('Invalid id.');
}
$sql = "select whereabout from downloadable_files where id=$id";
$res = mysql_query($sql);
if (!$res) {
  // error in query. For simplicity sake I'll just exit
  exit('Query error: ' . mysql_error());
}
if (mysql_num_rows($res) != 1) {
  // No id found in the database. For simplicity sake I'll just exit
  exit('id not found.');
}
$location = mysql_result($res, 0);
mysql_free_result($res);

clearstatcache();
if (is_file($location)) {
  if (is_readable($location)) {
    header('Content-Type: ' . mime_content_type($location));
    readfile($location);
    exit(0); ## done!
  } else {
    // file is not readable. For simplicity sake I'll just exit
    exit('File is not readable.');
  }
} else {
  // file does not exist. For simplicity sake I'll just exit
  exit('File does not exist.');
}
?>


For an upload script, you'll need to use move_uploaded_file(). Check the manual, including user comments, for usage examples.
[FuN]goku
im slightly confused.... im a php noob so i usually only understand something if i write it myself XD... so waht would the sql tables be just so i dont mess it up lol.

EDIT: or what if i used this for sql

Code:

CREATE TABLE `downloads` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`file` VARCHAR( 255 ) NOT NULL ,
`name` VARCHAR( 50 ) NOT NULL
) ENGINE = MYISAM ;

file being the link and name being the Name to display on the downloads page. could you work somthing out of that?
hexkid
[FuN]goku wrote:
im a php noob so i usually only understand something if i write it myself
That's a very good way to learn.

[FuN]goku wrote:
what if i used this for sql
That is a suitable table.

[FuN]goku wrote:
could you work somthing out of that?
I'll let you do it Smile

You need two (three?) things:
a) a list of downloadable files for the user to choose from
b) the download script
c) (?) an upload script

For a) you'll build links like
Code:
<a href="download.php?id=42">fortytwo.zip</a>
The id number and file number come from the database with an SQL instruction like
Code:
select id, name from downloads order by name limit 0, 20


For b) adapt my script above.

For c) you'll need to create a form with an <input type="file">. After processing the form, to update the database you can use something like
Code:
insert into downloads (file, name) values ('/path/to/uploaded/file', 'descriptive name')



Do write your own scripts. If you get stuck on something (check the manual and) post your problems here.
Happy Coding
[FuN]goku
well could u explain something im a bit confused....

Quote:

$id = (int)$_GET['id'];
if ($id < 1) {
exit('Invalid id.');
}
$sql = "select whereabout from downloadable_files where id=$id";


mainly the part of the sql query... what would i put in to fit my script.
"select file from downloads where id=$id"
or... cuz that didnt work for me
hexkid
[FuN]goku wrote:
"select file from downloads where id=$id"
That looks right.

How didn't it work for you?

Was there an error message? What was it?
The result was 'zip.zip', but you were expecting 'tar.tar'?

Give us something to work with. You're the only person in the whole internet with your specific "downloads" table in a database. I know the table can be recreated on my machine (thank you for the database code), but I will not go to that trouble (and I guess not many people would), so you have to tell us how it didn't work. You have to tell us what you were expecting (and why) and what the result was. You have to tell us the pertinent messages that show up. You have to show the code you're running.
Sometimes we can guess, but sometimes we guess wrong which is a waste of everybody's time.

In short, make it easy for us to help you.
[FuN]goku
right well say my site was example.com... i tried to visit it like so.
http://example.com/download.php?id=1
and it just says
Quote:
File does not exist.

but i have inserted a few things to test but it just says file doesnt exist.
hexkid
[FuN]goku wrote:
right well say my site was example.com... i tried to visit it like so.
http://example.com/download.php?id=1
and it just says
Quote:
File does not exist.

but i have inserted a few things to test but it just says file doesnt exist.
Ok, that's better Smile

So your problem lies here
Code:
// ...
$location = mysql_result($res, 0);
mysql_free_result($res);

clearstatcache();
if (is_file($location)) { #### HERE!!!!! $location is not a file
// ...
} else {
  // file does not exist. For simplicity sake I'll just exit
  exit('File does not exist.');
}


Let's debug ... try this change in that part of the code
Code:
// ...
$location = mysql_result($res, 0);

#### DEBUGGING
echo 'DEBUGGING $location is ' . $location . "<br>\n";

mysql_free_result($res);

clearstatcache();
if (is_file($location)) {
// ...
} else {
  // file does not exist. For simplicity sake I'll just exit
  exit('File does not exist.');
}
and verify that $location really exists on your system.
[FuN]goku
Quote:

DEBUGGING $location is http://mysite.com/test.zip
File does not exist.

is what it gave me. im not using the real path to my zip above, but it does show to the real path of where it is on my page.
hexkid
[FuN]goku wrote:
Quote:

DEBUGGING $location is http://mysite.com/test.zip
File does not exist.

is what it gave me. im not using the real path to my zip above, but it does show to the real path of where it is on my page.

Well, if your file is accessible through a URL, why go to the trouble of making a download script? Instead of linking to http://example.com/download.php?id=1 link directly to http://example.com/test.zip

Anyway, as far as PHP is concerned, that URL really is not a file. If you want PHP to first download the file to the server (why? it's already there; there's no need to create a copy) and then send it to the user, the script has to be rewritten; it will never work with URLs.

Try updating your database to real file system paths (C:/Uploads/test.zip or /home/fungoku/uploads/test.zip or whatever).
[FuN]goku
ermmm i want it because i want a download page on my site and then just call all of the id's from the mysql and have a list of all the downloads instead of adding em manually for 1, and 2 i want it so it kinda masks the file location. basically i want it so only registered users on my site can download it when logged in.
hexkid
[FuN]goku wrote:
basically i want it so only registered users on my site can download it when logged in.

So you want "test.zip" accessible in two ways:
a) http://example.com/download.php?id=1
b) http://example.com/test.zip

Your registered users would download with option a) and you unregistered users would download with option b) Smile

Yes, I know the option b) URL would not be publicized or written anywhere on your site. Imagine one of your users is very imaginative and tries a lot of
http://example.com/agora.zip
http://example.com/bofh.zip
http://example.com/firewall.zip
http://example.com/test.zip
...
until (s)he gets a match (and tells all (her)his friends about it).

Wouldn't it be better to only allow option a) downloads?
That was the basic assumption for my script a few posts above.
[FuN]goku
unregistered users dont get to downlaod at all , only registered ones. ne ways this thingy still aint working -_-
hexkid
[FuN]goku wrote:
unregistered users dont get to downlaod at all , only registered ones. ne ways this thingy still aint working -_-
What if an unregistered user types "http://example.com/test.zip" in the browser's address bar? Will the user be prompted to save the file?

The fact that there is no visible link to that URL does not mean it will never be used.

And you're, again, saying "it doesn't work".
How does it not work?
Did you update the database? Does the "DEBUGGING" line still print
Quote:
DEBUGGING $location is http://mysite.com/test.zip
File does not exist.


Ps. I'm getting used to "ne" for any, but I had a very hard time understanding it the first time you wrote it. Maybe I should start answering with my own set of abbreviations Smile
[FuN]goku
hexkid wrote:
[FuN]goku wrote:
unregistered users dont get to downlaod at all , only registered ones. ne ways this thingy still aint working -_-
What if an unregistered user types "http://example.com/test.zip" in the browser's address bar? Will the user be prompted to save the file?

The fact that there is no visible link to that URL does not mean it will never be used.

And you're, again, saying "it doesn't work".
How does it not work?
Did you update the database? Does the "DEBUGGING" line still print
Quote:
DEBUGGING $location is http://mysite.com/test.zip
File does not exist.


Ps. I'm getting used to "ne" for any, but I had a very hard time understanding it the first time you wrote it. Maybe I should start answering with my own set of abbreviations Smile
heheh uhhm i know it wont stop them from guessing the path but if i make an upload script ill make it generate random names for the zips or whatever the file is.
[FuN]goku
bump >.<
hexkid
[FuN]goku wrote:
bump >.<


If you really want your files accessible through HTTP, just remove the "if (is_file()) {}" check (I strongly recommend you leave it in).

How does it not work?
[FuN]goku
ermmm still does the
Quote:

DEBUGGING $location is http://mysite.com/test.zip
File does not exist.
hexkid
[FuN]goku wrote:
ermmm still does the
Quote:

DEBUGGING $location is http://mysite.com/test.zip
File does not exist.


Read this again:
http://www.frihost.com/forums/vp-478222.html#478222

Particularly the two last lines.
[FuN]goku
Quote:

/home/fungoku/uploads/test.zip

right so... if i got an account on a cpanel server (no not my frihost one.)
would it be like
Quote:

/home/username_here/public_html/test.zip


EDIT: right if i try the thing i just did above with the username_here and what not, i get this error when i call its id
Quote:

Fatal error: Call to undefined function: mime_content_type() in /home/xxxxx/public_html/download.php on line 44

BUT! thats only when i use
/home/username_here/public_html/test.zip

but if i use direct link it does nothing just says file doesnt exist.
hexkid
[FuN]goku wrote:
Quote:
Fatal error: Call to undefined function: mime_content_type() in /home/xxxxx/public_html/download.php on line 44

This means your server is using a PHP before version 4.3 (or that your server configuration doesn't allow you to call the function); you might be able to substitute it with one of the replacements suggested in the user notes at the mime_content_type() manual page.

[FuN]goku wrote:
BUT! thats only when i use
/home/username_here/public_html/test.zip

but if i use direct link it does nothing just says file doesnt exist.

Right! I'm not going to help you write a script that allows someone to download from a script or directly from a URL (any more than I already did).
You really should move your files to outside the public_html directory.
[FuN]goku
>.< ermm i only have that file in public_html for testing reasons. i never put files in there for non-testing reasons.
hexkid
[FuN]goku wrote:
>.< ermm i only have that file in public_html for testing reasons. i never put files in there for non-testing reasons.

I mean completely out of public_html.

This is not ok: /home/xxxxx/public_html/rand764537452/rand07327532/rand745364523/test.zip

This is ok
/home/xxxxx/uploads/test.zip
[FuN]goku
hexkid wrote:
[FuN]goku wrote:
>.< ermm i only have that file in public_html for testing reasons. i never put files in there for non-testing reasons.

I mean completely out of public_html.

This is not ok: /home/xxxxx/public_html/rand764537452/rand07327532/rand745364523/test.zip

This is ok
/home/xxxxx/uploads/test.zip

ahh i see wot u mean.
googliz
So you want "test.zip" accessible in two ways:
a) http://example.com/download.php?id=1
b) http://example.com/test.zip

Your registered users would download with option a) and you unregistered users would download with option b)

Yes, I know the option b) URL would not be publicized or written anywhere on your site. Imagine one of your users is very imaginative and tries a lot of
http://example.com/agora.zip
http://example.com/bofh.zip
http://example.com/firewall.zip
http://example.com/test.zip
...
until (s)he gets a match (and tells all (her)his friends about it).

Wouldn't it be better to only allow option a) downloads?
That was the basic assumption for my script a few posts above.
_________________
First thing to change when you have an error in a PHP script is to increase the level of error reporting. Add this line right after your first php tag.Code:
error_reporting(E_ALL); ini_set('display_errors', '1');
hexkid
googliz wrote:
big unquoted "quote"

Please quote your quotes. Thank you.
[FuN]goku
nvm about download db i got someone else to do it.
Related topics
script backup database
Importing an existing database...
UniqueGfx.com - If you want to earn money or .com domain ...
Changing Hosts, and download/restoring databases.
BeerSuckers :P
Forum Board without database
my HUGE download site
Automatic download start !!
php download system prob
How to backup competely?
transfer of joomla website incl database
Download System Help
Download option instead of display
connect android app to mySQL database(5 steps)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.