Jump to content

Recommended Posts

I want to display the content of a file which is stored in a database. I put a read button next to each column and if you click on this button you should be able to read the content of the stored file on a new page.

 

<?php

if (isset($_POST['Read']))
{
  mysql_connect("localhost","root","") or die ("Connection failed");
  mysql_select_db("txt_archive") or die ("Connection failed");

  $sql2 = "SELECT location FROM files";
  $myData = mysql_query ($sql2,$con) or die ("F?jl nem  tal?lhat?.");
  
  while ($result = $myData->fetch_assoc()){
$loc = $result["location"];
$content = file_get_contents($loc);
print ($content);
  }
};

 

I got the following error message: Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\txtupload\read.php on line 11

 

Has anybody any idea what's wrong with my code?

 

mod edit: add


tags

Link to comment
https://forums.phpfreaks.com/topic/265576-display-content-of-a-file/
Share on other sites

I use an auto-increment ID in the database and save the location next to it. I open the file from the location column of the database.

 

I put here the index.php file as well and please help me what to write in the WHERE clause. (Sorry some messages in the code are in Hungarian)

 

<html>
<head>
<body>

<?php
ini_set("memory_limit","128M");
ini_set("post_max_size","64M");
ini_set("upload_max-filesize","64M");
if(isset($_POST['ok']))
{
if($_FILES['fajl']['name']!="")
{
	if($_FILES['fajl']['type']=="text/plain")
	{
		if($_FILES['fajl']['error']==0)
		{
			if(!is_dir("uploads"))
			{
				mkdir("uploads");
			}
			$filename=basename($_FILES['fajl']['name']);
                $title=$_POST['title'];
			move_uploaded_file($_FILES['fajl']['tmp_name'],"uploads/".$filename);
                $location="uploads/".$filename;
                print $location;
			if(file_exists("uploads/".$filename))
			{
				$error="Sikeres f?jlfelt?lt?s!";
                    $con = mysql_connect("localhost","root","");
                        if (!$con)
                            {
                              die ("Kapcsol?d?s nem siker?lt a kapcsol?d?s ehhez: " . mysql_error());
                            }
                    mysql_real_escape_string($filename);
                    mysql_real_escape_string($title);
                    strip_tags($filename);
                    strip_tags($title);
                    mysql_select_db("txt_archive",$con);
                    $sql = mysql_query("INSERT INTO files (id,location,title) VALUES ('','$location','$title')");

			}
			else
			{
				$error="A F?jl felt?lt?se sikertelen!";
			}
		}
		else
		{
			$error="Hiba a felt?lt?s sor?n: ".$_FILES['fajl']['error'];
		}
	}
	else
		{
			$error="Hib?s f?jlform?tum - csak TXT megengedett";
		}
}
else
{
	$error="Nem adott meg f?jlt!";
}
}
echo "Kor?bban felt?lt?tt fileok:";

 

mod edit: add


tags

OK, we need to take a step back.

 

On the page that you create the buttons you need to pass the ID of the file when the button is clicked. You didn't show the code used to create the buttons, but each button should be inside it's own form with a hidden field for the file id.

 

Then on the page that you display the content you need to use the passed value of the ID to ONLY get the data for the selected file. Right now you are getting all the files and using a WHILE loop to display all of them. So, something like this

$file_id = isset($_POST['file_id']) ? (int) isset($_POST['file_id']) : false;
if ($file_id)
{
  mysql_connect("localhost","root","") or die ("Connection failed");
  mysql_select_db("txt_archive") or die ("Connection failed");

  $sql2 = "SELECT location FROM files WHERE id = $file_id LIMIT 1";
  $myData = mysql_query ($sql2, $con) or die ("F?jl nem  tal?lhat?.");
  if(!mysql_num_rows($myData))
  {
    echo "No file matching your request.";
  }
  else
  {
    $result = $myData->fetch_assoc();
    $loc = $result["location"];
    $content = file_get_contents($loc);
    print ($content);
  }
};

 

Not tested

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.