PHP - How To Make Php Files Download Rather Than Execute?
Hey guys,
im starting out on my php journey with a small question. I have a small uploading site and i want to allow php uploads. Once uploaded, you get a direct upload link, and i want the file to download, rather than execute. How can this be done? Thanks! Similar TutorialsHello, I've been assigned the task of creating an internal site for our office that has links to all the websites we use as well as the applications we use. I'm very comfortable using php with mysql for handling forms and displaying database information but I'm not sure how to go about this. So my question is, is there anyway I can use php to open an exe file? Code: [Select] <?php exec('java -jar/MicroChatServer.jar',$output); print_r($output); ?> Hello frds......... I have chat prg. jar file and I have to integrate this chat in a PHP website uses Wampserver.... I try above code but it display "Array()" instead of execute jar file. What should I do???? Any idea?????????????// Not sure if this is the right place for this, but I need to be able to run some PHP code before anything loads up on the server. Seems to be a basic question, but I couldn't find an answer nor figure it out on my own. Basically I have a script that takes out specific data out of the database, the script works on its own, now I just need a way to make the user execute it with a link or a button. Example: Category: [Smileys] - [Category2] - [Category3] - etc. As soon as the user clicks on [Smileys] all data in the database which contains the word smileys in the category field gets selected and outputted as a list. Again the script works, I just need to be able to execute it with a button. If I understood it correctly I have to run the script by adding a if (isset($_POST['Smileys'])) { in front of the script. But how do I build the connection with the text link? I've searched the forum, and haven't yet found what I need. I have a long list of PDF files: http://www.iampeth.com/vintage_magazines.php How can I track the number of downloads for each file? Hi Guys I trying to download files and I have this error message, hopelly you guys can help me please
Warning: Cannot modify header information - headers already sent by (output started at /home/content/04/12746204/html/Digital/core/inc/init.inc.php:6) in /home/content/04/12746204/html/Digital/download.php on line 14 Warning: Cannot modify header information - headers already sent by (output started at /home/content/04/12746204/html/Digital/core/inc/init.inc.php:6) in /home/content/04/12746204/html/Digital/download.php on line 15 Warning: Cannot modify header information - headers already sent by (output started at /home/content/04/12746204/html/Digital/core/inc/init.inc.php:6) in /home/content/04/12746204/html/Digital/download.php on line 16 Warning: Cannot modify header information - headers already sent by (output started at /home/content/04/12746204/html/Digital/core/inc/init.inc.php:6) in /home/content/04/12746204/html/Digital/download.php on line 17 Warning: Cannot modify header information - headers already sent by (output started at /home/content/04/12746204/html/Digital/core/inc/init.inc.php:6) in /home/content/04/12746204/html/Digital/download.php on line 18 This is the code I use to force the download <?php include('core/inc/init.inc.php'); if(isset($_GET['file_id'])) { $file_id=(int)$_GET['file_id']; $file=mysql_query("SELECT file_name, file_expiry FROM files WHERE file_id=$file_id"); if(mysql_num_rows($file)!=1){ echo'Invalid file Id'; }else{ $row=mysql_fetch_assoc($file); if ($row['file_expiry']<time()){ echo'This file has expired'; }else{ $path="core/files/{$row['$file_name']}"; header('Content-Type:application/octet-stream'); header('Content-Description:File Transfer'); header('Content-Transfer-Encoding:binary'); header("Content-Disposition:attachment;filename=\"{$row['file_name']}\""); header('Content-Length:'.filesize($path)); readfile($path); } } } ?>Thank you for your help..... Hi guys i am kinda stuck at a part where i want a download button to prompt out two download windows in a click. Currently i am able to prompt out one download window only. I have tried putting another header line of Content Disposition but it doesn't work..i will appreciate if you guys can help me out, thanks! The desired scenario will be WinSCP.exe download prompt will appear first, follow by auditgroup.bat $file1 = "WinSCP.exe"; $file = "auditgroup.bat"; //Set headers header("Cache-Control: private"); header("Expires: 0"); header("Pragma: cache"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$file"); header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: ASCII"); //Read the file from disk readfile($file); Hi PHP Community, I have this class in my DOCMAN script and would like to add aditional functionality to watermark existing PDF files: Code: [Select] class DOCMAN_File { /** * @access public * @var string */ var $path = null; /** * @access public * @var string */ var $name = null; /** * @access public * @var string */ var $mime = null; /** * @access public * @var string */ var $ext = null; /** * @access public * @var string */ var $size = null; /** * @access public * @var string */ var $date = null; /** * @access private * @var string */ var $_err = null; /** * @access private * @var boolean */ var $_isLink; function DOCMAN_File($name, $path) { $path = mosPathName( $path ); if (!is_dir( $path )) { $path = dirname( $path ); } $this->name = trim($name); $this->path = $path; if( strcasecmp( substr( $this->name , 0, _DM_DOCUMENT_LINK_LNG ) , _DM_DOCUMENT_LINK )==0){ $this->_isLink = true; $this->size = 0; $this->mime = 'link'; }else{ $this->_isLink = false; $this->size = filesize($this->path."/".$this->name); $this->mime = DOCMAN_MIME_Magic::filenameToMIME($this->name, false); } $this->ext = $this->getExtension(); } /** * Download or view a file from the server * @desc This is the function handling files downloading using HTTP protocol * @param void * @return void */ function download() { $user_agent = (isset($_SERVER["HTTP_USER_AGENT"]) ) ? $_SERVER["HTTP_USER_AGENT"] : $HTTP_USER_AGENT; //Fixed [#2534] clean all output buffers (needs PHP > 4.2.0) /* while (ob_get_level()) { * ob_end_clean(); }*/ // Fix [3164] while (@ob_end_clean()); if( $this->_isLink ){ header( "Location: " . substr( $this->name , 6 ) ); return; } $fsize = filesize($this->path."/".$this->name); $mod_date = date('r', filemtime( $this->path.'/'.$this->name ) ); $cont_dis = 'attachment;'; if(isset($_REQUEST['mode'])) $cont_dis = ($_REQUEST['mode'] == 'view') ? 'inline;' : 'attachment;'; header("Pragma: public"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Expires: 0"); header("Content-Transfer-Encoding: binary"); header('Content-Disposition:' . $cont_dis .'";' . ' filename="' . $this->name . '";' . ' modification-date="' . $mod_date . '";' . ' size=' . $fsize .';' ); //RFC2183 header("Content-Location: ". $this->path . '/' . $this->name ); header("Content-Type: " . $this->mime ); // MIME type header("Content-Length: " . $fsize); // No encoding - we aren't using compression... (RFC1945) // header("Content-Encoding: "); readfile($this->path."/".$this->name); // The caller MUST 'die();' } function exists() { if( $this->_isLink ){ return true; } return file_exists($this->path."/".$this->name); } function isLink(){ return $this->_isLink ; } /** * Get file size * * @desc Gets the file size and convert it to friendly format * @param void * @return string Returns filesize in a friendly format. */ function getSize() { if( $this->_isLink ){ return 'Link'; } $kb = 1024; $mb = 1024 * $kb; $gb = 1024 * $mb; $tb = 1024 * $gb; $size = $this->size; if ($size) { if ($size < $kb) { $file_size = "$size Bytes"; } elseif ($size < $mb) { $final = round($size/$kb,2); $file_size = "$final KB"; } elseif ($size < $gb) { $final = round($size/$mb,2); $file_size = "$final MB"; } elseif($size < $tb) { $final = round($size/$gb,2); $file_size = "$final GB"; } else { $final = round($size/$tb,2); $file_size = "$final TB"; } } else { if( $size == 0 ) $file_size = "Empty!"; else $file_size = "ERROR"; } return $file_size; } /** * @desc Gets the extension of a file * @return string The file extension */ function getExtension() { if( $this->_isLink ) return "lnk"; $dotpos = strrpos($this->name, "."); if ($dotpos < 1) return "unk"; return substr($this->name, $dotpos + 1); } function getDate($type = 'm') { if( $this->_isLink ){ return ""; } $date = ''; switch($type) { case 'm' : $date = filemtime($this->path."/".$this->name); break; case 'a' : $date = fileatime($this->path."/".$this->name); break; case 'c' : $date = filectime($this->path."/".$this->name); break; } return date ("m.d.y - H:i:s", mosFormatDate($date)); } } I found this function on the net and it works fast. But because I'm newbie I have problem in implementing it correctly to class: Code: [Select] function WaterMark($filename){ $tempfile=tempnam('/path/to/directory/tmp,'temp.pdf'); $watermark = "/path/to/directory/htdocs/components/com_docman/includes_frontend/water.pdf"; system("pdftk $filename stamp $watermark output $tempfile dont_ask", $errcode); if (!$errcode && $ih=fopen($tempfile, 'r')) { header('Content-Type: application/pdf'); fpassthru($ih); fclose($ih); } else { print "Whoops"; } unlink($tempfile); } Here is frontend use of class. Code: [Select] $downloadDoc = new DOCMAN_File($document->dmfilename,$_DOCMAN->getCfg('dmpath')); $downloadDoc->download(); For now I only managed to get it working to watermark PDF file for view mode but not before download mode as attachment. Function is working fine by itself but not when integrated in class. I'm getting really frustrated . Please help me!?! Thank you everybody. Al Hi, how much bandwidth is used for 1000 downloads per day or for each download? Is it safe to have no download limits? Thanks
I have a Joomla website having one folder for audio files in it and their links are saved in database. Now I want to show two calendars to the user at front end, so that they can choose that from when to from he want to download the audio files? for e.g he can select june-2-2014 in one calendar and august-2-2014 in second calendar, now on clicking download button the files residing between the two specific dates must get archived into one zip file and then start downloading... i have a reference link for creating a zip and downloading the zip :http://coursesweb.ne...-archive-php_cs now i am stuck that how to get the selection of user from calendar , save it into the array , and from the array get the files from the folder matching the array indexes and then zip , download them. please tell me how to achieve this task ? i need a download system like www.filehorse.com. can plz tell me how? its generate link 4 download for limited time. without giveing hot link. plztake a look and help me out
Hi All I am trying to set up a system that where a user sets up an account the system automatically sets up a new directory and inserts template files into it. I understand mkdir is something to do with it. Does anyone know how to go about this please? thanks I am working on a little project for my site and I am trying to get this script to ignore the index.html file and the .htaccess file in there. I just want it to display all files like .zip, .rar, .tar, .gz, .gtar, .ace, ect... echo " <td width='70%'><select id='download'>"; echo " <option value=''>None</option>"; if ($handle = opendir("./modules/Downloads/files")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo " <option value='".$file."'>".$file."</option>"; } } closedir($handle); } echo " </td>\n"; I have a folder containing files. I want to replace some characters in the file names (e.g. "_" with "-"), then make a list of new files (renamed ones). Thanks I'm a beginner in PHP I was wondering if this is easy to do or if i'd have to outsource this to a programmer - Basically when a user fills in the PHP Form and submits it I need this to generate as a PDF which will then email/attach to MY email and NOT the user who submitted this form. I have looked at tcpdf, fpdi but i dont think any of those scripts allow me to do this specifically as from what i heard it generates a download link for the user, and that is not what i need. If anyone can help me it would be greatly appreciated. Hello, I'm working on a script which should read lines from a file keyword.txt create a new file with that variable name, insert the content close and loop through all the lines in the keyword file. The loop itself works but now that i'm adding the fopen fwrite and fclose i'm running into trouble. I'm not that great a coder and have pieced this together so far. I'd appreciate any suggestions on how to fix the fopen and other file functions to create unique files using the keywords from the file Thanks in advance <?php $read_file = file('keywords.txt'); foreach($read_file as $var) { $file = fopen($var, "x"); $content = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> <title>This is the title</title> </head> <body> <h2>The keyword is $var</h2> <p><p> </body> </html>"; fwrite($file, $content); fclose($file); } ?>
Basically I would like to place a link on my website and have the user download a file, but rather than just right clicking and choosing save target as, the link must be clicked, on the next page the file is fetched and then the client can download the file. How would I go about setting this up please? I have made a Php program that downloads an Inno setup installation file for installing a program. However, if I for one or another reason want to make a new download of the same Inno setup installation file, the previous file will still be found in the Download folder. Each of the downloads get a number in parenthesis, setup(1), setup(2), setup(3) etc. However, I wondered if it is posible to erase the previous file in the same process as I download a new one, so that however many downloads I do, there will all the time only be one occurence of this file in the Download folder. The download code is as follows: $exe = "Inno script/Test_setup.exe"; header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"Test_setup.exe\""); header("Content-Length: " . filesize($exe)); readfile($exe); Thanks in advance. Sincerely
Hello I have a simple question about file handling... Is it possible to list all files in directories / subdirectories, and then read ALL files in those dirs, and put the content of their file into an array? Like this: array: [SomePath/test.php] = "All In this php file is being read by a new smart function!"; [SomePath/Weird/hello.txt = "Hello world. This is me and im just trying to get some help!";and so on, until no further files exists in that rootdir. All my attempts went totally crazy and none of them works... therefore i need to ask you for help. Do you have any ideas how to do this? If so, how can I be able to do it? Thanks in Advance, pros So far I have managed to create an upload process which uploads a picture, updates the database on file location and then tries to upload the db a 2nd time to update the Thumbnails file location (i tried updating the thumbnails location in one go and for some reason this causes failure) But the main problem is that it doesn't upload some files Here is my upload.php <?php include 'dbconnect.php'; $statusMsg = ''; $Title = $conn -> real_escape_string($_POST['Title']) ; $BodyText = $conn -> real_escape_string($_POST['ThreadBody']) ; // File upload path $targetDir = "upload/"; $fileName = basename($_FILES["file"]["name"]); $targetFilePath = $targetDir . $fileName; $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); $Thumbnail = "upload/Thumbnails/'$fileName'"; if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){ // Allow certain file formats $allowTypes = array('jpg','png','jpeg','gif','pdf', "webm", "mp4"); if(in_array($fileType, $allowTypes)){ // Upload file to server if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ // Insert image file name into database $insert = $conn->query("INSERT into Threads (Title, ThreadBody, filename) VALUES ('$Title', '$BodyText', '$fileName')"); if($insert){ $statusMsg = "The file ".$fileName. " has been uploaded successfully."; $targetFilePathArg = escapeshellarg($targetFilePath); $output=null; $retval=null; //exec("convert $targetFilePathArg -resize 300x200 ./upload/Thumbnails/'$fileName'", $output, $retval); exec("convert $targetFilePathArg -resize 200x200 $Thumbnail", $output, $retval); echo "REturned with status $retval and output:\n" ; if ($retval == null) { echo "Retval is null\n" ; echo "Thumbnail equals $Thumbnail\n" ; } }else{ $statusMsg = "File upload failed, please try again."; } }else{ $statusMsg = "Sorry, there was an error uploading your file."; } }else{ $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, mp4, webm & PDF files are allowed to upload.'; } }else{ $statusMsg = 'Please select a file to upload.'; } //Update SQL db by setting the thumbnail column to equal $Thumbnail $update = $conn->query("update Threads set thumbnail = '$Thumbnail' where filename = '$fileName'"); if($update){ $statusMsg = "Updated the thumbnail to sql correctly."; echo $statusMsg ; } else { echo "\n Failed to update Thumbnail. Thumbnail equals $Thumbnail" ; } // Display status message echo $statusMsg; ?> And this does work on most files however it is not working on a 9.9mb png file which is named "test.png" I tested on another 3.3 mb gif file and that failed too? For some reason it returns the following Updated the thumbnail to sql correctly.Updated the thumbnail to sql correctly. Whereas on the files it works on it returns REturned with status 0 and output: Retval is null Thumbnail equals upload/Thumbnails/'rainbow-trh-stache.gif' Failed to update Thumbnail. Thumbnail equals upload/Thumbnails/'rainbow-trh-stache.gif'The file rainbow-trh-stache.gif has been uploaded successfully. Any idea on why this is? |