Jump to content

[SOLVED] help with uploading a file and inserting file info to db


Recommended Posts

Hello all,

 

well i am working on a project here. and its a app. where a user uploads a file to a dir. and a page should list the file info. for example, the date, a link to view the file.

 

i have it to where i can upload the file to a dir, insert to the db (file info) and display the record from the db. BUT, i am having trouble with getting the date and making the link display on the page.

 

this is what i have:

 

uploadform.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>upload pdfs</title>
</head>

<body>
<form action="uploadpdf.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="51200" />
<p>File to upload:
<input type="file" name="fileupload" /></p>
<p><input type="submit" value="upload!" /></p>
</form>
</body>
</html>

 

uploadpdf.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>uploading file</title>
</head>

<body>
<?php
  $file_dir = "G:\cityweb1\pdfs\citysect";
  $file= $_FILES['fileupload'];
  
   print "path: ".$file['tmp_name']. "<br />";
   print "name: ".$file['name']."<br />";
   print "type: ".$file['type']."<br />";
   print "size: ".$file['size']."<br />";
   print "error: ".$file['error']."<br />";
      
   if (is_uploaded_file($file['tmp_name'])) {
     move_uploaded_file($file['tmp_name'],"$file_dir/".$file['name']) or die ("could not moved");
     print "file was moved!<br />";

		if(!($db = @ mysql_connect('localhost', 'user', 'pass')))
		{
			print "Error: Could not connect to our database sorry for any inconvience.<br /> Please try at a later time.";
		}						
			 //select which database you want to edit
			mysql_select_db("edinburg_site"); 
			$agenda_id=$_POST["agenda_id"];
			$adate=$_POST["adate"];
			$link=$_POST["link"];

			$query= "INSERT INTO ccagenda VALUES (null,'{$adate}','{$link}')";
			$result=mysql_query($query);

			while ($r=mysql_fetch_array($result))
			{
				$agenda_id=$r['agenda_id'];
				$adate=$r['adate'];
				$link=$r['link'];
			}

   }
?>
</body>
</html>

 

 

display all records from that db here

        <?php 
		if(!($db = @ mysql_connect('localhost', 'user', 'pass')))
		{
			print "Error: Could not connect to our database sorry for any inconvience.<br /> Please try at a later time.";
		}						
		 //select which database you want to edit
		mysql_select_db("edinburg_site"); 
		$agenda_id=$_GET[agenda_id];

            print "<center><table width=\"100%\" border=\"0\" cellpadding=\"1\" cellspacing=\"0\">";

		$query="SELECT * FROM ccagenda ORDER BY agenda_id DESC";
		$result=mysql_query($query);

		while ($r=mysql_fetch_array($result))
		{
			$agenda_id=$r['agenda_id'];
			$adate=$r['adate'];
			$link=$r['link'];

			print "<tr><td colspan=\"2\">";
			print "<table width=\"100%\" border=\"0\" cellpadding=\"1\" cellspacing=\"2\" bgcolor=\"#E8DFBB\">";
                print "<tr>";
                print "<td width=\"50%\" align=\"left\" style=\"color:#3949f9\">" .$adate. "</td>";
                print "<td align=\"right\" width=\"50%\">" .$link. " <img src=\"images/pdfimage.png\" title=\"\" border=\"0\" /></td>";
                print "</tr>";
                print "</table>";
			print "</td></tr>";			
		}
		print "</table></center>";
	?>

 

this page displays 0000-00-00 for date and no link. this is where i have trouble. how do i get the date and link into the db?

 

 

PLEASE HELP!!!!!!

            $agenda_id=$_POST["agenda_id"];
            $adate=$_POST["adate"];
            $link=$_POST["link"];

 

Where is that post data coming from? I do not see it anywhere on the form.

 

Is there a reason why you would want to use a passed in date instead of just using NOW() in the MySQL insert?

well we can disregard that info for now. i have it somewhat working. i'm thinking of just giving the option to input the date of the file during uploading the file. now on the page that displays all the records on the db as files get uploaded, i will be showing the date of the file as a link to grab that file from the dir.

 

but now i am having trouble with pointing that link(date) to the dir of files being uploaded.

 

this is what i have for the that page to display:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Meet Edinburg's City Council Agenda</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="style2.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="images/favicon.ico"  />

<script type="text/javascript">
<!--
function go() 
{
window.location=document.getElementById("menu").value;
}
//-->
</script>
<script type="text/javascript">
<!--
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
<!--[if lte IE 6]>
<link href="ie6style2.css" rel="stylesheet" type="text/css" />
<![endif]-->

