Jump to content

Retrieving a file path from another Page/Linking it to DB


blivori

Recommended Posts

Hi,

 

Basically I have a php script that lists all files in a directory and outputs them as hyperlinks and then saves the file names to a MySQL Database. However, only the file names are stored in the DB. Another script, search.php, gets the text input from list.php and outputs all the files in the database witouth being hyperlinked - just plain text.

 

Is it possible to save the filepath in the MySQL and output the file names hyperlinked?

 

This is the code:

 

List.php

 

<?php

$dbhost = 'localhost';
$dbuser = 'admin';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'files';
mysql_select_db($dbname);


mysql_query("TRUNCATE files_tbl");


if ($handle = opendir('./pdf/')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
  {
          	 

		$thelist .= '<a href="'.$file.'">'.$file.'<br></a>';

		$query = "INSERT INTO files.files_tbl(Name) VALUES ('$file');";


		mysql_query($query) or die('Error, Insert query failed' . mysql_error());

		          }
	  
	  
       }
  closedir($handle);
  }
?>

<html>
    <head>

        
    </head>

    <body>

    <form action="search.php" method="post">
     Search: <input type="text" name="term" /><br />
    <input type="submit" name="submit" value="Submit" />
    </form>

    </body>
</html>

<P>List of files:</p>
<P><?=$thelist?></p>

 

Search.php

 

<?php

	mysql_connect ("localhost", "admin","root")  or die (mysql_error());
	mysql_select_db ("files");

	$term = $_POST['term'];

	$sql = mysql_query("select * from files_tbl where Name like '%$term%'");

	while ($row = mysql_fetch_array($sql)){

		echo <h1><br/> Name: '.$row['Name'];
    }

?>

 

I would really appreciate your help.

 

Thanks & Regards,

blivori

Link to comment
Share on other sites

Hi Ptsface12,

 

Thanks for your quick reply.

 

Where will I have to put that?

 

It looks like a text box input.

 

What I want to do is to get the full link from the hyperlinked file on the list.php and save it to the fiels_tbl.

 

Thanks again,

blivori

Link to comment
Share on other sites

Hello,

What I suggest you doing is setting up your mysql table. Echoing out your links

 

Import this in your MySQL

CREATE TABLE `hreflinks` (
  `id` bigint(20) NOT NULL,
  `link` varchar(255) NOT NULL,
  `showtext` varchar(255) NOT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

<?php
$username="username"; // edit your username
$password="password"; // edit your password
$database="database"; // edit your database

mysql_connect(localhost,$username, $password);
@mysql_select_db($database) or die ("Unable to select Database");

$sql = mysql_query("SELECT id, link, showtext FROM hreflinks") or die(mysql_error());
$row = mysql_fetch_assoc($sql)
?>

 

Put this where you need to echo your details:

<?php

echo <a href=$row['link'><?php echo $row['showtext']; ?></a>;

?>

 

If you have any problems while running this script, just reply again. If you have IM and need to contact me this way add Ptsface12@yahoo.com

 

Best Wishes,

Ptsface12

Link to comment
Share on other sites

Hello,

Try this:

<input type="hidden" id="urlcontainer" name="urlcontainer" value="<?php dirname("/etc/passwd") . PHP_EOL; ?>"/>
<?php
$input = mysql_query("INSERT INTO files_tbl WHERE url_field");
?>

 

Best Wishes,

Ptsface12

 

And as for this code? Where do I put it? I'm stuck on where I have to put the code you have given me.

Link to comment
Share on other sites

Ok. So the code has to be like this?

 

List.php

 

 

<?php

$dbhost = 'localhost';
$dbuser = 'admin';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'files';
mysql_select_db($dbname);


mysql_query("TRUNCATE files_tbl");


if ($handle = opendir('./pdf/')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
  {
          	 

		$thelist .= '<a href="'.$file.'">'.$file.'<br></a>';

		$query = "INSERT INTO files.files_tbl(Name) VALUES ('$file');";


		mysql_query($query) or die('Error, Insert query failed' . mysql_error());

		          }
	  
	  
       }
  closedir($handle);
  }
?>

<?php
		$username="admin"; // edit your username
		$password="root"; // edit your password
		$database="files"; // edit your database

		mysql_connect(localhost,$username, $password);
		@mysql_select_db($database) or die ("Unable to select Database");

		$sql = mysql_query("SELECT id, link, showtext FROM hreflinks") or die(mysql_error());
		$row = mysql_fetch_assoc($sql)
?>

<html>
    <head>

        
    </head>

    <body>

    <form action="search.php" method="post">
     Search: <input type="text" name="term" /><br />
    <input type="submit" name="submit" value="Submit" />
    </form>

    </body>
</html>

<P>List of files:</p>
<P><?=$thelist?></p>

 

Search.php

 

<?php

	mysql_connect ("localhost", "admin","root")  or die (mysql_error());
	mysql_select_db ("files");

	$term = $_POST['term'];

	$sql = mysql_query("select * from files_tbl where Name like '%$term%'");
	$sql2 = mysql_query("SELECT FieldID, Link, ShowText FROM files_tbl") or die(mysql_error());


	while ($row = mysql_fetch_array($sql)){

		echo <h1><br/> Name: '.$row['Name'];
		echo <a href=$row['link'><?php echo $row['showtext']; ?></a>;


   }

?>

 

Sorry for my ignorance but I've only done some basic php.

Link to comment
Share on other sites

It's still not working :/.

 

Just to give you a basic idea:

 

This is the List.php with the hrefed links:

thumb_bmp.png

 

And when you enter a search term it is supposed to show up another page with files containing contents from the search term. But as you can see they are not hrefed.

thumb_bmp.png

Link to comment
Share on other sites

You mean you want to show the filenames as links from DB based on a user made search ? You have that query already in the earlier codes you posted? If you want to join the links data to the file data you need to have some id field on some of the tables for the reference. Anyways if the links are related to the files, why not just make one table like

 

Files

------------

file_id

filename

file_link

showtext

 

Or did I misunderstood something?

Link to comment
Share on other sites

I tried to do a circumvention by modifying search.php to be like this:

 

<?php

	mysql_connect ("localhost", "admin","root")  or die (mysql_error());
	mysql_select_db ("files");

	$term = $_POST['term'];

	$path = "127.0.0.1/pdf/"

	$sql = mysql_query("select * from files_tbl where Name like '%$term%'");


	while ($row = mysql_fetch_array($sql)){

		$full_path = $path.$row;

		echo <h1><br/> Name: '<a href = " > .$full_path['Name']';




   }

?>

 

However, it's not even inputting the path or the hyperlinked text.

 

Any help please?

Link to comment
Share on other sites

Tried this now but still no luck:

 

<?php

	mysql_connect ("localhost", "admin","root")  or die (mysql_error());
	mysql_select_db ("files");

	$term = $_POST['term'];

	$sql = mysql_query("select * from files_tbl where Name like '%$term%'");

	while ($row = mysql_fetch_array($sql)){

	$path = "/localhost/pdf/";
	$fullpath = $path.$row;

		echo '<h1><br/> Name: '.$row['Name'];
		echo '<h3><br/> Path: <a href = '.$fullpath'> '.$fullpath' </a> ';
    }

?>

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.