am using a script to download files from my site.
if (isset($_GET['file'])){
$file = $_GET['file'];
$query = "SELECT fileurl FROM dbname WHERE id='$file'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$filename = $row[fileurl];
send_download($filename);
}
function send_download($filename){
$file_path = $filename;
$file_size=#filesize($file_path);
header("Content-Type: application/x-zip-compressed");
header("Content-disposition: attachment; filename=$filename");
header("Content-Length: $file_size");
readfile($file_path);
exit;
}
It works great but the only caveat is that the file name is changed to some encryted string ie asdajkhasdkahdkasdakfsdhf.pdf how do I change this so that it downloads with the original name??? And if possible, append + to the spaces inbetween the file name ie My+File.pdf
Related
My script returns a file which has a proper name but it is without any filetype or extension. When I open this file it shows just very long almost infinite string of characters. I want to return an image (.png file) and I specified MIME type for it in
header("Content-type: image/png");
My script is:
$query = "SELECT * " ."FROM images WHERE filename = '$name2'";
$result = mysqli_query($connect,$query)
or die('Error, query failed');
list($file, $content) = mysqli_fetch_array($result);
header("Content-type: image/png");
header("Content-Disposition: attachment; filename=$file");
ob_clean();
flush();
echo $content;
mysqli_close($connect);
exit;
And the echoed file is just infinite string of characters like that:
¸A<'^ą~`U–°ŹĘŘC.
It doesn't convert the BLOB into proper filetype (.png)
<?php
$connection = mysqli_connect("localhost","root"," ",your_database)
or die('Database Connection Failed');
mysqli_set_charset($connection,'utf-8');
$id = 1;
// Use a prepared statement in production to avoid SQL injection;
// we can get away with this here because we're the only ones who
// are going to use this script.
$query = "SELECT * " ."FROM images WHERE filename = '$name2'";
$result = mysqli_query($connect,$query)
or die('Error, query failed');
list($id, $file, $type, $size,$content) = mysqli_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$file");
ob_clean();
flush();
echo $content;
mysqli_close($connection);
exit;
?>
It is worked for me!!
I wrote php script to upload file from admin panel and to download for user.
Uploading was done successfully but after downloading the file, it is unable to read. if it is a image file or pdf file. The uploaded files are copied to xamp/htdocs/myproject/admin/documents. and the path is stored to mysql database. When i tried to download it the file size is different for uploaded file and downloaded file and it is unable to read.
The uploading script
if (isset($_POST['submit'])){
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];
if ($name) {
$location = "documents/$name";
move_uploaded_file($tmp_name, $location);
$query = mysqli_query($con,"INSERT INTO posts (name,path) VALUES ('$location')");
}
Thanks everybody for helping me. i made changes for my script yesterday and it is working but the problem is that when i tried to add it to my project it is not working.
my uploads script(it is writen in myproject/admin/application.php)
if (isset($_POST['submit'])){
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];
if ($name) {
$location = "documents/$name";
move_uploaded_file($tmp_name, $location);
$query = mysqli_query($con,"INSERT INTO posts (name,path) VALUES ('$location')");
}
download.php (Written in myproject/download.php)
<?php
include('includes/connect.php');
if (isset($_GET['dow'])) {
$path = $_GET['dow'];
$res = mysqli_query("SELECT * FROM posts WHERE path = '$path'");
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"" . basename($path) . "\";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path); //showing the path to the server where the file is to be download
exit;
}
?>
The linking script to download.php b (written in myproject/viewprofile.php)
<?php
include('includes/connect.php');
$sql = "SELECT * FROM posts";
$res = mysqli_query($con, $sql);
$path = $row['post_path'];
echo "<a href='download.php?dow=$path' class='btn btn-dark btn-theme-colored btn-flat mr-5' style='width:100%; margin-top:5px;'> DOWNLOAD RESUME </a> <br>";
?>
I'm trying to create a temporary download link for download files.
my code is:
$file_temp_adrs = "temp/".md5(microtime());
mkdir($file_temp_adrs);
$file_temp_adr = $file_temp_adrs."/".$fileinfo['org_filename'];
$file_org_adr = "files/".$fileinfo['filename'];
copy($file_org_adr , $file_temp_adr);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $file_temp_adr);
finfo_close($finfo);
$name = basename($file_temp_adr);
$size = filesize($file_temp_adr);
header("Content-Disposition: attachment; filename=\"".$name."\"");
header("Content-Type: $mime_type");
header("Content-Length: $size");
header("Connection: close");
When i click on download button, the browser saves a file with true name and extention but the file size is 0KB that is not usable.
where is wrong?
I think you do not actually deliver the file content and you should output the file content using readfile:
readfile($file_temp_adr);
I'm currently making a file hosting website. I'm having a problem with renaming the file before the user downloads the file. When the file is uploaded the original name is saved in the mysql database and the file is renamed to a id.
<?php
require("includes/inc.php");
$id = trim(sanitize($_GET['id'])); //Receives the id of the file from the url
if($id) {
$fileid = mysql_query("SELECT * FROM files WHERE id = '$id'");
if (mysql_num_rows($fileid) != 1) {
header('Location: download.php?error=invalid_file');
exit();
} else {
while($info = mysql_fetch_array($fileid)) {
$fileid2 = $info['id'];
$filename = $info['name'];
$ext = $info['ext'];
$filesize = $info['size'];
$downloads = $info['downloads'];
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Location: uploads/".$fileid2.".".$ext);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: ' . $filesize);
}
?>
I would think your "while" loop and output would be something like this: (I also switched the $filename and $fileid2 in the headers)
while($info = mysql_fetch_array($fileid)) {
$fileid2 = $info['id'];
$filename = $info['name'];
$ext = $info['ext'];
$filesize = $info['size'];
$downloads = $info['downloads'];
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$fileid2.'.'.$ext.'"');
header('Content-Length: ' . $filesize);
readfile(uploads/".$filename.".".$ext);
I did not test this, so I hope it helps.
I wanted to open a save image dialog box when I click on an image. I managed to open the same but when saved, it does not save open saved image as the content of image is not saved somehow.
PHP code:
$imageName = $_GET['i'];
$imageName = $imageName . '-HR.jpg';
header ("Content-Type: application/download");
header ("Content-Disposition: attachment; filename=$imageName");
header("Content-Length: " . filesize("$imageName"));
$fp = fopen("$imageName", "r");
fpassthru($fp);
The passing URL is something like:
mydomain/download_image.php?c=atherothrombosis&i=embolus-carotid-artery-illustration
Please suggest solution. Thanks.
add this header also
header("Content-Type: application/force-download");
I managed to do so by using below code:
<?php
$imageName = $_GET['i'];$imageName = $imageName . '-HR.jpg';
$imageCatName = $_GET['c'];
$imageCatName = ucwords($imageCatName);
$file_path = $docRoot . '/static/media/images/content/image_library/'.$imageCatName . '/'. $imageName;
if(file_exists($file_path)) {
header("Content-disposition: attachment; filename={$imageName}");
header('Content-type: application/octet-stream');
readfile($file_path);
}else {
echo "Sorry, the file does not exist!";
}
?>
still thanks a lot for your support. :)