untz Posted January 25, 2016 at 03:31 PM Report Share #592472 Posted January 25, 2016 at 03:31 PM boa tarde, tenho um problema em fazer o download de um ficheiro que introduzi na base de dados, mas não estou a conseguir se alguem me pudesse ajudar ficaria agradecido no ficheiro download.php tenho <?php if(isset($_GET['id'])) { include (config/config.php); // query the server for the file $id = $_GET['id_ficheiro']; $query = "SELECT Ficheiro FROM ficherio WHERE id_ficheiro = '$id'"; $result = mysql_query($query) or die(mysql_error()); // define results into variables $name=mysql_result($result,0,"Ficheiro"); header("Content-disposition: attachment; filename=$Ficheiro"); mysql_close(); } else{ die("No file ID given..."); } ?> e no index.php: <td><a href='index.php?mod=download&id=$row->id_ficheiro'><button class='btn btn-success'> Download </button></a></td> com isto, ao carregar no button ele faz download do ficheiro index.php, não é o pretendido, se pudessem ajudar ficaria agradecido obrigado! Link to comment Share on other sites More sharing options...
tiago.f Posted January 25, 2016 at 03:41 PM Report Share #592474 Posted January 25, 2016 at 03:41 PM (edited) Olá, falta-te ler o ficheiro $name do disco e "imprimi-lo" o php permite fazer isso directamente com o comando 'readfile' (lê a documentação). Já agora, nos meu projectos, coloca sempre estes header no download de ficheiros: $res['Content-Description'] = 'File Transfer'; $res['Content-Type'] = $mimetype; $res['Content-Disposition'] = 'attachment; filename=' . basename($file); $res['Content-Transfer-Encoding'] = 'binary'; $res['Expires'] = '0'; $res['Cache-Control'] = 'must-revalidate'; $res['Pragma'] = 'public'; $res['Content-Length'] = filesize($file); Edited February 15, 2016 at 12:49 AM by apocsantos Link to comment Share on other sites More sharing options...
untz Posted January 25, 2016 at 03:57 PM Author Report Share #592476 Posted January 25, 2016 at 03:57 PM não percebi. Link to comment Share on other sites More sharing options...
tiago.f Posted January 25, 2016 at 04:16 PM Report Share #592479 Posted January 25, 2016 at 04:16 PM não percebi. qual é a linha do teu codigo que eniva o ficheiro para o cliente/browser? Link to comment Share on other sites More sharing options...
untz Posted January 25, 2016 at 04:18 PM Author Report Share #592480 Posted January 25, 2016 at 04:18 PM não sei Link to comment Share on other sites More sharing options...
tiago.f Posted January 25, 2016 at 04:23 PM Report Share #592483 Posted January 25, 2016 at 04:23 PM nao está! é por isso. A segiur ao(s) header("Content-disposition: attachment; filename=$Ficheiro"); falta-te readfile($name); o meu 2º comentario é que so tens uma instrucao "header", devias ter mais. Vê o meu exemplo e adapta.. Link to comment Share on other sites More sharing options...
untz Posted January 25, 2016 at 04:24 PM Author Report Share #592484 Posted January 25, 2016 at 04:24 PM eu altero os headers que tenho pelos que me enviaste no 2º comentário ? Link to comment Share on other sites More sharing options...
tiago.f Posted January 25, 2016 at 04:30 PM Report Share #592485 Posted January 25, 2016 at 04:30 PM eu altero os headers que tenho pelos que me enviaste no 2º comentário ? adaptando, claro. o meu "header" $res['Content-Disposition'] = 'attachment; filename=' . basename($file); é o equivalente ao teu único que tens. Sugiro criares os outro que te faltam, por exemplo: header('Content-Length: ' . filesize($file)); Link to comment Share on other sites More sharing options...
untz Posted January 25, 2016 at 04:30 PM Author Report Share #592486 Posted January 25, 2016 at 04:30 PM esta bem, obrigado Link to comment Share on other sites More sharing options...
untz Posted January 26, 2016 at 09:07 AM Author Report Share #592527 Posted January 26, 2016 at 09:07 AM eu fiz como o pedido, mas mesmo assim não resultou.. Link to comment Share on other sites More sharing options...
tiago.f Posted January 26, 2016 at 10:32 AM Report Share #592534 Posted January 26, 2016 at 10:32 AM Mostra o código com as correcções que fizeste... Link to comment Share on other sites More sharing options...
ruicosta.web Posted January 26, 2016 at 10:42 AM Report Share #592536 Posted January 26, 2016 at 10:42 AM Como não funcionou? Deu erro? Qual? Tens os erros e avisos, ligados para poder fazer debug de erros? Link to comment Share on other sites More sharing options...
untz Posted January 26, 2016 at 11:02 AM Author Report Share #592538 Posted January 26, 2016 at 11:02 AM bom dia, eu já resolvi parte do download, já faz download de ficheiros em pdf, excel e powerpoint e word no ficheiro download.php tenho o seguinte: <?php include(config/config.php); if (isset($_GET['id'])){ $query = mysql_query("SELECT * FROM ficherio WHERE id_ficheiro = '".$_GET['id']."' limit 0,1"); $data = mysql_fetch_array($query); $file = $data['Ficheiro']; $name = "img/".$file; header("Content-Disposition:attachment; filename=". urlencode($file)); header("content-type: application/octet-stream"); header("content-type: aplication/download"); header("content-description: File Transfer"); header("content-length: ". filesize($name)); flush(); $fp = fopen($name, "r"); while(!feof($fp)){ echo fread($fp, 65536); flush(); } fclose($fp); } ?> Link to comment Share on other sites More sharing options...
tiago.f Posted January 26, 2016 at 11:29 AM Report Share #592539 Posted January 26, 2016 at 11:29 AM Óptimo 🙂 Só algumas notas para melhorar o código. - tens 2 vezes "content-type". Mantém o octet-stream - podes substituir todo o código de leitura do ficheiro (incluindo e para baixo do 1o flush), por readfile($name) - se fores usar este código e, produção, lembra-te que tens falhas de segurança (lê os tópicos "sticky" do sub-forum php) Bom trabalho! 1 Report Link to comment Share on other sites More sharing options...
untz Posted January 26, 2016 at 11:41 AM Author Report Share #592541 Posted January 26, 2016 at 11:41 AM muito obrigado pelas dicas! muito agradecido Link to comment Share on other sites More sharing options...
ruicosta.web Posted January 26, 2016 at 11:51 AM Report Share #592542 Posted January 26, 2016 at 11:51 AM mysql_query é deprecated a partir da versão 5.5 Literatura: http://php.net/manual/en/function.mysql-query.php Já que estás no início, começa por recorrer ao PDO. http://php.net/manual/en/ref.pdo-mysql.php Nunca confies no utilizador. Sempre que necessitas de ler uma variável, faz uma "limpeza" antes de a leres: Se a variável $_GET['id'] receber numeros inteiros, utiliza (int)$_GET['id']. No mínimo dos mínimos segue estas "regras". 1 Report Link to comment Share on other sites More sharing options...
untz Posted March 8, 2016 at 11:42 AM Author Report Share #594165 Posted March 8, 2016 at 11:42 AM alguém me pode ajudar num problema aqui mesmo, eu tento fazer download de ficheiros protegidos e dá erro, diz que está danificado o ficheiro Link to comment Share on other sites More sharing options...
ruicosta.web Posted March 9, 2016 at 02:39 PM Report Share #594194 Posted March 9, 2016 at 02:39 PM Define: Ficheiros protegidos. Na minha definição e vendo o teu código eles são tudo menos protegidos, inclusive a base de dados. Aproveita e troca a utilização do mysql_ por PDO. Link to comment Share on other sites More sharing options...
untz Posted March 9, 2016 at 02:52 PM Author Report Share #594196 Posted March 9, 2016 at 02:52 PM São ficheiros pdf, docx etc.. protegidos Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now