Hi,
I want to write a script which can do the following job, i have looked with Google but could not find any.
The script should click a link and get the file to download on the server on which the script is running to a folder.

The link to the download is not a direct link such as website.com/file.zip rather it is a dynamic code which once clicked opens the dialog box for download. I want to save that file to the server.

There is one other option, we can also put it on the clients machine and then fetch it from there but i also do not know how this can be done.

I hope some one gives a detailed answer.

Thanks

Recommended Answers

All 3 Replies

Member Avatar for diafol

Hi,
I want to write a script which can do the following job, i have looked with Google but could not find any.
The script should click a link and get the file to download on the server on which the script is running to a folder.

The link to the download is not a direct link such as website.com/file.zip rather it is a dynamic code which once clicked opens the dialog box for download. I want to save that file to the server.

There is one other option, we can also put it on the clients machine and then fetch it from there but i also do not know how this can be done.

I hope some one gives a detailed answer.

Thanks

I don't think that the download/save dialog will save to the server as the download/save dialog is part of the browser and can therefore only save to the client machine - unless you mess with FTP - but I don't know if any browser allows this via "one stop".

The php copy function should be able to deal with this. I'd suggest having a list of file links, something like:

filelist.php

<a href="download.php?file=file1.jpg" ...>file1.jpg</a>

(obviously this would be a little more sophisticated and filenames need to be urlencoded)

download.php

<?php
if(isset($_GET['file'])){
  $file = $_GET['file']; //urldecode it 
  $value = basename($file);
}
?>
<form method="post" action="getfile.php">
<input id="file" name="file" type="hidden" value="<?php echo $file;?>" />
<label for="dir">Save to folder:</label>
<input id="dir" name="dir" type="text" /> {leave blank for root folder} 
<label for="dest">Save file as:</label>
<input id="dest" name="dest" type="text" value = "<?php echo $value;?>" />  
<input id="copyfile" name="copyfile" type="submit" value="Copy" />
</form>

The called page:

getfile.php

<?php
$file = $_POST['file'];
$dir = $_POST['dir'];
$dest_filename = $_POST['dest'];

//clean up the variables

$dest_server_dir = $_SERVER['DOCUMENT_ROOT'] . '/' . $dir;

if (!copy($file, $dest_server_dir.$dest_filename)) {
    echo "failed to copy $file...\n";
}
?>

This is all relatively straightforward, but you could ajaxify it and include the form in a lightbox popup. Prototype and jQuery have nice examples.

This script will put it on the clients machine and then i will need to upload it?

Can some one explain how can this be done with the FTP thing as told be the ardav...

Ardav i am really grateful for such a detailed reply.

Thanks

I can't tell you exactly how it works under Windows as I haven't tried it for a long time now.

With FTP or AFS you can basically access servers like the drives in your computer. Just google a bit and try to find something like "FTP Mount Windows" etc. Then you can maybe choose the webserver as the drive where you want the download to store. This at least works perfectly under UNIX.

If you're doing it with PHP, then you can have a look at the FTP-documentation and write something like that yourself. It's a bit work but actually not too tricky.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.