Jump to content

GET url from ID


Irishph4nt0m16

Recommended Posts

Hi, I have this code that removes unnecessary code from a file and I'm wanting to request the url associated with the uniqid. So instead of my select box containing the dir to the files, it will request the url by the uniqid.

$h = fopen("cache/form.txt", "rt");
  
  while (!feof($h)) {
  $title = trim(fgets($h));
  $url = trim(fgets($h)); 
  $doesnt_start_with_numbers = preg_replace('#^\d+#', '', $title);
  
// output
 
  $up_id = uniqid();
  $print = print "<option id='$up_id' value='$url'>$doesnt_start_with_numbers</option>\n";
}
fclose($h);

Any help on this would be great.

Link to comment
Share on other sites

You are not providing enough information for anyone to help you with this.

 

First off unique() is a function that returns a unique value.  How is this of use to you in this scenario?

 

Obviously it would be simple enough to change this line:

 

 

$print = print "<option id='$up_id' value='$url'>$doesnt_start_with_numbers</option>\n";

 

To this:

 

 

$print = print "<option id='$up_id' value='$up_id'>$doesnt_start_with_numbers</option>\n";

 

However, I don't see how that would allow anyone to select or identify a specific file from the drop down list.

Link to comment
Share on other sites

the uniqid gives each line there own id eg.

<option id='56d9c29d9332e' value='http://localhost/files/file.zip'>file.zip</option>
<option id='56d9c29d93830' value='http://localhost/files/file.zip'>file.zip</option>
<option id='56d9c29d93f0b' value='http://localhost/files/file.zip'>file.zip</option>
<option id='56d9c29d93f43' value='http://localhost/files/file.zip'>file.zip</option>

users can see the directory where the file is hosted so I am trying to use GET to find the id of the chosen file and start download of that file.

Link to comment
Share on other sites

If you don't want users to see the URL of the file (which they shouldn't see), then the url shoudl not be anywhere in the HTML code for the page. Instead you would have an ID that is statically associated with each URL and that shoudl be the value of the options. But, you are dynamically creating a unique ID when you create the form, so when the ID is passed upon form submission there is no way for your code to know which record the ID is for.

 

I don't know why you are reading this data from a text file. You should instead have a database with each record having a unique ID in the database. then when the user submits their selection you can match up the ID with the appropriate record in the DB. A less optimal solution is to have that ID in the data file. Although I would put the data into an array

 

options_file.php

 

<?php
 
$options = array(
  1 => array('title' => 'Title 1', 'url'=>'http://somedomain.com/url1'),
  2 => array('title' => 'Title 2', 'url'=>'http://somedomain.com/url2'),
  3 => array('title' => 'Title 3', 'url'=>'http://somedomain.com/url3')
);
?>
Link to comment
Share on other sites

It's difficult to understand what you want, and the whole context sounds rather odd.

 

Do you want to assign IDs to URLs so that you can hide the real URL behind something like https://yoursite.com/download.php?file_id=123 ?

 

In that case, I recommend you store all your URLs in a proper database. The table would look something like this:

files
- id CHAR(whatever-length-your-ids-have) PRIMARY KEY
- url TEXT NOT NULL
- title TEXT NOT NULL

The ID must be randomly generated as soon as you store the URL.

 

Whenever a user sends you an ID, you look up the corresponding URL in the database and redirect the user to the URL (assuming it's actually an external URL, not a local file).

 

If you don't want to install a full-blown database system, there's always SQLite which stores the entire database in a single file.

Edited by Jacques1
Link to comment
Share on other sites

the uniqid gives each line there own id eg.

<option id='56d9c29d9332e' value='http://localhost/files/file.zip'>file.zip</option>
<option id='56d9c29d93830' value='http://localhost/files/file.zip'>file.zip</option>
<option id='56d9c29d93f0b' value='http://localhost/files/file.zip'>file.zip</option>
<option id='56d9c29d93f43' value='http://localhost/files/file.zip'>file.zip</option>

users can see the directory where the file is hosted so I am trying to use GET to find the id of the chosen file and start download of that file.

 

 

Yes, we understand this simplistic code.  The problem is, that it doesn't seem you do.  

 

There is code here that you are apparently using, which you did not write and that needs to work on the form submission that you've also omitted from your code.

 

Jacques went ahead and jumped to the solution that most people use, but again we don't know what this app of yours is supposed to be doing or why.  

 

It's reading a temp? file, that has some format of lines, then spitting that out again in the form of a dropdown list, to then be processed, and to what end?  How is the temp file created and why is it created?

 

We don't like cryptic mysterious threads like this that waste our time.  Just looking at the code you provided, I have little doubt that everyone here will find that there are better ways to do this, but since it seems you are trying to use something someone else wrote (we have a different subforum for that as well --- see 3rd party scripts) you should at least start with listing what you started with and where you got it from.  

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.