There is a form

<form action="1.php" method="post" enctype="multipart/form-data"> <input type="file" name="raz" /> <input type="text" value="введите код" name="code" /> <input type="submit" value="submit" /> </form> 

Created a database where images are stored

$

 rs=getimagesize($_FILES['raz']['tmp_name']); if(!empty( $_FILES['raz']['name'] ) && $rs[0]>0){ echo 'images downloaded'."\n".'<br>'; $image = file_get_contents( $_FILES['raz']['tmp_name'] ); $image = mysql_escape_string( $image ); $size=$_FILES['raz']['size']; $type=$_FILES['raz']['type']; $name=$_FILES['raz']['name']; $conn=mysql_connect('localhost','root','') or die(mysql_error()); mysql_select_db('new_bd'); $sql2="CREATE TABLE files ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(30) NOT NULL, type VARCHAR(30) NOT NULL, size INT NOT NULL, content MEDIUMBLOB NOT NULL, PRIMARY KEY(id) );"; $sql="INSERT INTO `files`(name,type,size,content) VALUES( '".$name."','".$type."','".$size."','".$image."')"; 

Now I want to solve the inverse problem: we enter a number into the form and it should display a link to download the file. Tried to do so

 <form action="2.php" method="post" enctype="multipart/form-data"> <!--<input type="file" name="raz" />--> <input type="text" value="введите код" name="code" /> <input type="submit" value="submit" /> </form> 

2.php

 if(!empty($_POST['code'])){ $code=$_POST['code']; $conn=mysql_connect('localhost','root','') or die(mysql_error()); mysql_select_db('new_bd'); $query = "SELECT `content` FROM `files` WHERE `id`='".$code."'"; // Выполняем запрос и получаем файл $res = mysql_query($query); if ( mysql_num_rows( $res ) == 1 ) { $image = mysql_fetch_array($res); // Отсылаем браузеру заголовок header("Content-type: image/jpg"); // И передаем сам файл echo "<a href='".$image['content']."'>opa</a>"; } } Ошибка именно в формировании ссылки 
  • one
    And why MIME header("Content-type: aplication/*"); ? - stck
  • header ("Content-type: image / jpg"); // And transfer the file itself echo $ image ['content']; } The picture is displayed, it remains to make a link to download. echo "<a href='".$image['content'".> opa </a>"; -a error. - koza4ok
  • > <a href='".$image['content'".> opa </a> What is this idiocy? - Artem
  • Well, it turned out like this ... - koza4ok
  • one
    @ koza4ok, you will not be able to transfer the link to the picture - because, firstly, you get the picture by Post, and you can’t transfer it to the link, and secondly, instead of linking to the link, you drop the content of the picture and! this content as a picture! - stck

1 answer 1

 <form method="post" enctype="multipart/form-data"> <!--<input type="file" name="raz" />--> <input type="text" value="введите код" name="code" /> <input type="submit" value="submit" /> </form> <?php if(isset($_POST['code']) && !empty($_POST['code'])){ $code = intval($_POST['code']); echo "<img src=\"2.php?code=$code\" alt=\"Image\" />"; echo "<a href=\"2.php?code=$code\" title=\"Image\" />"; }?> 

2.php

 if(isset($_GET['code']) && !empty($_GET['code'])){ $code=intval($_GET['code']); $conn=mysql_connect('localhost','root','') or die(mysql_error()); mysql_select_db('new_bd'); $query = "SELECT `content` FROM `files` WHERE `id`='".$code."'"; // Выполняем запрос и получаем файл $res = mysql_query($query); if ( mysql_num_rows( $res ) == 1 ) { $image = mysql_fetch_array($res); // Отсылаем браузеру заголовок header("Content-type: image/jpg"); // И передаем сам файл echo $image['content']; } }