Hi,
I am very new to php. And I have got to submit a project very soon (By tomorrow evening). So please help.
The problem is this. I have created a file upload mechanism where the uploaded files are stored under a different id for security something like 0001.pdf, 0002.jpg etc.. The original names are stored in a mysql database. Now I have a file download page. The links are the original names of the files like user1.pdf, user2.pdf... When the link is clicked the file that comes up has the name 0001,0002 etc ie the one on server.
I want it to open as user1.pdf, user2.pdf etc that is under the original name in which they were uploaded.. How to do this?
14 2111
Heya, suryadithya. Welcome to TSDN!
You'd need to create a PHP script that outputs the images directly and then make the PHP script the src of the image tag.
For an example, check out this article. Some of the reading is a bit heavy, but you should find the concepts and syntax that you're looking for about halfway down the page.
Heya, suryadithya. Welcome to TSDN!
You'd need to create a PHP script that outputs the images directly and then make the PHP script the src of the image tag.
For an example, check out this article. Some of the reading is a bit heavy, but you should find the concepts and syntax that you're looking for about halfway down the page.
Thank you for that reply pbmods. But the article was very heavy for me. And I don't think that solved my problem. I have allowed upload of several file types not just images. They are not stored under their original names anywhere on the server. The only source of the names is the mysql db.
What I want is when the user clicks on "save as" the file should be saved under it's original name on the db. Not the one on the server.
Heya, Suryadithya.
Hm. So you need to be able to load any file by using an alias. -
<a href="getFile.php?file=0001.pdf" />
-
getFile.php would then connect to the database and load the file: -
$_file = mysql_real_escape_string($_GET['file']);
-
$_sql = "
-
SELECT
-
*
-
FROM
-
`Ext_Uploads`
-
WHERE
-
`file` = '{$_file}'
-
LIMIT 1";
-
.
-
.
-
.
-
That's the easy stuff; I'm sure I'm just repeating what you've already got working so far
The tricky part is to output the file, you need to know the Mime-type of the file. By default, the Content-type header will be set to 'text/html', so the browser won't display the file properly.
You could add a switch that matches off the file extension: -
$_fileName = $mysql_row['realFileName'];
-
switch( strtolower(substr($_fileName, 0, strrpos($_fileName, '.'))) )
-
{
-
case 'jpg':
-
case 'jpeg':
-
$type = 'image/jpeg';
-
break;
-
-
case 'pdf':
-
$type = 'application/pdf';
-
break;
-
-
.
-
.
-
.
-
}
-
-
header('Content-type: ' . $type);
-
After all that, actually outputting the file is quite easy:
The readfile() function opens a file and sends its content to the output buffer (outputs it to the browser).
Atli 5,058
Expert 4TB
Hi.
Additionally, if you want to change the name (most) browsers will download the file as, you can set this header aswell: -
header("Content-Disposition: attachment; filename=\"newFileName.pdf\";" );
-
Noe, that this apparently does not work with Safari. Works on pretty much everything else tho.
Thank you both..That shows me a direction..
Actually I had given only a html link to the files on server. I didn't get files using php.
But I have a doubt. I think your solution will work only if I have stored the file in the db. I have only stored the user given file names. The files are on the server. How do I load those files and connect them to the output buffer.
What is the command in php to read files from an external source? This I can then rename using your script.
Heya, Suryadithya.
readfile() will output a file on the server; to output a file stored in the database, you would simply echo the data that you fetched from the database.
Atli 5,058
Expert 4TB
Check out this thread.. They are discussing pretty much the same thing and they have posted a couple of examples.
I think I can do it now. The switch case and content disposition should do fine.
Can you people just explain me the syntax of - <a href = "getfile.php?file=0001.ext">
Here my file name is itself a variable say $id.Will - $link="getfile.php?file=".$id;
-
<a href ="$link">;
do fine?
And why does this go to $_GET array?
PBMODS shouldn't the last line of the code be readfile($file) because that is the name of the file on the server? ($file=0001.ext,00002.ext etc)
I assume that you meant by realfilename the user given name on db which is $_filename as per your code. How can I read it when there is no such file on server?
And thanks in advance.
Heya, Suryadithya.
Please use CODE tags when posting source code:
[CODE=php]
PHP code goes here.
[/CODE]
Unless I read your post incorrectly, you are storing the *real* file names in the database, while the value of $_GET['file'] is actually the 'alias' that you would use to look up the *real* file name in the database.
The goal is to readfile() using the file name stored in the database.
In terms of $_GET, some PHP setups have register_globals turned on, which means that any variables passed in the URL will be automagically turned into global variables. This behavior has been widely deplored for security reasons, and is in fact being discontinued in PHP 6. Using $_GET is a safer way to do it because it encourages you to validate your input before using it, for example by passing it through mysql_real_escape_string().
Thanks.I will be careful while posting codes in the future. My db has two fields. One corresponds to the id on server 0001 0002 etc and the other to user given names.
By alias I think you mean the id. Then -
header('Content-Disposition: attachment; filename="realname.ext"');
-
readfile('idlike0001.ext);
-
should be thecode I think.You say the goal is to readfile(realname).But how is it possible when it is stored only under alias in the server.?
Heya, Suryadithya.
In this line: -
readfile('idlike0001.ext');
-
instead of the literal 'idlike0001.ext', you'd want to use the filename you fetched from the database.
For example: -
$_id = mysql_real_escape_string($_GET['file']);
-
$_sql = "
-
SELECT
-
`filename`
-
FROM
-
`uploads`
-
WHERE
-
`id` = '{$_id}'
-
LIMIT 1";
-
$_res = mysql_query($_sql);
-
-
if( empty($_res) )
-
{
-
header('HTTP/1.0 404 Not Found');
-
exit;
-
}
-
-
$_data = mysql_fetch_assoc($_res);
-
mysql_free_result($_res);
-
-
// content-type stuff goes here...
-
-
.
-
.
-
.
-
-
readfile('/path/to/uploads/' . $_data['filename']);
-
This will pass the filename stored in the database to readfile() instead of the database ID number.
Thankyou everyone... I got it running.. But still I have a problem.I am not able to view image files like jpg,gif after downloading. The error message displays that it is not a valid gif file.. The file is viewable on the uploads folder and after downloading the properties are the same ie the file size is same. But still I can't open them in image viewer. My content type is ok. I have echoed it and checked it. And the code too must be ok since I am able to view pdf files. I am using -
readfile("uploads/".$filename);
-
The headers I have used it the same way pbmods posted. Any guess what's the problem?
Heya, Suryadithya.
Does your script output *anything* else? Is there any whitespace before the first <?php tag? Is there anything after the final ?> tag? Any other output from your script, intentional or otherwise, will cause the image data to be invalid.
Yeah.. Thanks.. That was the problem. There was a space b4 my php tags. Now it works fine.Thanks a lot.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Kajol |
last post by:
Hi
I am facing a strange problem in downloading files from the web.
CASE 1:
I have an ActiveX.dll file developed using Visual Basic 6.0 and uploaded on
a Linux Server. I am typing...
|
by: fibreiv |
last post by:
I am trying to download files from my database that I uploaded to it. I can
download bmp, txt file but have not been able to d/l pdf files. I am able to
d/l pdf files stored in the file system...
|
by: Garett Rogers |
last post by:
I am creating a document manager for our intranet in VB.NET and I have
stumbled across a problem that I cant seem to find a solution for.
Everything is working as planned:
1) upload a file from...
|
by: TJ |
last post by:
Hi,
I've written code web-based uploading and downloading.
Here is some code for it.
For saving file into MS-SQL database,
SaveFileIntoDB(HttpPostedFile file) {
int fileLength =...
|
by: RedHair |
last post by:
I'd like to set up a file system for the ASP.NET 2.0 application
to store user-uploaded files, since the members are more than
100,000 people, the basic requirements are as below:
(1) The file...
|
by: Vic Spainhower |
last post by:
Hello,
I am trying to download a file to my local machine using PHP. However, when
the script runs the file is placed in the folder assigned to the ftp user on
the Web server instead of...
|
by: keerthyragavendran |
last post by:
hi
i'm downloading a single file using multiple threads...
how can i specify a particular range of bytes alone from a single
large file... for example say if i need only bytes ranging from...
|
by: =?Utf-8?B?U2FjaGluIENoYXZhbg==?= |
last post by:
Hi,
I need to download files from the FTP location. This FTP location is
readonly n I dont hv access to delete files from this location once
downloaded. The files to the ftp location keeps...
|
by: jannyguy |
last post by:
Hi,
I have a web application running on an apache server. It has a download link for downloading a file. My requirement is to download this file without asking the user where to download....
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |