How to create a download link for a file using PHP?
In my job portal project, I have stored the job seekers data in MySQL Database. I have stored their uploaded resumes in the upload folder. I am able to retrieve that data of job seekers for the employers through pagination. And I want to create a download resume link of each job seeker with his data in the pagination result. How I can add the download resume link for the resume files from upload folder with each search result in the pagination?
3 Answers 3
If I understood the question, this is very easy. Since you have the resume files in the upload folder. Just select the file name from the database and store on a variable lets say $filename , Like
$filename = $row['cv_filename']; // supposing $row contains the field of database
Then, use prefix the folder name in the anchor a link to that file.
On clicking the Download link, the correct link to file is shown in address bar of the browser but file is not downloading. Browser is showing an error: The address wasn’t understood.Firefox doesn’t know how to open this address, because the protocol (c) isn’t associated with any program. You might need to install other software to open this address.
@YogeshRawal, Can you show the link, that you used to initiate the download? Please provide full link together with http(s)://
I am working on my localhost server. In $prefix variable I given the address of the my download folder i.e. C:/wamp/www/Job Portal/downloads. And on clicking the download CV link, the address shown in the browser was: C:/wamp/www/Job Portal/downloads/abc.doc
@YogeshRawal, That is wrong, of course browser does not know how open that address. You don’t need to include C:/wamp/www/Job Portal/ , Just include downloads/abc.doc , browser will handle the rest.
It’s done. Thanks. It is really very easy to implement. Regarding security issues is this method good to implement?
link
Returns true on success or false on failure.
Errors/Exceptions
The function fails, and issues E_WARNING , if link already exists, or if target does not exist.
Examples
Example #1 Creating a simple hard link
$target = ‘source.ext’ ; // This is the file that already exists
$link = ‘newfile.ext’ ; // This the filename that you want to link it to
?php
Notes
Note: This function will not work on remote files as the file to be examined must be accessible via the server’s filesystem.
Note: For Windows only: This function requires PHP to run in an elevated mode or with the UAC disabled.
See Also
- symlink() — Creates a symbolic link
- readlink() — Returns the target of a symbolic link
- linkinfo() — Gets information about a link
- unlink() — Deletes a file
User Contributed Notes 4 notes
in unix/linux:
hardlinks (by this function) cannot go across different filesystems.
softlinks can point anywhere.
in linux, hardlinking to directory is not permited.
For a backup utility I needed link-like functionality on a windows system. As it isn’t availible on windows, i tried to do it myself with the help of some tools. All you need is junction.exe from sysinternals in your %PATH%.
if(! function_exists ( ‘link’ )) < // Assume a windows system
function link ( $target , $link ) if( is_dir ( $target )) // junctions link to directories in windows
exec ( «junction $link $target » , $lines , $val );
return 0 == $val ;
>elseif( is_file ( $target )) // Hardlinks link to files in windows
exec ( «fsutil hardlink create $link $target » , $lines , $val );
return 0 == $val ;
>
I noticed that, differently from Unix ln command, the second parameter can´t be a directory name, i.e., if you want to create a link with the same filename of the target file (obviously on different directories), you must specify the filename on the link parameter.
Example:
Unix ln command:
ln /dir1/file /dir2/ // ok, creates /dir2/file link
PHP link function:
link («/dir1/file», «/dir2/»); // wrong, gives a «File exists» warning
link («/dir1/file», «/dir2/file»); // ok, creates /dir2/file link
In at least php-5.3 (linux-2.6.38.6) a process owned by apache could make a link() in a directory owned by apache, to a file owned by webmaster to which it had group read permissions. In php-7.0 (linux-4.13.16) that results in a «permission denied». Either the target file must be owned by apache or one must use copy() instead (so that ownership changes to apache).
PHP to create Links for files in a folder
I have a folder on my server that will receive monthly drops of newsletter files. These drops will occur automatically and I’ve been asked to write something in PHP to display the list of files as downloadable links while changing the display named based on the name of the file. The folder I’m looking to is «/var/newsletters» and I’m including code on the index.php page at the root directory. The code I have so far is this:
This does display the list of files, but it’s only the first step of the process. They are not linked and they are not renamed. These are monthly newsletter files and are named nmmyyyy.pdf (For example, September would be n092017.pdf). What I need to do is convert n092017.pdf to «September 2017» and then create the link, so something like n092017.pdf and n102017.pdf in the directory becomes
I’ve looked at a few links here:How to list files and folder in a dir (PHP) and List all files in one directory PHP, but found that the code I showed above worked best. What I need help with is displaying the list as links and converting the names. Thank you! EDIT: I was able to get teh link to work with this code:
I’m now working on changing the file name and that is not displaying properly. I’m currently working with it as:
format('F Y'); > // Open a directory, and read its contents if (is_dir($dir))< if ($dh = opendir($dir))< while (($file = readdir($dh)) !== false)< //echo $file . "
"; OR //echo "Click here
"; WORKING $formatted_date = getDateFromFileName($file); echo "
"; > closedir($dh); > > ?>
symlink
symlink() creates a symbolic link to the existing target with the specified name link .
Parameters
Return Values
Returns true on success or false on failure.
Errors/Exceptions
The function fails, and issues E_WARNING , if link already exists. On Windows, the function also fails, and issues E_WARNING , if target does not exist.
Examples
Example #1 Create a symbolic link
$target = ‘uploads.php’ ;
$link = ‘uploads’ ;
symlink ( $target , $link );
?php
See Also
- link() — Create a hard link
- readlink() — Returns the target of a symbolic link
- linkinfo() — Gets information about a link
- unlink() — Deletes a file
User Contributed Notes 20 notes
Here is a simple way to control who downloads your files.
You will have to set: $filename, $downloaddir, $safedir and $downloadURL.
Basically $filename is the name of a file, $downloaddir is any dir on your server, $safedir is a dir that is not accessible by a browser that contains a file named $filename and $downloadURL is the URL equivalent of your $downloaddir.
The way this works is when a user wants to download a file, a randomly named dir is created in the $downloaddir, and a symbolic link is created to the file being requested. The browser is then redirected to the new link and the download begins.
The code also deletes any past symbolic links created by any past users before creating one for itself. This in effect leaves only one symbolic link at a time and prevents past users from downloading the file again without going through this script. There appears to be no problem if a symbolic link is deleted while another person is downloading from that link.
This is not too great if not many people download the file since the symbolic link will not be deleted until another person downloads the same file.
$letters = ‘abcdefghijklmnopqrstuvwxyz’ ;
srand ((double) microtime () * 1000000 );
$string = » ;
for ( $i = 1 ; $i $q = rand ( 1 , 24 );
$string = $string . $letters [ $q ];
>
$handle = opendir ( $downloaddir );
while ( $dir = readdir ( $handle )) <
if ( is_dir ( $downloaddir . $dir )) <
if ( $dir != «.» && $dir != «..» ) <
@ unlink ( $downloaddir . $dir . «/» . $filename );
@ rmdir ( $downloaddir . $dir );
>
>
>
closedir ( $handle );
mkdir ( $downloaddir . $string , 0777 );
symlink ( $safedir . $filename , $downloaddir . $string . «/» . $filename );
Header ( «Location: » . $downloadURL . $string . «/» . $filename );
?>
Get a file link in PHP
We have a VOIP server that stores wav files for call recordings. My intention was to put together a PHP file, where i can pass the calldate and uniqueid value in the URL for the file (since the server stores these as part of the filename) to retrieve a link to the file. However, i get the following error when trying to do so: «Parse error: syntax error, unexpected $end in /var/www/html/maint/helloworld.php on line 42»
$audioDirectory_array = array(); if ($handle = opendir('/var/spool/asterisk/monitor/')) < while (false != ($file = readdir($handle))) < if ($file != "." && $file != "..") < $audioDirectory_array[] = $file; >> closedir($handle); > foreach($audioDirectory_array as $key => $value)< if (preg_match ("/".$uniqueId."/i", $value)) < if(file_exists('/var/spool/asterisk/monitor/'.$value))< $wavFile = $value; //return 'Recorded File'; return '/maint/cache/monitor/' . $wavFile; > >else < //return "Not Recorded"; >getRecordingLink ('20110513','1305274000.2'); //sample calldate and uniqueid values for testing
?> Here is the description of the MySQL table from where i will be picking up values to pass to this URL for calldate and uniqueid`
mysql> describe cdr -> ; +-------------+--------------+------+-----+---------------------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------------------+-------+ | calldate | datetime | NO | | 0000-00-00 00:00:00 | | | clid | varchar(80) | NO | | | | | src | varchar(80) | NO | | | | | dst | varchar(80) | NO | | | | | dcontext | varchar(80) | NO | | | | | channel | varchar(80) | NO | | | | | dstchannel | varchar(80) | NO | | | | | lastapp | varchar(80) | NO | | | | | lastdata | varchar(80) | NO | | | | | duration | int(11) | NO | | 0 | | | billsec | int(11) | NO | | 0 | | | disposition | varchar(45) | NO | | | | | amaflags | int(11) | NO | | 0 | | | accountcode | varchar(20) | NO | | | | | uniqueid | varchar(32) | NO | | | | | userfield | varchar(255) | NO | | | | +-------------+--------------+------+-----+---------------------+-------+