I was making a script for remote upload files to my server and here the code. BTW i took it from a site. I didn't post the site name or it might b considered as spam or something. Its about downloading remote files into our server.
<?php
if (isset($_POST['myupload']))
{
$links_list = $_POST['upload'];
$incr = 0;
$links = explode("
",$links_list);
define('BUFSIZ', 4095);
for ( $incr == 0 ; $incr < count($links) ; $incr++ )
{
$url = $links[$incr];
$rfile = fopen($url, 'r');
$lfile = fopen(basename($url), 'wb');
while(!feof($rfile))
fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ);
fclose($rfile);
fclose($lfile);
}
}
?>
<script type="text/javascript"></script>
</head>
<body>
<div id="upload_box">
<form action="" method="post">
<textarea name="upload" cols=80 rows=20></textarea>
<input type="submit" name="myupload" value="Upload Files">
</form>
I want to ask few things about this script.
$lfile = fopen(basename($url), 'wb')
< what does this code do ?
fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ);
< and what about this code ?
I know am asking a foolish question but hope you guys could shed some light on that for me.
$lfile = fopen(basename($url), 'wb')
fopen
: Function that opens a file and returns a handle that can be used to read/write that file, depending on the second argument (explained below).
basename
: Function that strips the path from the file name, leaving just the name part. For example: basename('http://foo.com/bar.txt')
would return bar.txt
'wb'
: This tells fopen how to open the file: w
means open it for writing, b
means open it for binary access, which just means don't do any funky line ending translation (only really useful on Windows).
$lfile
: This is the handle to the file that will be used later to write to it.
fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ);
fwrite
: Writes data to a previously opened file handle ($lfile
in this case)
fread
: Reads data from a file.
BUFSIZ
: I'm assuming this is a constant defined somewhere. Regardless, the 3rd parameter to fwrite
specifies how many bytes to write to the file, and the 2nd parameter to fread
specifies how many bytes to read. In this case, they are both the same.
This kind of operation is sometimes known as a buffered or block copy.
Broken apart it would look something like this:
$data = fread($rfile, BUFSIZ);
fwrite($lfile, $data, BUFSIZ);
Hope that helps!
1st > $lfile = fopen(basename($url), 'wb') < what does this code do ?
This line opens the $url and returns handle for that url.
Basename()
Given a string containing the path to a file or directory, this function will return the trailing name component.
2nd > fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ); < and what about this code ?
This will write for stream $lfile
the content of stream $rfile
and size of reading/writing is 4095 defined define('BUFSIZ', 4095);
Take a look at this part
while(!feof($rfile))
fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ);
This part will read the data in portions defined by BUFSIZ until the character "end of file" feof()
function.
So your code will download the links that are posted in form. That's all what it does.
$lfile = fopen(basename($url), 'wb')
creates a file pointer to write binary file using the last segment of the url provided (which is usually the filename) and returns the file pointer/handler to the variable named $lfile
fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ);
reads BUFSIZ long bytes from file pointer/handler $rfile and push to to be written to the file pointed by $lfile. Writes also BUFSIZ amoung of bytes