</head>
<body>
<div id="cmain_wrapper">
<?php include ('header.php');?>
  </div>
   <div class="padding">
      <div id="cmain_content">
	<p style="padding-bottom:0"><font style="font-family: Arial, Helvetica, sans-serif; color:#0066CC; font-size:18px">City Council Meeting Agendas</font></p><p>The Edinburg City Council meets regularly on the <b>first and third Tuesday of every month at 7:00 p.m.</b> Regular meetings are video and audio taped. Minutes of all meetings are available at the City Secretary Department. Meetings are held regularly at the new Council Chamber in the new City Hall.</p>
        <?php 
		if(!($db = @ mysql_connect('localhost', 'user', 'pass')))
		{
			print "Error: Could not connect to our database sorry for any inconvience.<br /> Please try at a later time.";
		}						
		 //select which database you want to edit
		mysql_select_db("edinburg_site"); 
		$agenda_id=$_GET[agenda_id];

            print "<center><table width=\"100%\" border=\"0\" cellpadding=\"1\" cellspacing=\"0\">";

		$query="SELECT * FROM ccagenda ORDER BY agenda_id DESC";
		$result=mysql_query($query);

		while ($r=mysql_fetch_array($result))
		{
			$agenda_id=$r['agenda_id'];
			$adate=$r['adate'];
			$link=$r['link'];

			print "<tr><td colspan=\"2\">";
			print "<table width=\"100%\" border=\"0\" cellpadding=\"1\" cellspacing=\"2\" bgcolor=\"#E8DFBB\">";
                print "<tr>";


//RIGHT HERE IS WHERE I WANT TO DISPLAY THE DATE AS A LINK TO VIEW THAT FILE. ANY HELP
                print "<td width=\"50%\" align=\"left\" style=\"color:#3949f9\"><a href='pdfs\citysect\?agenda_id={$agenda_id}'>{$adate}</a></td>";
                print "<td align=\"right\" width=\"50%\"><img src=\"images/pdfimage.png\" title=\"\" border=\"0\" />PDF File </td>";
                print "</tr>";
                print "</table>";
			print "</td></tr>";			
		}
		print "</table></center>";
	?>
      </div>
      <div id="cleft_bar">
	<?php include ('links.php');?>
          <br />
          <ul>
          	<li style="text-decoration:underline">Side Menu</li>
            <li><a href="council.php">City Council Home</a></li>
            <li><a href="citysect.php">City Secretary</a></li>
            <li><a href="meet.php">Meet the Council</a></li>
            <li><a href="councilagendas.php">Meeting Agendas</a></li>
            <li><a href="">Video Meetings</a></li>
            <li><a href="meetinrules.php">Rules &amp Procedures</a></li>
            <li><a href="">Advisory Board & Comities</a></li>
          </ul>
      </div>
    </div>
<?php include ('footer.php');?>
</div>
</body>
</html>

Does it not work? From the looks of it you have the link there with the date, but your link slashes are backwards they need to be /

 

If that does not help, post on what is not working about it? Are you sure the index file under citysect is working properly? or does there actually need to be a different file name that it should point to?

well under citysect dir. there are all pdf files. the date link should be pointing to that the file corresponding with that date. would it help if i use the date with the file?

 

i have changed the slashes.

 

right now if you click on the date link it points to a page not found.

Well here are a few issues that I see.

 

A. You do not send the filename to the DB. If you did you could just query the DB and pull out the filename and use that in place of ?agenda_id.

 

B. It is hard to disregard that info I requested, because the real question is, what is suppose to be contained in "link" in the DB? Why is that there?

 

You seem to be missing a few steps that are crucial, such as the actual file name stored in the db. 

so you think i should ask while uploading the file (uploadform.php) to type in the date and name of the file?

 

i know the link thing, well i will be tweaking up the db im planning on putting the file name there

so you think i should ask while uploading the file (uploadform.php) to type in the date and name of the file?

 

i know the link thing, well i will be tweaking up the db im planning on putting the file name there

 

You can, or you can pull it out from $_FILES. Whichever you prefer, but you will need it to access it later.

   print "path: ".$file['tmp_name']. "<br />";
   print "name: ".$file['name']."<br />";
   print "type: ".$file['type']."<br />";
   print "size: ".$file['size']."<br />";
   print "error: ".$file['error']."<br />";

 

Are you not already doing that? the $file['name'] is what that file name was when it was uploaded....

right but how would i about getting it or sending it to the display page where i display all files that are being uploaded?

 

        <?php 
		if(!($db = @ mysql_connect('localhost', 'user', 'pass')))
		{
			print "Error: Could not connect to our database sorry for any inconvience.<br /> Please try at a later time.";
		}						
		 //select which database you want to edit
		mysql_select_db("edinburg_site"); 
		$agenda_id=$_GET[agenda_id];
		$file = $_GET[$file];

            print "<center><table width=\"100%\" border=\"0\" cellpadding=\"1\" cellspacing=\"0\">";

		$query="SELECT * FROM ccagenda ORDER BY agenda_id DESC";
		$result=mysql_query($query);

		while ($r=mysql_fetch_array($result))
		{
			$agenda_id=$r['agenda_id'];
			$adate=$r['adate'];
			$filename=$r['filename'];

			print "<tr><td colspan=\"2\">";
			print "<table width=\"100%\" border=\"0\" cellpadding=\"1\" cellspacing=\"2\" bgcolor=\"#E8DFBB\">";
                print "<tr>";
                print "<td width=\"50%\" align=\"left\" style=\"color:#3949f9\"><a href='pdfs/citysect/{$adate}.pdf' target='_blank'>{$adate}</a></td>";
                print "<td align=\"right\" width=\"50%\"><img src=\"images/pdfimage.png\" title=\"\" border=\"0\" />PDF File </td>";
                print "</tr>";
                print "</table>";
			print "</td></tr>";			
		}
		print "</table></center>";
	?>

You would need to enter that into the DB, you currently are not. So in the file upload script enter it there, you may have to modify your DB to accept a new column filename.

 

To clarify further, this would be your insert statement:

 

            $query= "INSERT INTO ccagenda VALUES (null,'{$adate}','{$link}', '{$file['name']}')";

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.