Irishph4nt0m16 Posted March 4, 2016 Share Posted March 4, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/300926-get-url-from-id/ Share on other sites More sharing options...
gizmola Posted March 4, 2016 Share Posted March 4, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/300926-get-url-from-id/#findComment-1531683 Share on other sites More sharing options...
Irishph4nt0m16 Posted March 4, 2016 Author Share Posted March 4, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/300926-get-url-from-id/#findComment-1531684 Share on other sites More sharing options...
Psycho Posted March 4, 2016 Share Posted March 4, 2016 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') ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/300926-get-url-from-id/#findComment-1531686 Share on other sites More sharing options...
Jacques1 Posted March 4, 2016 Share Posted March 4, 2016 (edited) 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 March 4, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/300926-get-url-from-id/#findComment-1531687 Share on other sites More sharing options...
gizmola Posted March 4, 2016 Share Posted March 4, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/300926-get-url-from-id/#findComment-1531688 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.