Question

I am trying to create a page for just personal use. What I want to do is, create a system through which I can download images to my local hard disk, directly by providing the link through a localhost like WAMP. The reason I am trying to do is, I want the images to be automatically sorted onto my hard disk. My form field will be something like this

<form method="POST" action="process.php">
   <input type="text" name="URL" id="URL" />
   <input type="text" name="category" id="category" />
   <input type="text" name="subcategory" id="category" />
   <input type="submit">
</form>

Now, in the process.php

//This is just a sample... So please ignore the roughness of the coding

copy($_POST['url'],$_POST['category']."/".$_POST['subcategory']."/somename.jpg");

// If you noticed, the categories and subcategories are actually the name of directory, where I want the picture to go be saved....

I am thinking my approach is wrong. How can I achieve something like this?

Error

Warning: copy(images/image.jpg) [function.copy]: failed to open stream: No such file or directory in
Was it helpful?

Solution 2

Ok, I used curl instead to achieve what i am expecting. Here is the piece of code

$img = $_POST['url'];
$fullpath = $_POST['category']."/".$_POST['subcategory']."/".basename($img);
$ch = curl_init ($img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);                   
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);

OTHER TIPS

If allow_url_fopen is true in the php.ini and your PHPVERSION is >= 4.3.0 your code should work.

you can first download from php script document from specified url, then give http-client link to download locally stored file. or put document content to output stream:

 $cont = file_get_contents($_GET['url']);
  header('Content-Type: application/octet-stream');
  header("Content-Transfer-Encoding: binary ");
  header('Accept-Ranges: bytes');
  header('Content-disposition: attachment; filename=' . $new_file_name));
  echo($cont);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top