Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
i got this php code below from internet.I saved it in txt file and saved name to abc.php. but it is not running. when opening the file on chrome. it just showing the code

<?php
// Check download token
if (empty($_GET['mime']) OR empty($_GET['token']))
{
exit('Invalid download token 8{');
}
// Set operation params
$mime = filter_var($_GET['mime']);
$ext  = str_replace(array('/', 'x-'), '', strstr($mime, '/'));
$url  = base64_decode(filter_var($_GET['token']));
$name = urldecode($_GET['title']). '.' .$ext; 
// Fetch and serve
if ($url)
{
// Generate the server headers
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
{/*
    header('Content-Type: "' . $mime . '"');
    header('Content-Disposition: attachment; filename="' . $name . '"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header("Content-Transfer-Encoding: binary");
    header('Pragma: public');
*/}
else
{/*
    header('Content-Type: "' . $mime . '"');
    header('Content-Disposition: attachment; filename="' . $name . '"');
    header("Content-Transfer-Encoding: binary");
    header('Expires: 0');
    header('Pragma: no-cache');
*/}
$download_video_file = file_put_contents($file_path, fopen($url, 'r'));
exit;
}

// Not found
exit('File not found 8{');
?>
Posted

PHP code cannot be executed in the browser. PHP can run through a PHP interpreter which you can install or on the server side of an HTTP server setup to process PHP (virtually all Web hosting packages provide this opportunity). As the code you are trying to use starts with "<?php", it means that it was developed to run in a Web application on a server side. Moreover, it is designed to serve as a response to some HTTP request, but the page or anything sending a request is not shown. The code does next to nothing; looks like a small fragment of something.

This code is practically useless for you, no matter what your purpose is. First of all, you should understand how Web works; but your question shows that at this moment you have no clue… Maybe we could help you if you explain your purpose.

[EDIT]

To download any file from an HTTP server, you have to send an HTTP request to it. If a file is big, you have a possibility to indicate in a request from what offset to start and what size of the block to download (partial download), and repeat it appropriate number of times. A Web server does it for you just on a click of an anchor element pointing to a file to download. Would it be enough for you?

If you really need to download a file using PHP on a server side, to a file on a server side, the answer depends on where is the source file. If it is on your host (under the root directory set up for your Web site), there is nothing to download, you just need to read this file, but you may need to force downloading it on client's request. Please see: http://www.media-division.com/the-right-way-to-handle-file-downloads-in-php[^].

See also: http://www.php.net/manual/en/function.readfile.php[^].

If you need to download a file from some other site, you can open the file and write it to the output (whatever it is, could be the . The problem, again, is when the file is big, then reading it to memory can exhaust the available RAM. To read the file piece by piece, you can just open input stream, as the line you show does:
PHP
$download_video_file = file_put_contents($file_path, fopen($url, 'r'));

This line put the content of the file directly to the content of the HTTP response. Also, you need to determine right HTTP header (commented-out in your code). Please see:
http://www.php.net//manual/en/function.header.php[^].

To correctly define a content type, please see where they are standardized:
http://en.wikipedia.org/wiki/Content_type[^];
the standard types are supported by IANA here:
http://www.iana.org/assignments/media-types[^].

See also: http://en.wikipedia.org/wiki/Internet_Assigned_Numbers_Authority[^].

In PHP resource like that one, you whole PHP page is "substituted" instead of the "real" file to be downloaded, possibly doing some job on the fly. To put it to work, you need an HTTP request to request this page, say, HTML with the anchor. Usually, it is done of some calculation of the content is done on the page (such as creation of graphics). For example, such script can calculate the URI to be downloaded from, based on some algorithm. I have no idea why would you need this, because, to let the user download some file, you could directly give the user it's URI. You need to do the calculations on the server side only if you need to do something special.

—SA
 
Share this answer
 
v3
Comments
veena nair 8-Jun-14 6:35am    
i need to create an web page or app that help me to download youtube videos to my web server directly other than saving it my computer. can you help me to do that
As Sergey said, this is a server-side script that can only be understood and executed by PHP engine on a server like Apache. The output of which will be the HTML web page that you see on the browser. PHP in its raw form presents no clue to the browser.
Refer:
1. To get basic understanding: Beginner's Guide to HTML5 & CSS3 - Server Side Story[^]
2. To get into serious learning: Web Development tutorials[^]
 
Share this answer
 
v3
Comments
veena nair 8-Jun-14 6:35am    
i need to create an web page or app that help me to download youtube videos to my web server directly other than saving it my computer. can you help me to do that
Peter Leow 8-Jun-14 8:17am    
This is do-able, but I will not do it. In general, downloading videos that other people have posted on YouTube is not allowed. Refer: https://support.google.com/youtube/answer/6035694?rd=1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900