Jump to content

trouble retrieving file with php


jweiner89

Recommended Posts

Hey everyone, I'm new to this forum (and to PHP). I'm trying to make a website where people can post PDF files for others to download. I have been able to successfully upload the files using HTML forms and enter data about the file into a MYSQL database. However, I have been unable to properly retrieve the file for download in a link. I used this line to try it:

 

Echo "<a href=http://www.mysite.com/files/".$info['content_file']['name'] ."> ".$info['title'] . "</a> <br>";

 

But it bring it to the page mysite.com/files/T. How do I get this address to end with the name of the uploaded PDF?

 

This is the script I used to submit the data to my database:

 

<?php

function findexts ($filename)

{

$filename = strtolower($filename) ;

$exts = split("[/\\.]", $filename) ;

$n = count($exts)-1;

$exts = $exts[$n];

return $exts;

}

 

$ext = findexts ($_FILES['content_file']['name']) ;

$ran = rand () ;

$ran2 = $ran.".";

 

$target = "files/";

$target = $target . $ran2.$ext;

 

$title=$_POST['title'];

$description=$_POST['description'];

$content_type=$_POST['content_type'];

$content_file=($_FILES['content_file']['name']);

 

mysql_connect("..........") or die(mysql_error()) ;

mysql_select_db(".......") or die(mysql_error()) ;

 

mysql_query("INSERT INTO `materials` VALUES ('$title', '$semester', '$content_type', '$content_file')") ;

 

if(move_uploaded_file($_FILES['content_file']['tmp_name'], $target))

{

 

echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";

}

else {

 

echo "Sorry, there was a problem uploading your file.";

}

?>

 

 

And this is the function I used to retrieve it:

<?php

mysql_connect("...........") or die(mysql_error()) ;

mysql_select_db("..........") or die(mysql_error()) ;

 

$data = mysql_query("SELECT * FROM materials") or die(mysql_error());

 

while($info = mysql_fetch_array( $data ))

{

 

Echo "<a href=http://www.mysite.com/files/".$filename ."> ".$info['title'] . "</a> <br>";

Echo "<b>Description:</b> ".$info['description'] . " <br>";

Echo "<b>Content Type:</b> ".$info['content_type'] . " <hr>"; }

?>

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/239594-trouble-retrieving-file-with-php/
Share on other sites

 

And this is the function I used to retrieve it:

<?php

mysql_connect("...........") or die(mysql_error()) ;

mysql_select_db("..........") or die(mysql_error()) ;

 

$data = mysql_query("SELECT * FROM materials") or die(mysql_error());

 

while($info = mysql_fetch_array( $data ))

{

 

Echo "<a href=http://www.mysite.com/files/".$filename ."> ".$info['title'] . "</a> <br>";

Echo "<b>Description:</b> ".$info['description'] . " <br>";

Echo "<b>Content Type:</b> ".$info['content_type'] . " <hr>"; }

?>

 

should be $info['filename'] and NOT $filename.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.